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

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
© Xiaoying Gao, Peter Andreae UI methods, Variables, Constants COMP 102 #4 2015T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
CIS 3.5 Lecture 2.2 More programming with "Processing"
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Fields, Constructors COMP 102 # T2
Program with two classes COMP 102 # T2
CSC 111 Exam 3 Review Fall 2005.
Presentation transcript:

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

©Xiaoying Gao, Peter Andreae COMP :2 Assignments and term test A5 due after the break, Wed 10am No labs, lectures, tutorials during the break Online help: 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

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

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

© 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); } }

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

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

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

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

©Xiaoying Gao, Peter Andreae COMP :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() {???

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

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

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

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

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

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

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

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

© Xiaoying Gao, Peter Andreae COMP :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: “ smile ” “ right ” “ yellow ” null 0 0

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

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

© Xiaoying Gao, Peter Andreae COMP :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 ; }

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

©Xiaoying Gao, Peter Andreae COMP :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:.... “ ”

©Xiaoying Gao, Peter Andreae COMP :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 ( … ) !!

©Xiaoying Gao, Peter Andreae COMP :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: “ smile ” “ right ” “ green ” cf1: CartoonFigure-24 Method Object ID of Object

©Xiaoying Gao, Peter Andreae COMP :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: “ smile ” “ left ” “ green ” Method Object

©Xiaoying Gao, Peter Andreae COMP :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: “ smile ” “ green ” cfg1: CartoonFigure-24 Object ID of Object

©Xiaoying Gao, Peter Andreae COMP :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: “ smile ” “ left ” “ green ” cfg1: CartoonFigure-24