ITIP © Ron Poet Lecture 14 1 Responsibilities. ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job,

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Helper Methods ITP © Ron Poet Lecture 11.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Cosc 4755 Phone programming: GUI Concepts & Threads.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
CS 201 Functions Debzani Deb.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object Oriented Software Development
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
ITP © Ron Poet Lecture 2 1 Mental Models. ITP © Ron Poet Lecture 2 2 Mental Models  A mental model is a way of making sense of something we experience.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Classes / Objects An introduction to object-oriented programming.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Java Classes Using Java Classes Introduction to UML.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Utilities (Part 2) Implementing static features 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CS107 References and Arrays By Chris Pable Spring 2009.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 CS161 Introduction to Computer Science Topic #9.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
1  lecture slides online
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
ITP © Ron Poet Lecture 15 1 Helper Objects Again.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Unified Modeling Language (UML)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
Andrew(amwallis) Classes!
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Abstract Data Types II.
More Object Oriented Programming
Object Oriented Programming
Week 4 Lecture-2 Chapter 6 (Methods).
Object Oriented Programming Review
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Agenda Warmup Lesson 2.2 (parameters, etc)
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Methods/Functions.
Inner Classes 25-Oct-19.
Presentation transcript:

ITIP © Ron Poet Lecture 14 1 Responsibilities

ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job, the first question to answer is:  Who does what?  We can only start to work out details of the individual tasks once we have divided up the responsibilities.

ITIP © Ron Poet Lecture 14 3 User Interface is Separate  One very useful guideline is that the user interface should be separate from code that does the work.  main can be responsible for the dialog with the user (user interaction).  A helper object is then responsible for acting on the user’s wishes.

ITIP © Ron Poet Lecture 14 4 Drawing a Box  There are 4 methods in the exercise 2 version.  main  drawBox  drawTop  drawSide  drawRow in exercise 3 version.  Assigning responsibilities will govern the information flow: parameters and returns.

ITIP © Ron Poet Lecture 14 5 drawTop, drawSide, drawRow  These responsibilities are easy to assign.  Each draws one line of characters.  They are given a Console and do not need to create it.  They are given the number of characters on the line.  drawRow is also told the end and middle characters.  They do not return anything.

ITIP © Ron Poet Lecture 14 6 drawBox  These responsibilities are harder to work out.  Clearly it should draw the box!  It should use drawTop and drawSide.  They are helpers for the helper!  This means that it should provide drawTop and drawSide with the information they need.  A Console for drawing  The width of the box.

ITIP © Ron Poet Lecture 14 7 drawBox (2)  Should drawBox create the Console where the box is drawn?  The spec says that each box is drawn on a new Console.  So it is reasonable to ask drawBox to create this Console.  It does not need a Console as input.

ITIP © Ron Poet Lecture 14 8 drawBox (3)  Should drawBox get information from the user?  NO. That would spread its responsibilities too far.  So drawBox needs nRows and nCols as inputs.  Some other part of the code should get this information from the user.

ITIP © Ron Poet Lecture 14 9 main  The remaining task is interacting with the user.  This should be done by main.  It should control the loop, asking if the user wants to draw another box.  It should also get the number of rows and columns for each box.

ITIP © Ron Poet Lecture Main has two tasks?  main does two things.  Gets the information for each box.  Controls the loop to draw many boxes.  We could make a case that we need two methods, one for each of these tasks.  However, both tasks are small and related.  It makes sense to let main do both of them.  Balance the number of different methods with their length.

ITIP © Ron Poet Lecture Helper Methods Summary  We define a helper method for each task.  Each method does one thing well.  User interaction is a separate task.  It should have a method dedicated to it.  That method is quite often main.  Do not mix user interaction with other tasks.

ITIP © Ron Poet Lecture Displaying Information  Displaying information, such as drawing a box, is not user interaction.  drawBox, drawTop and drawSide all display information.  They need a Console for the display.  They are not interacting with the user.  Someone who talks all the time but does not listen is not interacting!

ITIP © Ron Poet Lecture Static Objects

ITIP © Ron Poet Lecture Unique Omnipresent Objects  Sometimes we want to create a single helper object that is always there.  All the methods of these sorts of object must be called static.  The single unique object does not need to be created, it is always there.  Its name is the class name.  Methods are called using this class name.

ITIP © Ron Poet Lecture No Constructors  They do not have constructors.  Instance variables can be initialised where they are defined.  See an example later.  All main programs are static objects.

ITIP © Ron Poet Lecture Java Static Objects  System.out and System.err  These are the names of the classes.  Methods are called using these names.  System.err.println(“Hello World”);  Java has many other static objects.

ITIP © Ron Poet Lecture Memoriser Object  This silly example is an object that memorises any number we tell it.  It has 2 methods.  public static void remember(int num);  public static int tellMe();  It stores the number in a static instance variable.  private static int theNumber;

ITIP © Ron Poet Lecture The Code public class Memoriser { private static int theNumber = 0; public static void remember(int num) {theNumber = num;} public static int tellMe() { return theNumber; } }

ITIP © Ron Poet Lecture Mixed Objects  Ordinary objects can have static members as well.  static methods  static instance variables.  Ordinary methods can use static members.  The static object is always there.  static methods can only use static members.

ITIP © Ron Poet Lecture Current Time  It would be useful if our MyTime class had a method the returned the current time.  This would be a static method.  It must be always available.  It can be implemented using the Java System object, which is also static.  It has a method that returns the current time counted in milliseconds since 1Jan  We add this method to the existing ones.

ITIP © Ron Poet Lecture Code public static MyTime currentTime() { // convert to seconds, divide by 1000 int secs = System.currentTimeMillis() / 1000; // convert to mins, divide by 60 int mins = secs / 60; // remainder divide by 1440 (mins in day) int mins %= 1440; // return object return new MyTime(mins / 60, mins % 60); }

ITIP © Ron Poet Lecture More on this  this can be used inside methods.  It is the object that called the method.  static methods don’t have this.  Instance variables can have this in front of them. It can make the code clearer.  this.hours.  Or we can leave this out.  hours.

ITIP © Ron Poet Lecture Reusing Constructors  One constructor can call other constructors.  They use the word this as the first line of the constructor.  Parameters can be passed this way.  this is useful if we want to provide default parameters to a constructor.

ITIP © Ron Poet Lecture Console Constructors public Console(String name, int nr, int nc) { // the real constructor } // provide default parameters public Console() { this(“Console Window”, 30, 80); } public Console(String name) { this(name, 30, 80); } public Console(int nr, int nc) { this(“Console Window”, nr, nc); }