classes and objects cs177 rec 10. announcements project 4 is posted ◦ milestone due on nov. 12....

Post on 18-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Classes and ObjectsClasses and ObjectsCS177 Rec 10

AnnouncementsAnnouncementsProject 4 is posted

◦Milestone due on Nov. 12.◦Final submission due on Nov. 19.

Exam 2 on Nov. 4◦Next Wednesday. 6:30pm

Questions?Questions?

Data Types in JavaData Types in JavaPrimitive Types

◦int◦double◦boolean◦char

Object Types◦String◦Arrays, ex int[]◦You can create them!

Reference vs. Primitive Reference vs. Primitive variablesvariablesVariables of primitive types hold

values◦int x = 25;

Variables of object types hold references◦int [] array = new int[3];

25x

array 0 0 0

Sample class myClockSample class myClockpublic class myClock{

int hour;

int min;

int sec;

}Declare an object of type myClock

myClock clock;

Here, clock holds null. It points to nothing.Create the object

clock = new myClock();clock

0hour

0min

0sec

class

objectclass

Create memory space and assign to clock

X

ExampleExamplemyClock clock1 = new myClock();myClock clock2 = clock1;

The address is copied. clock1.hour and clock2.hour

all refer to the same variable. clock1.hour = 3;

System.out.println(clock2.hour);//prints out 3!

clock1

0hour

0min

0secclock2

““==“ and Comparison==“ and Comparison“==“ compares whatever is in the

variables.

int x = 25;int y = 25;System.out.println(x == y); //prints out “true”

myClock clock1 = new myClock();myClock clock2 = clock1;System.out.println(clock1 == clock2);//The two reference variables point to// the same address. Prints out “true”

25x 25y

clock1

0hour

0min

0secclock2

““==“ and Comparison ==“ and Comparison cont.cont.“==“ compares whatever is in the

variables. myClock clock1 = new myClock();

myClock clock2 = new myClock();System.out.println(clock1 == clock2);// The two reference variables point to different addresses.// Prints out “false” although the two objects have the same content!

clock1

0hour

0min

0sec

clock2

0hour

0min

0sec

““==“ and Comparison ==“ and Comparison cont.cont.Define a method to compare the

contents of two objects.public class myClock{

int hour;

int min;

int sec;

boolean equals(myClock c2){

if ( hour == c2.hour && min == c2.min && sec == c2.sec )

return true;

else return false;

}

}

““==“ and Comparison ==“ and Comparison cont.cont.Now you can compare object

contents using that method. myClock clock1 = new myClock();

myClock clock2 = new myClock();System.out.println(clock1.equals(clock2));// Prints out “true” because the values are the same!

clock1

0hour

0min

0sec

clock2

0hour

0min

0sec

ConstructorsConstructorsA Constructor is invoked when you create an

object using the keyword “new”You can use it to initialize member variables.public class myClock{

public myClock(int h, int m, int s){

hour = h; min = m; sec = s;

}

int hour;

int min;

int sec;

} Now you need to specify the values when you use

“new”myClock clock = new myClock(3, 30, 10);

clock 3hour

30min

10sec

public class myClock{ public myClock(){ } public myClock(int h, int m, int s){ hour = h; min = m; sec = s; } int hour; int min; int sec;}

Constructors cont.Constructors cont.A default constructor is a constructor that

takes zero input arguments. It assigns default values for the variables or

simply does nothing If you do not specify your own constructor,

Java creates a default constructor for you.

default constructor

clock 0hour

0min

0sec

myClock clock = new myClock();

public vs. privatepublic vs. privatePublic members are accessible to the

outside world.Private members are only accessible

inside the class itself.Usually we have public methods and

private variables

public vs. private cont.public vs. private cont.public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; }}

public class rec10{ public static void main(String [] strs){ myClock clock1 = new myClock(); myClock clock2 = new myClock(3, 30, 2); System.out.println(clock1.equals(clock2)); System.out.println(clock1.hour); }}

Compile error!

Accessor methods – get Accessor methods – get valuesvaluespublic class myClock{

private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; }}

Mutator methods – set Mutator methods – set valuesvaluespublic class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ hour = h; min = m; sec = s;} public myClock(){} public void print(){ System.out.println(hour + " " + min + " " + sec); } public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; }}

public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; }

Validity checking is done to ensure that the change is allowable.

public class myClock{ private int hour; private int min; private int sec; public myClock(int h, int m, int s){ setHour(h); setMin(m); setSec(s); } public myClock(){} public void print(){ System.out.println(hour + " " + min + " " + sec); } public boolean equals(myClock c2){ if ( hour == c2.hour && min == c2.min && sec == c2.sec ) return true; else return false; } public int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } public void setHour(int h){ hour = h; } public void setMin(int m){ if ( m >= 0 && m < 60 ) min = m; } public void setSec(int s){ if ( s >= 0 && s < 60 ) sec = s; }}

public myClock(int h, int m, int s){ setHour(h); setMin(m); setSec(s);}

Constructors set up the values, too.You can take advantage of the mutator methods to do that.

Example of How to Use Example of How to Use myClockmyClockpublic class rec10{ public static void main(String [] strs){ myClock clock1 = new myClock(); myClock clock2 = new myClock(3, 30, 2); System.out.println(clock1.equals(clock2)); clock1.setHour(3); clock1.setMin(30); clock1.setSec(2); System.out.println(clock1.equals(clock2)); System.out.println("clock1 shows: " + clock1.getHour() + ":" + clock1.getMin() + ":" + clock1.getSec()); }} The program prints out:

falsetrueclock1 shows: 3:30:2

Questions?Questions?

top related