170 likes | 291 Views
Introducing Objects. Structure. Objects have two parts: Instance Variables (attributes, adjectives) private int myNumBeepers; private boolean isLightOn; private boolean isSwitchUp; Methods (behaviors, verbs) public int getNumBeepers() public boolean isOn()
E N D
Structure • Objects have two parts: • Instance Variables (attributes, adjectives) private int myNumBeepers; private boolean isLightOn; private boolean isSwitchUp; • Methods (behaviors, verbs) public int getNumBeepers() public boolean isOn() public boolean isUp()
Instance Variables private int myNumBeepers; private boolean isLightOn; private boolean isSwitchUp; • There are three parts to the declaration of a Instance Variables private <dataType> <name> • private – makes the information available only to the object itself. • dataType– the type of information this variable holds. • name – generally dataFields names start with “my”
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.
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 not have argument then the constructor is called the default constructor
Methods • Methods fall into four categories: • Accessors • Predicates • Modifiers • General methods
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.
Predicates • Predicates are a special type of accessor. • Predicates will ALWAYS have a boolean return type. public boolean isOn() { return myLightOn; } public boolean nextToARobot() • Predicates are a special type of accessor. They should NEVER alter the state of the world.
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()
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.
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 public Student(int stuNumber, String account) { // initialize instance variables myNumber = stuNumber; myAccount = account; } public int getStudentNumber() { return myNumber; } public void changeAccount(String newAccount) { myAccount = newAccount; } }
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. • Remember the constructor for objects of class student public Student(int stuNumber, String account) { // initialize instance variables myNumber = stuNumber; // 5555 myAccount = account; // “AX205” } myNumber = 5555 myAccount = “AX205” • Now stu.getStudentNumber() returns5555
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 it mean both Objects look identical? • 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. 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
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. 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
Equal Cars • What does it mean for two Car to be .equals()? • Same year? • Same mileage? • Same value? • ???? • We decide • Requirements: .equals(Object obj) for the Car class public boolean equals(Object obj) { Car temp = (Car) obj; // a java thing if ( // same year e.g., getYear() == temp.getYear() // same price // same mileage // same name e.g., getName().equals(temp.getName()) return true; return ????; }
Equal Cars • Implementing .equals(Object obj) for the Car class public boolean equals(Object obj) { Car temp = (Car) obj; // just a java thing if (getModelYear() == temp.getModelYear() // year && getOrigPrice() == temp.getOrigPrice() // price && getMileage() == temp.getMileage() // mileage && getName().equals( temp.getName()) ) // name return ???; return ????; } • Why == instead of .equals? • The values are primitives (not classes) • Why does getName use .equals? • Name is a String, which is a class.
Assignment Part 1 • From the CarDealer Project, complete the Car class. • You will need to complete the following methods: • public String getName() • public double getOrigPrice() • public int getModelYear() • public int getAge() • public int getMileage() • public int averageMilesPerYear() • public double rateOfDepreciation() • public double getCurrentValue() • public void addMiles(int miles)