Program with two classes COMP 102 # T2

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Access to Names Namespaces, Scopes, Access privileges.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
COMP More About Classes Yi Hong May 22, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Create Objects,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
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 More Event-driven Input TextFields,
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 Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
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 Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
A High Flying Overview CS139 – Fall 2010 How far we have come.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
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
Defining Your Own Classes II
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Values vs. References Lecture 13.
Static data members Constructors and Destructors
Object Oriented Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Classes.
Namespaces, Scopes, Access privileges
Advanced Programming in Java
CS Week 14 Jim Williams, PhD.
CS 302 Week 11 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
CS1316: Representing Structure and Behavior
Topics Introduction to File Input and Output
CS1316: Representing Structure and Behavior
Sit next to someone. Today, we do some work in pairs.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Recap Week 2 and 3.
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Topics Introduction to File Input and Output
Corresponds with Chapter 5
Building Java Programs
Presentation transcript:

Program with two classes COMP 102 #15 2016T2

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 Email 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

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

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

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 = 40 //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(); }

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 ;

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

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

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?

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

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-

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-

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

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

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

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

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

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.

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.

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

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 = 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 ⇒ Text book LDC 5.3

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

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: . . “ ” “ ” “ ”