Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.

Similar presentations


Presentation on theme: "Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private."— Presentation transcript:

1 Introducing Objects

2 Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private int myNumBeepers; private boolean isLightOn; private boolean isSwitchUp; Methods (behaviors, verbs) Methods (behaviors, verbs) public int getNumBeepers() public boolean isOn() public boolean isUp()

3 Instance Variables private int myNumBeepers; private boolean isLightOn; private boolean isSwitchUp;  There are three parts to the declaration of a Instance Variables private private private – makes the information available only to the object itself. private – makes the information available only to the object itself. dataType – the type of information this variable holds. dataType – the type of information this variable holds. name – generally dataFields names start with “my” name – generally dataFields names start with “my”

4 Instance Variables  The instance variables, also called “data field” are used to allow an object to “remember” information about its state. (Where I am. How many beepers I am holding. If my switch is up.)  The only way a client should be able to access the data fields is through the methods that the object provides.

5 Constructors  The constructor looks like a method. The constructor has NO return type, and the name must be exactly the same as the name of the class.  The job of the constructor is to correctly and completely create an instance of a class.  The parameter list of the constructor may or may not contain any values. If it does, the constructor should accept those values and assign them into the appropriate instance variable. If it does, the constructor should accept those values and assign them into the appropriate instance variable. If it does not have argument then the constructor is called the default constructor If it does not have argument then the constructor is called the default constructor

6 Methods  Methods fall into four categories: Accessors Accessors Predicates Predicates Modifiers Modifiers General methods General methods

7 Accessors  Accessors allow the client program to “learn” information about the state of the object.  Ex. public int getNumBeepers() { return myNumBeepers; } Since myNumBeepers is private, the client CANNOT directly access it. For the client to “learn” how many beepers the object is holding, it would have to use the accessor method.

8 Predicates  Predicates are a special type of accessor.  Predicates will ALWAYS have a boolean return type. public boolean isOn() { return myLightOn; return myLightOn;} public boolean nextToARobot()  Predicates are a special type of accessor. They should NEVER alter the state of the world.

9 Modifiers  Modifiers are methods that may cause changes to the instance variables.  These methods cause the object to “do” something. public void move() public void moveToWall() public void pickAllBeepersOnCorner()

10 General Methods  These are methods that do a job for us, but do not alter or return any of the instance variables.  Examples: public double distanceTo(Object obj) //from Alice public int numCornersToWall() These have the object “calculate” a value for us, and return it.

11 Sample Class public class Student { // instance variables - replace the example below with your own private int myNumber; private String myAccount; // Constructor for objects of class student // Constructor for objects of class student public Student(int stuNumber, String account) { // initialize instance variables myNumber = stuNumber; myNumber = stuNumber; myAccount = account; myAccount = account;} public int getStudentNumber() {return myNumber;} public void changeAccount(String newAccount) public void changeAccount(String newAccount) { myAccount = newAccount; } { myAccount = newAccount; }}

12 Example  Create a Student Object Student stu = new Student(5555, “AX205”);  What happens? The Student Constructor initializes all the data fields of the Student Object. The Student Constructor initializes all the data fields of the Student Object.  Remember the constructor for objects of class student public Student(int stuNumber, String account) { // initialize instance variables myNumber = stuNumber; // 5555 myNumber = stuNumber; // 5555 myAccount = account; // “AX205” myAccount = account; // “AX205”} myNumber = 5555 myAccount = “AX205”  Now stu.getStudentNumber() returns 5555

13 Equal Objects  What does it mean for two Objects to be equal?  How does == work with Objects. What does it mean? Does being equal mean both Objects are the same Object? Does being equal mean both Objects are the same Object? Does it mean both Objects look identical? Does it mean both Objects look identical? Or does it mean Objects look mostly a like? Or does it mean Objects look mostly a like?  If equal means both Objects are the same Object Use == determine if the Objects are the same. Use == determine if the Objects are the same. For example Robot karel1 = new Robot(1, 1, North, 1); Robot karel2 = new Robot(1, 1, North, 1); Robot karel3 = karel1; karel1 == karel2 returns false // Objects look alike, but are not the same Object karel1 == karel3 returns true // karel1 and karel3 are the Robot

14 Equal Objects  Does it mean both Objects have to look identical?  Or does it mean Objects have to look mostly a like?  If equal means both Objects look a like Use the.equals(Object obj) method to determine if the Objects are the look a like. Use the.equals(Object obj) method to determine if the Objects are the look a like. For example Robot karel1 = new Robot(1, 1, North, 1); Robot karel2 = new Robot(1, 1, North, 1); Robot karel3 = karel1; Karel1.equals(karel2) returns true // Objects look alike Karel1.equals(karel3) returns true // karel1 and karel3 // are the same Robot and look a like // are the same Robot and look a like

15 Equal Cars  What does it mean for two Car to be.equals() ? Same year? Same year? Same mileage? Same mileage? Same value? Same value? ???? ????  We decide  Requirements:.equals(Object obj) for the Car class public boolean equals(Object obj) { Car temp = (Car) obj; // a java thing Car temp = (Car) obj; // a java thing if ( // same year e.g., getYear() == temp.getYear() if ( // same year e.g., getYear() == temp.getYear() // same price // same price // same mileage // same mileage // same name e.g., getName().equals(temp.getName()) // same name e.g., getName().equals(temp.getName()) return true; return true; return ????; return ????;}

16 Equal Cars  Implementing.equals(Object obj) for the Car class public boolean equals(Object obj) { Car temp = (Car) obj; // just a java thing Car temp = (Car) obj; // just a java thing if (getModelYear() == temp.getModelYear() // year if (getModelYear() == temp.getModelYear() // year && getOrigPrice() == temp.getOrigPrice() // price && getOrigPrice() == temp.getOrigPrice() // price && getMileage() == temp.getMileage() // mileage && getMileage() == temp.getMileage() // mileage && getName().equals( temp.getName()) ) // name && getName().equals( temp.getName()) ) // name return ???; return ???; return ????; return ????;}  Why == instead of.equals? The values are primitives (not classes) The values are primitives (not classes)  Why does getName use.equals? Name is a String, which is a class. Name is a String, which is a class.

17 Assignment Part 1  From the CarDealer Project, complete the Car class. You will need to complete the following methods: You will need to complete the following methods: public String getName() public String getName() public double getOrigPrice() public double getOrigPrice() public int getModelYear() public int getModelYear() public int getAge() public int getAge() public int getMileage() public int getMileage() public int averageMilesPerYear() public int averageMilesPerYear() public double rateOfDepreciation() public double rateOfDepreciation() public double getCurrentValue() public double getCurrentValue() public void addMiles(int miles) public void addMiles(int miles)


Download ppt "Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private."

Similar presentations


Ads by Google