Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.

Similar presentations


Presentation on theme: "David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects."— Presentation transcript:

1 David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP 112 2015T1 Week 3

2 COMP 112 lect 8: 2 © D.Streader Menu Explanation Assignment 1 Challenging! Some tricky computation & Learning Java Assignment 2: FloorCleaner, Tracer, Dodgem Cars Questions about what you are not clear on? Objects

3 COMP 112 lect 8: 3 © D.Streader Objects The world contains many objects for example: Student objects (Physical objects) Car objects(Physical objects) Course objects (Abstract objects) 1.The students are of a different type or class to the courses. 2.Many objects can be of the same type/class. 3.Different classes of object can do different things 4.Different objects can be in different states or have different properties.

4 COMP 112 lect 8: 4 © D.Streader Java Objects A Class defines a type of object What kind of data the objects contain What actions the objects can do / can be done on the objects A Class is a collection of: Private fields to hold data numbers, Strings, booleans, …or other objects Public methods Each object has its own fields that define its state/information The methods defined for a class can be applied to an object of that class and can access/change the state of the object.

5 COMP 112 lect 8: 5 © D.Streader Student Record System If implementing in Java, might have: 1.A Student class a Student object to represent each student. Fields for information about each student Methods to act on students getEmail() …. 2.A Course class a Course object to represent each course Fields for information about each course Methods to act on courses: printCourseList() addStudent(Student s) ….

6 COMP 112 lect 8: 6 © D.Streader Objects in Java File MyObj.java should define the class MyObj MyObj class defines Fields for MyObj objects (hold data) Constructor(s) to specify how to create a new MyObj object. Name of constructor = name of Class Methods called on MyObj objects May have static fields: belong to class as whole, not a field for each object May have main method, executed when Foo run from command line

7 COMP 112 lect 8: 7 © D.Streader Student Class public class Student { // fields private static long nextID = 0; // static field – belongs to class private long ID; private String name; private String email = “unknown”; // constructor public Student(String name){ this.ID = nextID; nextID++; this.name = name; } // methods ……. public void setEmail(String e) { this.email = e; } public String getEmail(String e) { return this.email; } Student-85 ID: name: email:

8 COMP 112 lect 8: 8 © D.Streader More on Student Class Can have multiple constructors Same name, Distinguished by the parameters: // constructors public Student(String name){ this.ID = nextID; nextID++; this.name = name; } public Student(String nm, long id, String e ){ ID = id; name = nm; email = e; } Student s1 = new Student(“Jamie Booker”); Student s2 = new Student(“Lindsay Meyer”, 300123001, “lm9@gmail.com”);

9 COMP 112 lect 8: 9 © D.Streader Car class public class Car{ // fields private String color; public Car(String col){ this.color = col; } public void draw(double x, double y) { if (color.equals(“red”)) { UI.setColor(Color.red);} else if (color.equals(“blue”)) { UI.setColor(Color.blue);} UI.fillRect(x, y, 30, 15); } public boolean equals(Object other) { …. }

10 COMP 112 lect 8: 10 © D.Streader Primitives and Objects int x = 1; x: 1 x names a piec e of memory in which the value 1 is stored Car z = new Car(“red”); z: Car-21 Car-21 col: “red” z names a piec e of memory in which a reference to a car object is stored

11 COMP 112 lect 8: 11 © D.Streader Assignment of object values Car c1 = new Car(“red”); Car c2 = new Car(“red”); Car c3 = new Car(“blue”); Car c4 = c3; c1: Car-21 Car-21 col: “red” c2: Car-24 Car-24 col: “red” c3: Car-25 Car-25 col: “blue” c4: Car-

12 COMP 112 lect 8: 12 © D.Streader Comparing object values Car c1 = new Car(“red”);c1 == c2 ? Car c2 = new Car(“red”);c1.equals(c2) ? Car c3 = new Car(“blue”);c3 == c4 ? Car c4 = c3;c3.equals(c4) ? c1: Car-21 Car-21 col: “red” c2: Car-24 Car-24 col: “red” c3: Car-25 Car-25 col: “blue” c4: Car-

13 COMP 112 lect 8: 13 © D.Streader Passing and modifying values int age = 35; this.change1(age); UI.println(“age: ” + age); public void change1(int v){ v = v + 10; } Student s = new Student(“Roy”); this.change2(s); UI.println(“email: ” + s.getEmail()); public void change2(Student st){ st.setEmail(“jobs@ecs.vuw.ac.nz”); }

14 COMP 112 lect 8: 14 © D.Streader Passing Objects A variable of Object type is a reference (or “pointer”). The value passed when an Object is an argument is the pointer. two is changed

15 COMP 112 lect 8: 15 © D.Streader Car class: equals method (1) public class Car{ private String color; public Car(String col){ this.color = col; } public void draw(double x, double y) { if (color.equals(“red”)) { UI.setColor(Color.red);} else if (color.equals(“blue”)) { UI.setColor(Color.blue);} UI.fillRect(x, y, 30, 15); } public boolean equals(Car other) { return (this.color.equals(other.color)); } What if other object isn’t a car?

16 COMP 112 lect 8: 16 © D.Streader Equals You can call equals on any Object The default operation of equals is to do == public boolean equals(Object other){  in Object class return (this == other); } Many Object types have their own equals: String will check that all the characters in the strings are the same: public boolean equals(String other){  in String class if (this.length() != other.length()) { return false; } for (int i = 0; i < this.length(); i++){ if (this.charAt(i) != other.charAt(i)) { return false; } } return true; }

17 COMP 112 lect 8: 17 © D.Streader Car class: equals method (1) In your class: need to define equals, if it shouldn’t mean == public class Car{ private Color color; private double x, y; : public boolean equals(Car other) { return (this.color.equals(other.color)); } public boolean equals(Object other) {  better! if (this == other) return true; if (! (other instanceof Car) ) return false; Car otherCar = (Car) other; return (this.color.equals(otherCar.color)); } }


Download ppt "David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects."

Similar presentations


Ads by Google