Download presentation
Presentation is loading. Please wait.
Published byPhillip Porter Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and Objects Scope and Extent COMP 102 #18 2014T1
2
© Peter Andreae Comp102 18:2 Menu Classes and Objects: LDC Chapter 5 Designing with classes. Scope, Extent, Visibility Admin: Mandatory requirements: submit 8 out of assigs 2-10 ! If you have missed two of assigs 2 – 4: Then you need to do the make-up assignment (on the assignment page)
3
© Peter Andreae Comp102 18:3 Garden plant flowers; make them grow and bloom; pick them Two classes: Garden and Flower Another "visual" object
4
© Peter Andreae Comp102 18:4 Designing Classes: Flower Flower Class: What fields does it need? What methods should it have? What should happen when it is first created?
5
© Peter Andreae Comp102 18:5 Flower: fields & constructor public class Flower { // fields private final double baseX; // position of flower private final double baseY; private double ht = 20; // how high private String stage = "Bud";// "Bud", "Bloom", or "Picked" // constructor public Flower(double x, double y){ this.baseX = x; this.baseY = y; this.draw(); } Promise: the value won’t change after it is first set.
6
© Peter Andreae Comp102 18:6 Flower: methods public void grow(double amt) { if ( ! this.stage.equals("Picked") ) { this.erase(); this.ht = this.ht + amt; this.draw(); } } public void bloom() { if ( this.stage.equals("Bud") ) { this.erase(); this.stage = "Bloom"; this.draw(); } } public void pick() { if ( ! this.stage.equals("Picked") ) { this.erase(); this.stage = "Picked"; this.ht = this.ht / 2; this.draw(); }
7
© Peter Andreae Comp102 18:7 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.
8
© Peter Andreae Comp102 18:8 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.
9
© Peter Andreae Comp102 18:9 Scope of variables while (input.hasNext() ){ String ans = input.next(); 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); : } while (input.hasNext() ){ String ans = input.next(); 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 may not be intialised Out of scope may not be intialised ; ; How do you fix it? different variables!
10
© Peter Andreae Comp102 18:10 Fields: scope, visibility, encapsulation Fields are accessible to all code in all the (ordinary) 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 = 30 in the CartoonStrip class would be OK private means that methods in other classes cannot access the fields cfg1.figX = 30 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 ⇒ LDC 5.3
11
© Peter Andreae Comp102 18:11 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
12
© Peter Andreae Comp102 18:12 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:.. “ ”
13
© Peter Andreae Comp102 18: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 Methods: getting/setting mark and name construct message
14
© Peter Andreae Comp102 18:14 The Student class class Student { private String name; private double mark; public Student (String nm) { this.name = nm; } public Student(Scanner scan){ this.mark = scan.nextDouble(); this.name = scan.nextLine(); } 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 ( m > 50 ) { return ( “going well" ); } else { return ( “needs help" ) ; } } public String diagnostic () { return ( (m>50) ? “going well" : “needs help" ); } Second Constructor: Reads data from a Scanner Shorthand println will automatically call toString to convert an object into a String
15
© Peter Andreae Comp102 18:15 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
16
© Peter Andreae Comp102 18:16 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); System.out.println(s + " " + s.diagnostic()); } scan.close(); } catch(IOException e){….} }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.