Ch 4 Objects and References - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Ch 4 Objects and References

Description:

s2 = 'Hey!' s1 = s2; s1 = 'Sure.' s2 = 'Okey Dokey.' System.out.print(s1); Questions ... Which one(s) of a,b,c,d is(are) false after the following statements ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 29
Provided by: qizh
Category:

less

Transcript and Presenter's Notes

Title: Ch 4 Objects and References


1
Ch 4 Objects and References
Qi ZhangOct 6
COMP 110 Introduction to Programming
2
Questions
  • What is the output?
  • s1 Hello!
  • s2 Hey!
  • s1 s2
  • s1 Sure.
  • s2 Okey Dokey.
  • System.out.print(s1)

2
3
Questions
  • 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
4
Questions
  • True or False
  • The return type of constructor is void
  • The return type of constructor is the same type
    pf the class.

4
5
Announcement
  • Assignment 3 today
  • Lab 5 Fri

5
6
Today in COMP 110
  • More on Strings, Classes, and Memory
  • Objects, references, passing parameters

6
7
Strings
  • 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
8
Defining A Class
  • Typical Order in the Java Source File
  • data members
  • constructor(s)
  • other methods

9
class Rectangle
class name
data members
methods
10
Constructor
public class Rectangle private int
length private int width
public Rectangle (int l, int w) length
l width w
// definition of other classes
11
Constructor
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
12
Rectangles in Memory
Rectangle r1 new Rectangle (5, 10)
  • Rectangle r2 new Rectangle (20, 30)

3800
r1
10
3800
5
4500
r2
4500
20
30
13
Using 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)

14
Using 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
15
Using 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
16
Calling methods
  • int answer math.add(4, 6)
  • answer math.add(answer, 7)
  • public int add(int a, int b)
  • return (a b)

16
17
Global vs. Local
  • Variables are local to methods
  • Instance variables are global for all methods in
    a class

17
18
public int computeArea()
int area width length return
area
public boolean ifBigRectangle()
if(area gt 100) return area
else return false
19
Class 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)
20
Class 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)
21
Class 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)
22
Class 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)
23
Class 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)
24
Class 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)
25
Class 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)
26
Assignment 3
27
In-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.

28
In-class Exercises-2
Write the class Rectangle with empty methods
Write a Comment
User Comments (0)
About PowerShow.com