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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

ITP © Ron Poet Lecture 4 1 Program Development. ITP © Ron Poet Lecture 4 2 Preparation Cannot just start programming, must prepare first. Decide what.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Helper Methods ITP © Ron Poet Lecture 11.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Enhancing classes Visibility modifiers and encapsulation revisited
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Lecture 9 Concepts of Programming Languages
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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.
Chapter 4 Objects and Classes.
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
ITP © Ron Poet Lecture 3 1 Comments. ITP © Ron Poet Lecture 3 2 Legibility  It is important that programs are easy to read.  It is easier to find bugs.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 8: User-Defined Classes and ADTs
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
ITI 1120 Lab #11 Contributors: Diana Inkpen, Daniel Amyot, Sylvia Boyd, Amy Felty, Romelia Plesa, Alan Williams.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
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,
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
ITP © Ron Poet Lecture 15 1 Helper Objects Again.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
CompSci 230 S Programming Techniques
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Java Course Review.
Java Programming: Guided Learning with Early Objects
Lecture 5: Some more Java!
CompSci 230 Software Construction
Incrementing ITP © Ron Poet Lecture 8.
Lecture 9 Concepts of Programming Languages
User-Defined Classes and ADTs
Building Java Programs
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 8: User-Defined Classes and ADTs
Java Classes and Objects 3rd Lecture
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Tonga Institute of Higher Education
Building Java Programs
References Revisted (Ch 5)
Lecture 9 Concepts of Programming Languages
Presentation transcript:

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 they provide.  Console objects stay around for a while.  But they are not permanent.  They must be created with new  They can be closed before the program finishes.  We can call a series of different methods.

ITP © Ron Poet Lecture 13 3 Helper Object / Helper Method  A helper method does a single action.  Draw a box, play a single game.  And then it is finished.  A helper object remembers information from one method call to the next.  The Console window and characters displayed in it.

ITP © Ron Poet Lecture 13 4 Example: MyTime Objects  A MyTime object stores a single time.  It supports time based methods such as  Subtracting times  Adding times.  Converting times to and from strings.  Using 24 hour or 12 hour clocks.  It can have many methods.

ITP © Ron Poet Lecture 13 5 Can Be Used By Many Programs  Console objects are helper objects  They are used by many programs.  MyTime objects can also be used by many programs.  This cuts down programming work  We can share code between many programs.  The code only has to be tested once.

ITP © Ron Poet Lecture 13 6 Expert In One Thing  MyTime just knows about time.  It does not know about dates or other related topics.  This makes it easy to use in many different programs.  It is not specialised.

ITP © Ron Poet Lecture 13 7 Class and Objects  All MyTime objects have the same properties.  The methods such as adding times.  We define one MyTime class.  The Java code representing a time.  Each MyTime object can store a different time.  We create MyTime objects as we need them.  Using new.

ITP © Ron Poet Lecture 13 8 Instance Variables  Instance variables can be used by all methods of a MyTime object.  They have a wide scope.  They are defined inside the class rather than inside a method.  Methods can still define local variables.  They can only be used inside the methods in which they are defined.

ITP © Ron Poet Lecture 13 9 Public Class and Methods  The MyTime class is public.  It can be used by any other bit of code.  It is used when we create MyTime objects.  The MyTime methods are public.  They can be called by any other bit of code.  Methods are not static.  MyTime is not a unique, omnipresent object.

ITP © Ron Poet Lecture Private Data  The instance variables are private.  They can only be used by methods in the MyTime class.  We can only influence the instance variables by calling methods.  This makes the code easier to understand and maintain.  There are no hidden ways of changing the variables.

ITP © Ron Poet Lecture One class per file  We need to create a new file called MyTime.java.  eclipse will do this automatically when we create a new class.  In Java, each public class must be in a separate file.  The filename must also match the class name.

ITP © Ron Poet Lecture MyTime – Instance Variables public class MyTime { privateint hours, mins; // methods go here }

ITP © Ron Poet Lecture Initialisation - Constructors  Objects must be initialised before being used, just like other variables.  The instance variables must be given initial values.  A constructor method does this.  It is called when the object is created using new.  Its name is the same is the class.  It does not have a return type, not even void.

ITP © Ron Poet Lecture MyTime – Constructor public class MyTime { privateint hours, mins; // last lifetime of the object // each object has its own copy of them. // constructor public MyTime(int h, int m) { hours = h; // remember them before mins = m; // they are destroyed } // h and m are destroyed here } // example call MyTime a = new MyTime(1, 30);

ITP © Ron Poet Lecture Constructor Explained  hours and mins are instance variables.  They exist as long as the object exists.  They can be used by any method.  They are private and cannot be used by main.  h, m are parameters of the constructor.  Only last while the constructor is being called.  Long enough to initialise hours and mins.  They get their values from the constructor call.

ITP © Ron Poet Lecture Accessor Methods  Accessor methods just return information about the object without changing it.  We can have methods to return the hours and mins.  By convention, accessors start with get. // accessors public int getHours() { return hours; } public int getMins() { return mins; }  Methods that change the instance variables are called transformers.

ITP © Ron Poet Lecture Different Internal Representation  Private data means that we can change the instance variables without any customers noticing.  They cannot get at private parts of a class.  We can store a time as totMins (total minutes) instead.  We rewrite the class internals, but not the method headings.  It looks the same to all customers.

ITP © Ron Poet Lecture MyTime – totMins public class MyTime { privateint totMmins; // constructor public MyTime(int h, int m) { totMins = 60 * h + m; } public int getHours() { return totMins / 60; } public int getMins() { return totMins % 60; } } // example call MyTime a = new MyTime(1, 30);

ITP © Ron Poet Lecture to 10h, 30m  Time can be represented by a single number of the form hhmm.  MyTime can convert to and from this representation.  Converting from hhmm to a MyTime object is constructing.  Converting the other way is an accessor method.

ITP © Ron Poet Lecture hhmm Methods public MyTime(int hhmm) { hours = hhmm / 100; mins = hhmm % 100; } public int getHhmm() { return 100 * hours + mins; }

ITP © Ron Poet Lecture More Than One Constructor  We can have several constructors  Provided they have different parameters.  The compiler can work out which is which.  We must not forget one of the parameters or we will call the wrong constructor.  They all have the same name: MyTime. MyTime a = new MyTime(10, 30); MyTime b = new MyTime(1030);

ITP © Ron Poet Lecture Adding and Subtracting Times public MyTime sub(MyTime b) { int totMins = 60 * this.hours + this.mins - 60 * b.hours - b.mins; if (totMins < 0) totMins += 1440; return new MyTime(totMins/60, totMins%60); }

ITP © Ron Poet Lecture this  Note that the new object, the difference between the two times, is created using new.  Adding MyTimes is very similar.  We add two objects.  One is the parameter b.  The other is this. What is it?

ITP © Ron Poet Lecture Calling sub MyTime x = new MyTime(10, 30); MyTime y = new MyTime(2,17); MyTime z = x.sub(y);  The parameter b is clearly y.  this is x, the object that is calling sub.  We can’t call it x inside sub because any MyTime object can call sub.  this is another confusing Java word.