Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.

Similar presentations


Presentation on theme: "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors."— Presentation transcript:

1 Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors COMP 102 #16 2015T2

2 ©Xiaoying Gao, Peter Andreae COMP 102 16:2 Assignments and term test A5 due after the break, Wed 10am No labs, lectures, tutorials during the break Online help: comp102-help@ecs.vuw.ac.nzcomp102-help@ecs.vuw.ac.nz Forum Term test: 11 Sep Friday during lecture time Review lecture slides and lecture code Complete the core part of the assignments Do the previous years term test questions 2015T1, 2014T2

3 ©Xiaoying Gao, Peter Andreae COMP 102 16:3 Menu A typical class Data fields Constructor Methods

4 ©Xiaoying Gao, Peter Andreae COMP 102 16:4 Define a method Input Ask user Parameters Files From data fields Output Print/draw to screen Return a value Files Update data fields

5 © Xiaoying Gao, Peter Andreae COMP102 8:5 A typical 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); } }

6 © Xiaoying Gao, Peter Andreae COMP 102 17:6 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

7 ©Xiaoying Gao, Peter Andreae COMP 102 16:7 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, Constants Data fields static methods non-static methods

8 ©Xiaoying Gao, Peter Andreae COMP 102 16:8 Our programs Most of your assignments: one class Constants Methods 2 classes PetShow, with methods only Animal Data fields Constructor Non-static methods Typical classes with fields, constructor Animal (in A3) Car, Flower, Balloon, StudentMarkRecord (in A3 exercises) BankAccount, (in lecture) Butterfly (in lecture, only give documentation version) In future: Customer, Product, Employee, Student, Tutor, Course,

9 ©Xiaoying Gao, Peter Andreae COMP 102 16:9 CartoonFigure: methods public class CartoonFigure { public void talk(String msg) {public void move(double dist) { // draw msg in bubble // erase figure // wait// change position // erase msg// redraw figure } public void turnLeft() {public void turnRight() {// erase figure // change direction // redraw figure} public void frown() {public void smile() { // erase figure // change emotion // redraw figure }

10 ©Xiaoying Gao, Peter Andreae COMP 102 16:10 CartoonFigure: 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 msgthis.draw(); } public void turnLeft() {public void turnRight() { this.erase();this.erase(); // change direction this.draw();this.draw();} public void frown() {public void smile() { this.erase(); // change emotion this.draw(); } public void erase() {public void draw() {???

11 ©Xiaoying Gao, Peter Andreae COMP 102 16:11 CartoonFigure: draw public void draw() { // work out which image to use (eg, “green/right-smile.png”) // draw the image on the graphics pane // wait a bit } public void draw() { 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?

12 ©Xiaoying Gao, Peter Andreae COMP 102 16:12 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

13 © Xiaoying Gao, Peter Andreae COMP 102 16:13 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 ……. field name expression = ;private type Like variables, BUT (a) NOT inside a method (b) have private in front

14 © Xiaoying Gao, Peter Andreae COMP 102 17:14 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 (unlike local variables) The set of field declarations is a template for the object (just like a method is a template for a worksheet).

15 © Xiaoying Gao, Peter Andreae COMP 102 17:15 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.

16 © Xiaoying Gao, Peter Andreae COMP 102 17:16 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

17 © Xiaoying Gao, Peter Andreae COMP 102 17:17 public class nametype (){ statement } parameter name, public CartoonFigure(String base, double x, double y){ this.baseImgNm = base; this.figX = x; this.figY = y; this.draw(); } Syntax of Constructors

18 © Xiaoying Gao, Peter Andreae COMP 102 17:18 Defining a Constructor 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. Constructors

19 © Xiaoying Gao, Peter Andreae COMP 102 17:19 What happens with new ? 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. CartoonFigure-24 figX: figY: emotion: direction: baseImgNm : wd: ht: 150. 100. 40. 80. “ smile ” “ right ” “ yellow ” null 0 0

20 © Xiaoying Gao, Peter Andreae COMP 102 17:20 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(); }

21 © Xiaoying Gao, Peter Andreae COMP 102 17:21 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.sleep(500); UI.eraseRect(bubX, bubY, bubWd, bubHt); }

22 © Xiaoying Gao, Peter Andreae COMP 102 17:22 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.emotion = "frown"; this.emotion = "smile"; 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 ; }

23 © Xiaoying Gao, Peter Andreae COMP 102 17:23 The whole Program has another class 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"); } Simple class: - no fields - no constructor - just methods

24 ©Xiaoying Gao, Peter Andreae COMP 102 16: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 ©Xiaoying Gao, Peter Andreae COMP 102 16: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 } Object the method was called on no ( … ) !!

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

27 ©Xiaoying Gao, Peter Andreae COMP 102 16:27 Using fields: 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); } this: CartoonFigure- “ ” CartoonFigure-24 figX: wd: figY: ht: emotion:baseImgNm: direction: 150 300 40 80 “ smile ” “ left ” “ green ” Method Object

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

29 ©Xiaoying Gao, Peter Andreae COMP 102 16:29 Using fields: 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() ; } this: CartoonFigure- CartoonFigure-24 figX: wd: figY: ht: emotion:baseImgNm: direction: 150 300 40 80 “ smile ” “ left ” “ green ” cfg1: CartoonFigure-24


Download ppt "Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors."

Similar presentations


Ads by Google