Download presentation
Presentation is loading. Please wait.
Published byAllen Turner Modified over 9 years ago
1
Classes and Objects CS177 Rec 10
2
Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦ Next Wednesday. 6:30pm
3
Questions?
4
Data Types in Java Primitive Types ◦ int ◦ double ◦ boolean ◦ char Object Types ◦ String ◦ Arrays, ex int[] ◦ You can create them!
5
Reference vs. Primitive variables Variables of primitive types hold values ◦ int x = 25; Variables of object types hold references ◦ int [] array = new int[3]; 25 x array 0 0 0 0 0 0
6
Sample class myClock public 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 0 0 hour 0 0 min 0 0 sec class object class Create memory space and assign to clock X
7
Example myClock 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 0 0 hour 0 0 min 0 0 sec clock2
8
“==“ 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” 25 x y clock1 0 0 hour 0 0 min 0 0 sec clock2
9
“==“ and Comparison 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 0 0 hour 0 0 min 0 0 sec clock2 0 0 hour 0 0 min 0 0 sec
10
“==“ and Comparison 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; }
11
“==“ and Comparison 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 0 0 hour 0 0 min 0 0 sec clock2 0 0 hour 0 0 min 0 0 sec
12
Constructors A 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 3 3 hour 30 min 10 sec
13
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. 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 0 0 hour 0 0 min 0 0 sec myClock clock = new myClock();
14
public vs. private Public members are accessible to the outside world. Private members are only accessible inside the class itself. Usually we have public methods and private variables
15
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!
16
Accessor methods – get values 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 int getHour(){ return hour; } public int getMin(){ return min; } public int getSec(){ return sec; } }
17
Mutator methods – set values 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 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.
18
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.
19
Example of How to Use myClock 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)); 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: false true clock1 shows: 3:30:2
20
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.