Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program with two classes COMP 102 # T2

Similar presentations


Presentation on theme: "Program with two classes COMP 102 # T2"— Presentation transcript:

1 Program with two classes COMP 102 #15 2016T2

2 Admin Marks for Assign2 are ready
Contact me for any marking mistakes Term test covers material in lecture 1-11, A1-A4 Term test: this Thursday, 11-11:50 me if you can not make it Bring your student ID Allowed: paper copy dictionary, calculator with cleared memory Brief documentation will be handed out Your family name A—K MCLT102 L—Z KKLT301

3 Today’s topic More examples on programs with two classes
1st class, no field, no constructor (the main class) 2nd class, typical class: defining object Fields: Constructors Methods Scope, Extends, visibility Local variable or data field private or public

4 A Program with two classes
Simple class: - no fields - no constructor - just methods public class CartoonStrip{ public void animate(){ 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"); } public static void main(String[ ] args){ CartoonStrip cs = new CartoonStrip(); cs.animate(); Note the main method ⇒ don't need BlueJ

5 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 static final double WD = //class wide constants private static final double HT=80; // belong to class, do not change // constructor public CartoonFigure(String base, double x, double y){ this.baseImgNm = base; this.figX = x; this.figY = y; this.draw(); }

6 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 ;

7 CartoonFigure: methods
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); } public void erase() { UI.eraseRect(this.figX, this.figY, WD, HT); public void draw() { String filename = this. baseImgNm +“-"+this.direction+"-"+ this.emotion+“.png” ; UI.drawImage(filename, this.figX, this.figY, WD, HT);

8 Running the program: main
> java CartoonStrip or call main on the class from BlueJ public static void main(String[ ] args){ CartoonStrip cs = new CartoonStrip(); cs.animate(); cs: CartoonStrip-3 CartoonStrip-3 Very simple object! - no fields - no constructor

9 CartoonStrip Program: playStory
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"); cf1: CartoonFigure-24 CartoonFigure-24 figX: figY: emotion: direction: baseImgNm : 150. 100. “ smile ” “ right ” “ green ” cf2: CartoonFigure- Is anyone here?

10 CartoonStrip Program: playStory
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"); cf1: CartoonFigure-24 cf2: CartoonFigure-27 CartoonFigure-27 figX: figY: emotion: direction: baseImgNm : 300. 100. “ smile ” “ right ” “ blue ” Hello

11 Keeping track of Multiple objects:
CartoonFigure-24 figX: figY: emotion: direction: baseImgNm : 150. 100. “ frown ” “ right ” “ blue ” CartoonFigure-27 figX: figY: emotion: direction: baseImgNm : 300. 100. “ smile ” “ right ” “ blue ” : cf2.turnLeft() ; cf1.smile(); public void turnLeft() { this.erase() ; this.direction = “left”; this.draw() ; } cf1: CartoonFigure-24 cf2: CartoonFigure-27 this: CartoonFigure-

12 Using fields: “ frown ” “ right ” “ blue ” “ smile ” “ left ” “ blue ”
CartoonFigure-24 figX: figY: emotion: direction: baseImgNm : 150. 100. “ frown ” “ right ” “ blue ” CartoonFigure-27 figX: figY: emotion: direction: baseImgNm : 300. 100. “ smile ” “ left ” “ blue ” : cf2.turnLeft() ; cf1.smile(); public void smile() { this.erase() ; this.emotion = “smile”; this.draw() ; } cf1: CartoonFigure-24 cf2: CartoonFigure-27 this: CartoonFigure-

13 Another Object Design Example
SchoolOffice: Record the names and marks of students. Print, enter, modify the records Print “going well” or “needs help” message SchoolOffice Student Fields: Name, Mark Constructor Methods: getting/setting mark and name construct message

14 The Student class Fields Constructors Methods Access the fields
Modify the fields

15 The Student class println will automatically call toString to convert an object into a String class Student { private String name; private double mark; public Student (String nm) { this.name = nm; } public void setName(String n){ this.name = n; } public String getName() { return this.name; public void setMark(double m) { this.mark = m; public double getMark(){ return this.mark ; public String toString (){ return ("Student: " + this.name ", Mark: " + this.mark); } public String diagnostic () { if ( this.mark > 50 ) { return ( “going well" ); else { return ( “needs help" ) ; /* return ( (this.mark>50) ? “going well" : “needs help" ); } */ Shorthand

16 The SchoolOffice class
public class SchoolOffice{ public void demo() { Student s1 = new Student(“Alan”); Student s2 = new Student(“Bob”); s1.setMark(90.5); s2.setMark(40); UI.println(s1.getName()); UI.println(s2.toString()); UI.println(s1); s1.setMark(45.6); UI.println(s1.getName() + " : " + s1.diagnostic()); Student s3 = new Student(“Claire”); UI.println(s3 + s3.diagnostic()); } println automatically calls toString() on objects

17 The StudentOffice class
/** Print out all students from a file, along with message */ public void reportAll(){ String filename = UIFileChooser.open(“Choose student file:"); try { Scanner scan = new Scanner(new File(filename)); while (scan.hasNext()){ Student s = new Student(scan); UI.println(s + " " + s.diagnostic()); } scan.close(); } catch(IOException e){UI.println(“file failure”);}

18 Places: variables and fields
Two kinds of places to store information: Variables (including parameters) defined inside a method specify places on a worksheet temporary – information is lost when worksheet is finished new place created every time method is called. only accessible from inside the method. Fields defined inside a class, but not inside a method specify places in an object (filing card) long term – information lasts as long as the object new place created for each object accessible from all methods in the class, and from constructor.

19 Extent and scope A place with a value must be accessible to some code at some time. Extent: how long it will be accessible local variables (and parameters) in methods have a limited extent ⇒ only until the end of the current invocation of the method fields have indefinite extent ⇒ as long as the object exists Scope: what parts of the code can access it Full scope rules are complicated!!! local variables: accessible only to statements inside the block { … } containing the declaration after the declaration fields: at least visible to the containing class; maybe further.

20 Scope of variables if ( ans.equals("flower") ) {
String ans = “flower”; // or “bud” int x=100; int y = 200; if ( ans.equals("flower") ) { Color center = Color.red; int diam = 30; } else if (ans.equals("bud") ) { Color center = Color.green; int diam = 15; : UI.setColor(center); UI.fillOval(x, y, diam, diam); String ans = “flower”; int x=100; int y = 200; Color center = null; int diam = 0; if ( ans.equals("flower") ) { center = Color.red; diam = 15; } else if (ans.equals("bud") ) { center = Color.blue; diam = 30; : UI,setColor(center); UI.fillOval(x, y, diam, diam); ; ; Out of scope Out of scope may not be intialised may not be intialised

21 Fields: scope, visibility, encapsulation
Fields are accessible to all code in all the instance(non-static) methods in the class. Should they be accessible to methods in other classes? ⇒ visibility: public or private public means that methods in other classes can access the fields cfg1.figX = in the CartoonStrip class would be OK private means that methods in other classes cannot access the fields cfg1.figX = in the CartoonStrip class would be an error. The principle of encapsulation says Keep fields private. Provide methods to access and modify the fields, if necessary ⇒ Text book LDC 5.3

22 Final: variables that don’t vary
If a field will hold a value that should not change (a “constant”): signal it to reader ensure that no code changes it by mistake final is a modifier on field (or variable) declarations means that it can only be assigned to once. public class CartoonFigure { private double figX, figY; private String direction = "right"; private String emotion = "smiling"; private final String baseImgNm; private final double wd = 40 private final double ht = 80; public CartoonFigure(String img, double x, double y){ this.baseImgNm = img // fine – this is the first assignment this.wd = 50; // NO!!! Can't change the previous value

23 Static Final: class wide constants
If a constant (final field) has the same value for every object signal that to reader don’t make every object have its own copy static is a modifier on field declarations means that it belongs to the class as a whole, not to each object public class CartoonFigure { private double figX; private double figY; private String direction = "right"; private String emotion = "smiling"; private final String baseImgNm; private static final double WD = 40 private static final double HT=80; CartoonFigure-24 figX: figY: emotion: direction: baseImgNm: . . “ ” “ ” “ ”


Download ppt "Program with two classes COMP 102 # T2"

Similar presentations


Ads by Google