COMP110
$ave dat Money Fall 2015 Sections 2 & 3 Sitterson 014
November 3rd, 2015 💸
Kris Jordan
[email protected] Sitterson 238
Announcements •
110 Girl Code - Women’s Meetup
Tonight at 7pm in SN115
•
Keep Away Tournament - Online Leaderboard:
http://comp110.com/keepaway
Pig 🐽 Due Thursday If you need help, I *strongly* suggest
office hours today/tomorrow. Thursday will be slammed.
Reading •
Objects feeling uncomfortable? Confusing?
•
Check out sections: •
9.2 - 9.9
•
11.1 - 11.9
Feedback
•
Still a month left for me to get better at this whole teaching thing.
•
Let me know how: http://comp110.com/feedback
Back Above the Machine Last week we took a technical deep dive into the call stack and memory representations of an OOP program. From here on we’re back in Java land with a few exceptional details.
Unit Price is Right
Instance Variables class Person { String costume; int age; double bac;
•
Declared in a class’ body
•
The declarations are saying:
“when you create a Person object, it will contain these variables”
}
These are instance variable declarations.
Objects Person vader = new Person();
1.
2.
Construct a new Person object and return a reference to it.
Assign that new object reference to…
3. A variable named vader.
Instance Methods class Person { String costume; int age; double bac;
•
Declared in a class’ body
•
Don’t have the word static
•
Define the actions/verbs of an object
boolean canLegallyDrink() { return this.age >= 21; } }
This is an instance method.
Calling an Instance Method 5
if(vader.canLegallyDrink()) {
1. Start from our reference to an object
3. Call this instance method on that object 2. Follow the reference to the object
this is a local variable reference
to the object an instance method is called on.
Things to Know •
When a method is called, arguments are copied in as parameters
•
Modifications to primitive parameters (int, double, boolean) do not impact their respective arguments
•
Object parameters are references. Changes to an object from within a method, will remain after returning.
•
When a method call returns, you substitute the method call with its return value.
Things to Know
•
When you call a method on an instance, i.e. vader.says(“Hello”) — there is a local variable called “this” available in that method that references the vader object
Drugs are generic but still work the same.
Unit Price is Right
Unit Price is Right Code Walk
Declare Product’s Instance Variables 1. Open com.comp110.lecture21 / Product.java 2. Add String instance variables brand, unitName 3. Add double instance variables price, units
Done? Check in on PollEverywhere pollev.com/comp110
Declare an Instance Method 1. Define an instance method named unitsPerDollar in the Product class. 2. It should return a double value. 3. It should not have any parameters. 4. Implement its body assuming instance variable ‘price’ is in dollars. Done? Check in on PollEverywhere pollev.com/comp110
Declare an Instance Method 1. Define an instance method named unitsPerDollar in the Product class. 2. It should return a double value. 3. It should not have any parameters. 4. Implement its body assuming instance variable ‘price’ is in dollars. Done? Check in on PollEverywhere pollev.com/comp110
null Person leia; Person vader = null; •
If an object reference doesn’t point to an object it is considered a null reference.
•
If you try and use the dot operator to address a null reference, Java will be upset. What does it even mean?
•
You can check to see if an object reference is null with: vader == null
Implement Game’s decideWinner
instance method. 1. Write a for loop that iterates through each player. •
Caution: # of players is this.playerCount
2. If player[i].guess is less than this.answer, then: 3. If closest == null OR player[i].guess is closer than closest.guess, then: 4. Set the closest player to be player[i]
Done? Check in on PollEverywhere pollev.com/comp110
Instance Variable Scoping
Instance Variable Scoping class Person { String costume; int age; double bac;
•
You don’t have to use this to refer to instance variables
•
Unless a parameter or local variable has the same name.
boolean canLegallyDrink() { return age >= 21; } }
This is ok.
Scoping Priority •
General rule of thumb:
Variable names are accessible only within their surrounding { } block
•
You can access other variables (i.e. another object’s) if you address them via dot operator & references:
otherObj.instanceVar
Constructors
Constructors class Person { String costume; int age; double bac; Person() { this.bac = 0.0; }
•
Special methods that are called when you construct a new object, i.e. new Person();
•
Used to initialize an object’s instance variables.
}
This is a constructor.
Constructors w/ Parameters class Person { String costume; int age; double bac;
•
Constructors can have parameters to require important instance variables to be provided.
•
Simplifies code in common pattern where you construct an object and then assign instance variables:
Person(double bac) { this.bac = bac; }
}
Person a = new Person();
a.bac = 0.0;
Person b = new Person();
b.bac = 0.0;
vs.
Person a = new Person(0.0);
Person b = new Person(0.0);
Multiple Constructors class Person {
•
You can define multiple constructors if there are multiple ways you would like to initialize a new object.
•
Code left allows for either:
String costume; int age; double bac; Person() { this.bac = 0.0; }
Person a = new Person(0.0);
Person b = new Person(0.0);
Person(double bac) { this.bac = bac; } }
or
Person a = new Person();
Person b = new Person();
Method Overloading •
This idea applies to methods, too.
•
You can define multiple methods with the same name if they have different parameters
•
Java figures out the correct method to call
•
println(String string) vs. println(double number)
static