Upload
tyler-bradley
View
218
Download
0
Embed Size (px)
Citation preview
Introducing ObjectsIntroducing Objects
StructureStructure
Objects have two parts:Objects have two parts: Instance Variables (attributes, adjectives)Instance Variables (attributes, adjectives)
private int myNumBeepers;private int myNumBeepers;
private boolean isLightOn;private boolean isLightOn;
private boolean isSwitchUp;private boolean isSwitchUp; Methods (behaviors, verbs)Methods (behaviors, verbs)
public int getNumBeepers()public int getNumBeepers()
public boolean isOn()public boolean isOn()
public boolean isUp()public boolean isUp()
Instance VariablesInstance Variables
private int myNumBeepers;private int myNumBeepers;
private boolean isLightOn;private boolean isLightOn;
private boolean isSwitchUp;private boolean isSwitchUp;
There are three parts to the declaration of a There are three parts to the declaration of a Instance Variables Instance Variables
private <private <dataTypedataType> <> <namename>> private private – makes the information available only to the – makes the information available only to the
object itself.object itself. dataTypedataType – the type of information this variable holds.– the type of information this variable holds. namename – generally dataFields names start with “my” – generally dataFields names start with “my”
Instance VariablesInstance Variables
The instance variables, also called “data The instance variables, also called “data field” are used to allow an object to field” are used to allow an object to “remember” information about its state. “remember” information about its state. (Where I am. How many beepers I am (Where I am. How many beepers I am holding. If my switch is up.)holding. If my switch is up.)
The only way a client should be able to The only way a client should be able to access the data fields is through the access the data fields is through the methods that the object provides.methods that the object provides.
ConstructorsConstructors
The constructor looks like a method. The The constructor looks like a method. The constructor has NO return type, and the name constructor has NO return type, and the name must be exactly the same as the name of the must be exactly the same as the name of the class.class.
The job of the constructor is to correctly and The job of the constructor is to correctly and completelycompletely create an instance of a class. create an instance of a class.
The parameter list of the constructor may or may The parameter list of the constructor may or may not contain any values.not contain any values. If it does, the constructor should accept those values If it does, the constructor should accept those values
and assign them into the appropriate instance variable. and assign them into the appropriate instance variable. If it does not have argument then the constructor is If it does not have argument then the constructor is
called the default constructorcalled the default constructor
MethodsMethods
Methods fall into four categories:Methods fall into four categories:
Accessors Accessors PredicatesPredicates ModifiersModifiers General methodsGeneral methods
AccessorsAccessors Accessors allow the client program to “learn” Accessors allow the client program to “learn”
information about the state of the object.information about the state of the object. Ex.Ex.
public int getNumBeepers()public int getNumBeepers(){{
return myNumBeepers;return myNumBeepers;}}Since myNumBeepers is private, the client CANNOT Since myNumBeepers is private, the client CANNOT
directly access it. For the client to “learn” how many directly access it. For the client to “learn” how many beepers the object is holding, it would have to use the beepers the object is holding, it would have to use the accessor method.accessor method.
PredicatesPredicates
Predicates are a special type of accessor. Predicates are a special type of accessor. Predicates will ALWAYS have a boolean return type.Predicates will ALWAYS have a boolean return type.public boolean isOn()public boolean isOn(){{ return myLightOn;return myLightOn;}}
public boolean nextToARobot()public boolean nextToARobot()
Predicates are a special type of accessor. They should Predicates are a special type of accessor. They should NEVER alter the state of the world.NEVER alter the state of the world.
ModifiersModifiers
Modifiers are methods that may cause Modifiers are methods that may cause changes to the instance variables.changes to the instance variables.
These methods cause the object to “do” These methods cause the object to “do” something.something.public void move()public void move()
public void moveToWall()public void moveToWall()
public void pickAllBeepersOnCorner()public void pickAllBeepersOnCorner()
General MethodsGeneral Methods
These are methods that do a job for us, but These are methods that do a job for us, but do not alter or return any of the instance do not alter or return any of the instance variables.variables.
Examples:Examples:public double distanceTo(Object obj) //from Alicepublic double distanceTo(Object obj) //from Alice
public int numCornersToWall()public int numCornersToWall()
These have the object “calculate” a value for us, These have the object “calculate” a value for us, and return it.and return it.
Sample ClassSample Classpublic class Studentpublic class Student{{
// instance variables - replace the example below with your own// instance variables - replace the example below with your ownprivate int myNumber;private int myNumber;private String myAccount;private String myAccount;
// Constructor for objects of class student// Constructor for objects of class studentpublic Student(int stuNumber, String account)public Student(int stuNumber, String account){{
// initialize instance variables// initialize instance variables myNumber = stuNumber;myNumber = stuNumber; myAccount = account;myAccount = account;}}
public int getStudentNumber()public int getStudentNumber(){{ return myNumber;return myNumber; }}
public void changeAccount(String newAccount)public void changeAccount(String newAccount) { myAccount = newAccount; }{ myAccount = newAccount; }}}
ExampleExample Create aCreate a Student Student ObjectObject
Student stu = new Student(5555, “AX205”);Student stu = new Student(5555, “AX205”);
What happens?What happens? The The StudentStudent Constructor initializes all the data fields of the Constructor initializes all the data fields of the StudentStudent Object. Object.
Remember the constructor for objects of class Remember the constructor for objects of class studentstudentpublic Student(int stuNumber, String account)public Student(int stuNumber, String account){{
// initialize instance variables// initialize instance variables myNumber = stuNumber; myNumber = stuNumber; // 5555// 5555 myAccount = account; myAccount = account; // “AX205”// “AX205”}}
myNumber = myNumber = 55555555myAccount = myAccount = “AX205”“AX205”
Now stu.getStudentNumber() Now stu.getStudentNumber() returnsreturns 55555555
Equal ObjectsEqual Objects What does it mean for two Objects to be equal?What does it mean for two Objects to be equal? How does How does ==== work with Objects. What does it mean? 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 ObjectIf equal means both Objects are the same Object Use Use ==== determine if the Objects are the same. determine if the Objects are the same.
For exampleFor exampleRobot karel1 = new Robot(1, 1, North, 1);Robot karel1 = new Robot(1, 1, North, 1);Robot karel2 = new Robot(1, 1, North, 1);Robot karel2 = new Robot(1, 1, North, 1);Robot karel3 = karel1;Robot karel3 = karel1;
karel1 == karel2 karel1 == karel2 returns returns false false // Objects look alike, but are not the same Object// Objects look alike, but are not the same Objectkarel1 == karel3 karel1 == karel3 returns returns true true // // karel1 karel1 and and karel3 karel3 are the Robotare the Robot
Equal ObjectsEqual Objects Does it mean both Objects have to look identical?Does it mean both Objects have to look identical? Or does it mean Objects have to look mostly a like?Or does it mean Objects have to look mostly a like?
If equal means both Objects look a likeIf equal means both Objects look a like Use the Use the .equals(Object obj).equals(Object obj) method to determine if the Objects are method to determine if the Objects are
the look a like.the look a like.
For exampleFor exampleRobot karel1 = new Robot(1, 1, North, 1);Robot karel1 = new Robot(1, 1, North, 1);Robot karel2 = new Robot(1, 1, North, 1);Robot karel2 = new Robot(1, 1, North, 1);Robot karel3 = karel1;Robot karel3 = karel1;
Karel1.equals(karel2) Karel1.equals(karel2) returns returns true true // Objects look alike// Objects look alikeKarel1.equals(karel3) Karel1.equals(karel3) returns returns true true // // karel1 karel1 and and karel3karel3 // // are the same Robot and look a likeare the same Robot and look a like
Equal CarsEqual Cars What does it mean for two Car to be What does it mean for two Car to be .equals().equals()??
Same year?Same year? Same mileage?Same mileage? Same value?Same value? ????????
We decideWe decide
Requirements: Requirements: .equals(Object obj).equals(Object obj) for the for the CarCar class class
public boolean equals(Object obj) {public boolean equals(Object obj) { Car temp = (Car) obj; // a java thingCar 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 ????;} }
Equal CarsEqual Cars Implementing Implementing .equals(Object obj).equals(Object obj) for the for the CarCar class class
public boolean equals(Object obj) {public boolean equals(Object obj) { Car temp = (Car) obj; // just a java thingCar temp = (Car) obj; // just a java thing if (getModelYear() == temp.getModelYear() // yearif (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?Why == instead of .equals? The values are primitives (not classes)The values are primitives (not classes)
Why does getName use .equals?Why does getName use .equals? Name is a String, which is a class.Name is a String, which is a class.
Assignment Part 1Assignment Part 1
From the CarDealer Project, complete the Car From the CarDealer Project, complete the Car class.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)