Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
-
Upload
ashley-daniels -
Category
Documents
-
view
217 -
download
0
Transcript of Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
Classes
• Class—definition of a kind of object
• Like an outline or plan for constructing specific objects
• Class specifies what kind of data objects of that class have– Each object has the same data items but can have different
values
• Class specifies what methods each object will have– All objects of the same class have the same methods available to
them
Class as an Outline
Class Name: Automobile
Data:
amount of fuel ________
speed ________
license plate ________
Methods (actions):
increaseSpeed:
How: Press on gas pedal.
stop:
How: Press on brake pedal.
Class Definition
Objects that are instantiations of the class
First Instantiation:
Object name: patsCar
amount of fuel: 10 gallons
speed: 55 miles per hour
license plate: “135 XJK”
Second Instantiation:
Object name: suesCar
amount of fuel: 14 gallons
speed: 0 miles per hour
license plate: “SUES CAR”
Third Instantiation:
Object name: ronsCar
amount of fuel: 2 gallons
speed: 75 miles per hour
license plate: “351 WLF”
Objects
• Variables that are named instances of a class– the class is their type
• Have both data and methods– called members of the object
• Data items are also called fields or instance variables
• Invoking a method means to call the method, i.e. execute the method. Ex:– objectVariableName.method()– objectVariableName is the calling (invoking) object
Containment
• A class contains another class if it instantiates an object of that class– “HAS-A”
• PairOfDice HAS-A Die
PairOfDice dice
Die die1
int upValue
int numFaces
Die die2
int upValue
int numFaces
RollGames main method
Object Variables as Instance Variables for other Objects
Primitive Types vs. Objects (Class Types)
• Primitive variables:– assigned a memory location when declared– variable’s data stored there
• Object variables – assigned a memory location when it is declared– address of where the object data will be is stored there– address starts as null
• assigned when object variable is constructed
new
• Used when constructing an object
• Asks the JVM for a block of memory
• What for?– to store the instance variables of the object
• What does it return?– the address (#) of the memory block
Primitive vs. Class - Example
int sum = 0;
Die die1; MEMORY
die1 null
MEMORY
sum 0
die1.roll();
// CAN'T DO THIS! // die1 hasn’t been constructed
Before object variables are constructed, they store the address null (0)
Syntax or Runtime Error
Primitive vs. Class – Example (cont’d)
die1 = new Die(8,3);
die1.roll();
// Now it’s OK to roll
MEMORY
die1
upValue
numFaces
3
8
etc…
die1 stores an address, which is the address of the beginning of the memory block where the object’s data is
== and Objects
Die die1 = new Die(8,4);
Die die2 = new Die(8,4);
if (die1==die2)
FALSE!
if (die1.equals(die2))
SHOULD BE TRUE!
DEPENDING ON equals DEF.
MEMORY
4
8
4
8
etc…
die1
upValue
numFaces
die2
upValue
numFaces
etc…
.equals and Objects• equals should return true when the two objects have
equivalent state (instance variable) values
// INSIDE Die class
public boolean equals(Die otherDie)
{
return (
(upValue == otherDie.upValue)
&& (this.numFaces==otherDie.numFaces)
);
} this is optional, but what is it?
Call-by-value Revisited
• Note: – method arguments are copies of the original data
• Consequence?– methods cannot assign (‘=’) new values to arguments
and affect the original passed variables
• Why?– changing argument values changes the copy, not the
original
Java Primitives Examplepublic static void main(String[] args)
{
int a = 5;
int b = 5;
changeNums(a, b);
System.out.println(a);
System.out.println(b);
}
public static void changeNums(int x, int y)
{
x = 0;
y = 0;
}
Output?55
Java Objects (Strings) Examplepublic static void main(String[] args)
{
String a = "Hateful";
String b = "Career Opportunities";
changeStrings(a, b);
System.out.println(a);
System.out.println(b);
}
public static void changeString(String x, String y)
{
x = "The Magnificent Seven";
y = "The Magnificent Seven";
}• NOTE: When you pass an object to a method, you are passing a copy of the
object’s address
Output?HatefulCareer Opportunities
How can methods change local variables?
• By assigning returned values
• Ex, in the String class:– substring method returns a new String
String s = "Hello";
s.substring(0, 4);
System.out.println(s);
s = s.substring(0, 4);
System.out.println(s);
Output?HelloHell
Parameter Passing Revisited
• Primitive variables are passed using call-by-value.– a copy of the value is sent to the method
– any change to that value in the method does NOT affect the original primitive variable
• What about with objects?– a copy of the address is sent
– any changes made via mutator methods can change the original object
– Danger: Reconstructing an object parameter for a method does not reconstruct the originally passed variable
swap
a
die
x
y
3
6
1
3
upValue
numSides
• Example Method:
public void swap(int x, Die y){
int temp = x;x = y.getUpValue();y.setUpValue(temp);
}
• Example Method Call:int a = 3;Die die = new Die(6,1);swap(a,die);System.out.print("a is " + a);System.out.print(", die is " + die.getUpValue());
Parameter Passing Revisited (cont’d)
OUTPUT: a is 3, die is 3
x and y are temporary variables, and are stored in different places in memory than a and die
3
1
Changing passed parameterspublic class ParameterPassing {
public static void change(int idNum, String name, StringBuffer surname)
{ idNum = 15; name = "Oscar"; surname.replace(0, 4, "Jobs"); }
public static void main(String[] args) { int id = 0; String fName = "Steve"; StringBuffer lName = new StringBuffer("????"); change(id, fName, lName); System.out.println(fName + " " + lName + " is employee # " + id); }}
OUTPUT: Steve Jobs is employee # 0
What is the output?
These are only temporary variables
What’s happening in memory?
…
chars[0]"O"
chars[4]"r"
… …
"J"
"s"
15
…
change0idNum
surname
name
…
…
0id
lName
fName
…
…
…
MEMORY
main
chars[0]"S"
chars[4]"e"
… …
chars[0]"?"
chars[3]"?"
… …
…
…
…
MEMORY
…
…