Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fields, Constructors COMP 102 # T2

Similar presentations


Presentation on theme: "Fields, Constructors COMP 102 # T2"— Presentation transcript:

1 Fields, Constructors COMP 102 #14 2016T2

2 Assignments and term test
A4 due after the break, Thursday 10am No labs, lectures, tutorials during the break Term test: 18 August during lecture time 50 minutes Covers materials in lecture 1-11, A1-A4 Variables, arguments parameters If, while, Program with two classes: one call the other Review lecture slides and lecture code Complete the core part of the assignments Do the previous years term test questions 2016T1, 2015T2

3 Menu A program with two classes The second class Data fields
Constructor Methods

4 Define a method Input Output Ask user Parameters From data fields
Print/draw to screen Return a value Update data fields

5 The main class: create new objects
public class NewObjects{ public void createNewObj(){ BankAccount b1 = new BankAccount("Alice"); b1.deposit(100); BankAccount b2 = new BankAccount("Bob"); b2.deposit(200); b1.withdraw(50); b2.statement(); b1.statement(); }

6 The second class with fields, constructor
public class BankAccount { private String name; private double balance; public BankAccount(String n){ this.name = n; this.balance = 0.0; } public void deposit(double x){ this.balance = this.balance + x; } public void withdraw(double x){ this.balance = this.balance - x; } public void statement(){ UI.println(this.name + " $"+this.balance); }

7 A typical class Classes define objects:
Fields: places in an object that store the information associated with the object Constructors: specify how to set up an object when it is first created. Methods: can be called on any object of the class methods can refer to fields of the object they were called on using: this.fieldname

8 Class in the library UI, Math, System, Trace, UIFileChooser
Constants static methods Scanner, PrintStream, File, Data fields Constructor Non-static methods Color, String, JColorChooser, Integer, Double, non-static methods

9 Our programs Most of your assignments: one class 2 classes
Constants Methods 2 classes CartoonStrip, with methods only CartoonFigure Data fields Constructor Non-static methods Typical classes with fields, constructor Animal, Frog, Snake (in A5) Car, Flower, Balloon, StudentMarkRecord (in A5 exercises) BankAccount, (in lecture) Butterfly (in lecture, only give documentation version) In future: Customer, Product, Employee, Student, Tutor, Course, …

10 Program has two classes: the main class
Simple class: - no fields - no constructor - just methods public class CartoonStrip{ public void playStory(){ CartoonFigure cf1 = new CartoonFigure(“green”, 150, 100); cf1.turnLeft(); cf1.turnRight(); cf1.frown() cf1.talk("Is anyone here?"); CartoonFigure cf2 = new CartoonFigure(“blue”, 300, 100); cf2.talk("Hello"); cf2.turnLeft() ; cf1.smile(); cf1.talk("Hi there, I'm Jim"); cf2.talk("I'm Jan"); }

11 CartoonFigure class: wishful methods
public class CartoonFigure { public void talk(String msg) { public void move(double dist) { // draw msg in bubble this.erase(); // wait // change position // erase msg this.draw(); } } public void turnLeft() { public void turnRight() { this.erase(); this.erase(); // change direction // change direction this.draw(); this.draw(); } } public void frown() { public void smile() { this.erase(); this.erase(); // change emotion // change emotion this.draw(); this.draw(); public void erase() { public void draw() { ??? ???

12 CartoonFigure: draw But where are those variables defined?
public void draw() { // work out which image to use (eg, “green/right-smile.png”) // draw the image on the graphics pane // wait a bit } String filename = baseImgNm+“-"+direction+"-"+emotion+".png" ; UI.drawImage(filename, figX, figY, wd, ht); UI.sleep(500); // wait 500 mS But where are those variables defined? Where do they get their values?

13 Remembering state Each CartoonFigure must remember:
its state: position emotion direction the set of image files that it is using. its size Can’t be stored in local variables in a method local variables are “lost” when the method finishes. Have to be stored in the Object itself ⇒ fields

14 Syntax of Field declarations:
public class CartoonFigure { // fields private double figX; // current position of figure private double figY; private String direction = "right"; // current direction it is facing private String emotion = "smiling"; // current emotion private String baseImgNm; // base name of images private double wd = 40; // dimensions of figure private double ht=80; // methods ……. private type field name ; = expression Like variables, BUT (a) NOT inside a method (b) have private in front

15 Setting up an object How do we declare the Fields of an object?
Declared in the class (not inside a method) Must specify the type and the name (just like local variables in methods) Can specify an initial value (but you don’t have to!) otherwise, automatically initialised with 0 or null (unlike local variables) Have a visibility specifier (“private”) Fields remain indefinitely The set of field declarations is a template for the object (just like a method is a template for a worksheet).

16 Setting up an object How do you initialise the values in the fields?
Can specify an initial value in the field declaration but only if every object should start with the same value!!! Must have a way of setting up different objects when you create them: Constructor: specifies what happens when you make a new object (eg, evaluate the expression new CartoonFigure(“green” 150, 100) Like a method but some differences Has parameters that can be used to set up the new object.

17 CartoonFigure class public class CartoonFigure { // fields
private double figX, figY; // current position of figure private String direction = "right"; // current direction it is facing private String emotion = "smile"; // current emotion private String baseImgNm; // folder where images stored private double wd = 40, ht=80; // dimensions // constructor public CartoonFigure(String base, double x, double y){ this.baseImgNm = base; this.figX = x; this.figY = y; this.draw(); } // methods ……. public void turnLeft() { this.erase(); ….. Name of the class, instead of return type and method name

18 Syntax of Constructors
public class name ( type parameter name ) { , statement } public CartoonFigure(String base, double x, double y){ this.baseImgNm = base; this.figX = x; this.figY = y; this.draw(); }

19 Constructors Defining a Constructor Constructor typically
Part of the class Like a method, but called with new Does not have a return type (new always returns an object of the given type) this will hold the new object that is being constructed Constructor typically fills in initial values of fields may set up the user interface, if there is one. may call other methods on the object, can do anything an ordinary method can do.

20 What happens with new ? When an object is created “ smile ” “ right ”
CartoonFigure-24 figX: figY: emotion: direction: baseImgNm : wd: ht: When an object is created eg new CartoonFigure("yellow", 100, 200); New chunk of memory is allocated (new filing card). Reference (ID) to object is constructed CartoonFigure-24 Any initial values specified in the field declarations are assigned to the fields. If no initial value, default values: 0 for fields of a number type (int, double, etc) false for for boolean fields null for fields of an object type (String, Scanner, Car, …) The arguments are passed to the constructor The actions specified in the constructor are performed on the object. The reference is returned as the value of the constructor. 150. 100. “ smile ” “ right ” “ yellow ” null 40. 80.

21 CartoonFigure: fields & constructor
public class CartoonFigure { // fields private double figX; // current position of figure private double figY; private String direction = "right"; // current direction it is facing private String emotion = "smile"; // current emotion private String baseImgNm; // base name of image set private double wd = 40; // dimensions private double ht=80; // constructor public CartoonFigure(String base, double x, double y){ this.baseImgNm = base; this.figX = x; this.figY = y; this.draw(); }

22 CartoonFigure: methods
public void draw() { String filename = this. baseImgNm +“-"+this.direction+"-"+ this.emotion+“.png” ; UI.drawImage(filename, this.figX, this.figY, this.wd, this.ht); UI.sleep(500); } public void erase() { UI.eraseRect(this.figX, this.figY, this.wd, this.ht); public void talk(String msg) { double bubX =… UI.drawOval(bubX, bubY, bubWd, bubHt); UI.drawString(msg, bubX+9, bubY+bubHt/2+3); UI.eraseRect(bubX, bubY, bubWd, bubHt);

23 CartoonFigure: methods
public void turnLeft() { public void turnRight() { this.erase(); this.erase(); this.direction = "left"; this.direction = "right"; this.draw(); this.draw(); } } public void frown() { public void smile() { this.erase(); this.erase(); this.emotion = "frown"; this.emotion = "smile"; this.draw(); this.draw(); } } public void move(double dist) { this.erase(); if ( this.direction.equals(“right”) { this.figX = this.figX + dist ; } else { this.figX = this.figX – dist ;

24 CartoonFigure Objects
Objects need Fields to store values – places inside the object Objects are like filing cards CartoonFigure-24 figX: figY: emotion: direction: baseImgNm: wd: ht: CartoonFigure-27 figX: figY: emotion: direction: baseImgNm: wd: ht: . . . . “ ” “ ” “ ” “ ” “ ” “ ” . . . .

25 Using fields: A method can refer to a field of the object it was called on: this . fieldname eg: public void turnLeft() { this.erase() ; this.direction = “left”; this.draw() ; } public void draw() { String filename = this.baseImgNm + ”/” + this.direction + “-” + this.emotion + “.png” ; UI.drawImage(filename, this.figX, this.figY, this.wd, this.ht); UI.sleep(500); // wait 500 mS no (…) !! Object the method was called on

26 Using fields: CartoonFigure-24 figX: wd: figY: ht: emotion: baseImgNm:
direction: : cf1. turnLeft(); cf1. move(20); public void turnLeft() { this.erase() ; this.direction = “left”; this.draw() ; } Object 150 40 300 80 “smile” “green” “right” “left” cf1: CartoonFigure-24 ID of Object Method this: CartoonFigure-

27 Using fields: CartoonFigure-24 figX: wd: figY: ht: emotion: baseImgNm:
direction: public void draw() { String filename = this. baseImgNm + ”/” + this.direction + “-” + this.emotion + “.png” ; UI.drawImage(filename, this.figX, this.figY, this.wd, this.ht); UI.sleep(500); } Object 150 40 300 80 “smile” “green” “left” Method this: CartoonFigure- “ ”

28 Using fields: CartoonFigure-24 Object figX: wd: figY: ht: 150 40
emotion: baseImgNm: direction: : cfg1. turnLeft(); cfg1. move(20); public void turnLeft() { this.erase() ; this.direction = “left”; this.draw() ; } Object 150 40 300 80 “smile” “green” “left” cfg1: CartoonFigure-24 ID of Object this: CartoonFigure-

29 Using fields: CartoonFigure-24 figX: wd: figY: ht: 150 40
emotion: baseImgNm: direction: cfg1.turnLeft(); cfg1.move(20); : public void move (double dist) { this.erase() ; if ( this.direction.equals(“right”) { this.figX = this.figX + dist ; } else { this.figX = this.figX – dist ; } this.draw() ; } 150 40 300 80 “smile” “green” “left” cfg1: CartoonFigure-24 this: CartoonFigure-


Download ppt "Fields, Constructors COMP 102 # T2"

Similar presentations


Ads by Google