Title: Ch 4 Objects and References
1Ch 4 Objects and References
Qi ZhangOct 6
COMP 110 Introduction to Programming
2Questions
- What is the output?
- s1 Hello!
- s2 Hey!
- s1 s2
- s1 Sure.
- s2 Okey Dokey.
- System.out.print(s1)
2
3Questions
- Which one(s) of a,b,c,d is(are) false after
the following statements are executed? - s1 Hello!
- s2 Hey!
- s3 Hey!
- s1 s2
- s1 s2
- s1.equals(s2)
- s2 s3
- s2.equals(s3)
3
4Questions
- True or False
- The return type of constructor is void
- The return type of constructor is the same type
pf the class.
4
5Announcement
- Assignment 3 today
- Lab 5 Fri
5
6Today in COMP 110
- More on Strings, Classes, and Memory
- Objects, references, passing parameters
6
7Strings
- Declaring and initializing strings
- String s1 a string
- String s2 new String(a string)
- What is the difference?
- String s3 new String(s2)
- s3 s2
7
8Defining A Class
- Typical Order in the Java Source File
- data members
- constructor(s)
- other methods
9class Rectangle
class name
data members
methods
10Constructor
public class Rectangle private int
length private int width
public Rectangle (int l, int w) length
l width w
// definition of other classes
11Constructor
Rectangle r2 new Rectangle (5, 10)
public class Rectangle private int
length private int width
public Rectangle (int l, int w) length
l width w
// definition of other classes
12Rectangles in Memory
Rectangle r1 new Rectangle (5, 10)
- Rectangle r2 new Rectangle (20, 30)
3800
r1
10
3800
5
4500
r2
4500
20
30
13Using the Rectangle Class
r
- Create an object
- Rectangle r
- r new Rectangle(2, 3)
- OR
- Rectangle r new Rectangle(2, 3)
- Use the object and the dot operator to access
methods - r.setLength(5)
- r.setWidth(10)
14Using the Rectangle Class
r
2500
- Create an object
- Rectangle r
- r new Rectangle(2, 3)
- OR
- Rectangle r new Rectangle(2, 3)
- Change the values of instance variables using
accessors - r.setLength(5)
- r.setWidth(10)
2500
3
2
15Using the Rectangle Class
r
2500
- Create an object
- Rectangle r
- r new Rectangle(2, 3)
- OR
- Rectangle r new Rectangle(2, 3)
- Change the values of instance variables using
accessors - r.setLength(5)
- r.setWidth(10)
2500
3
2
3
2
10
5
16Calling methods
- int answer math.add(4, 6)
- answer math.add(answer, 7)
- public int add(int a, int b)
-
- return (a b)
16
17Global vs. Local
- Variables are local to methods
- Instance variables are global for all methods in
a class
17
18public int computeArea()
int area width length return
area
public boolean ifBigRectangle()
if(area gt 100) return area
else return false
19Class Parameters
- When a parameter in a method invocation is a
primitive type, the value of the parameter is
copied. - When a parameter in a method invocation is a
reference to a named object, the addess is
copied, not the value of the object. - public void countBigRec(int count, Rectangle
rec) -
- if (rec.ifBigRectangle() true)
- rec.length rec.length/2
- count
-
-
count4 countBigRec(count, rec)
20Class Parameters
2500
50 10
4
count
2500
rec
- public void countBigRec(int count, Rectangle
rec) -
- if (rec.ifBigRectangle() true)
- rec.length rec.length/2
- count
-
count4 countBigRec(count, rec)
21Class Parameters
2500
50 10
4
count
2500
rec
- public void countBigRec(int count, Rectangle
rec) -
- if (rec.ifBigRectangle() true)
- rec.length rec.length/2
- count
-
2500
rec(local)
4
count(local)
count4 countBigRec(count, rec)
22Class Parameters
2500
25 10
4
count
2500
rec
- public void countBigRec(int count, Rectangle
rec) -
- if (rec.ifBigRectangle() true)
- rec.length rec.length/2
- count
-
2500
rec(local)
4
count(local)
count4 countBigRec(count, rec)
23Class Parameters
2500
25 10
4
count
2500
rec
- public void countBigRec(int count, Rectangle
rec) -
- if (rec.ifBigRectangle() true)
- rec.length rec.length/2
- count
-
2500
rec(local)
5
count(local)
count4 countBigRec(count, rec)
24Class Parameters
2500
3500
50 10
5 15
4
count
2500
rec
3500
rec2
- public void countBigRec(int count, Rectangle
rec, Rectangle rec2) -
- if (rec.ifBigRectangle() true)
- rec rec2
- count
-
2500
rec(local)
5
count(local)
3500
rec2(local)
count4 countBigRec(count, rec)
25Class Parameters
2500
3500
50 10
5 15
4
count
2500
rec
3500
rec2
- public void countBigRec(int count, Rectangle
rec, Rectangle rec2) -
- if (rec.ifBigRectangle() true)
- rec rec2
- count
-
3500
rec(local)
5
count(local)
3500
rec2(local)
count4 countBigRec(count, rec)
26Assignment 3
27In-class Exercises-1
- Add a method getName() to Student class
- Test it add s1.getName after s1.name
"Jim" in StudentStats.java, compile and run
StudentStats.java - Add a private data member secondMajor to Student
class - Add a method setSecondMajor to Student class,
with one input parameter (the 2nd major) of type
String - Test it add s1.setSecondMajor(Biology)
after s1.major "Computer Science" in
StudentStats.java. Compile and run
StudentStats.java. - Modily printData() to also output the second
major, compile and run StudentStats.java again.
28In-class Exercises-2
Write the class Rectangle with empty methods