CSBP219 SP25 Oop LN2
CSBP219 SP25 Oop LN2
Continued
Chapter Objectives
Learn about assignment operators, deep and shallow
copy, copy constructors and how to perform
comparison on objects
Learn the significance of the keywords “this”, and
“static”
Learn about UML Class Diagrams
Objects and Assignment Operator
Clock myClock = new Clock(10,28,45);
Clock yourClock = new Clock(7,27,36);
Objects and Assignment Operator
myClock = yourClock; //shallow copy
//to use it
clock2.makeCopy(clock1);
//this will copy clock1 to clock2
The Copy Constructor
Executes when an object is instantiated
Initialized using an existing object
//In public class Clock
public Clock(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
//to use it
Clock clock3 = new Clock(clock2);
//this will create a new clock (clock3)
//and initialize it with clock2
In Class Exercise – 1
Shallow and Deep Copy
https://onlinegdb.com/VsEo0p6Im
public class TestClock2 { public class TestClock3 {
public static void main(String[] args) public static void main(String[] args)
{ {
1. Clock clock1 = new Clock(); 1. Clock clock1 = new Clock();
2. Clock clock2 = new Clock(11, 12, 15);
3. Clock1 = clock2; 2. Clock clock2 = new Clock(11, 12, 15);
3. Clock1 = new Clock(clock2);
4. //what are the hr,min,sec of clock1?
5. System.out.println(clock1); 4. //what are the hr,min,sec of clock1?
5. System.out.println(clock1);
6. //see the side effect
7. clock1.setHr(5);
6. //see the side effect
8. //what are the values of clock1? 7. clock1.setHr(5);
9. System.out.println(clock1); 8. //what are the values of clock1?
9. System.out.println(clock1);
10. //what are the values of clock2?
10. //what are the values of clock2?
11. System.out.println(clock2);
} 11. System.out.println(clock2);
} }
}
https://onlinegdb.com/nx31Nc9QB
//main
Clock ckTest = new Clock(10, 30, 20);
Sys.. (ckTest.getHr() ) ;
The “this” Keyword
A class can use “this” to access its own members
(variables/methods)
//Class Clock
public int getHr(){ return this.hr;}
public int getMin(){ return this.min;}
public int getSec() { return this.sec;}
Why?
Sometimes just to avoid confusion
this Keyword
public class Foo{
//variables
private int x;
public Foo(int v) // What happens if you remove
{ // the word this
this.set(v); //OP //Will it work?
}
//methods public void set(int x)
public void set(int v) {
{ this.x = x;
this.x = v; //OP }
}
}
This(..) Keyword
this() refers to the constructor
public class MC{
What is the output of the following code?
//variables
public int x;
public class Test{
//methods
public static void main(String[] args){
public MC(){
1. MC mc = new MC(10);
this(5);
2. MC mc2 = new MC();
}
3. System.out.println(mc2.x+”,”+mc.x);
public MC(int v){
4. mc = mc2.inc();
this.x = v;
5. System.out.println(mc2.x+”,”+mc.x);
}
}
public MC inc(){
}
this.x++;
return this;
} Output (1) : Output (2) :
} A) 0, 10 A) 1, 10
B) 5, 10 B) 6, 10
C) 10, 5 C) 5, 11
D) 5, 5 D) 6, 6
Review Questions
non-static method:
can access static members (Variables and Methods).
can access non-static members (Variables and Methods).
cannot be invoked by class name.
can be invoked by object name.
The Modifier static
static members:
can be invoked by class name.
can be invoked by object name.
can be called even without having any objects
Static Methods:
can access only static members (Variables and Methods).
cannot access non-static members (Variables and Methods).
non-static members :
cannot be invoked by class name.
can be invoked by object name.
cannot be called without creating objects
Non-Static Methods:
can access static members (Variables and Methods).
can access non-static members (Variables and Methods).
The Modifier static
numOfClocks
Class Clock
Class Tester{
public static void main(String[] a){
StatEx sObj = new StatEx();
sObj.incY();
sObj.count++;
}}
Static Members of a Class (continued)
1. public class StatEx{
2. private int x;
StatEx sObj1 = new StatEx(3); 3. private static int y;
4. public static int count;
StatEx sObj2 = new StatEx(5);
5. public StatEx(int a){x = a;}
…
sObj1 sObj2
sObj1 sObj2
Question
1. public class Student{
2. private String name;
3. private double gpa;
4. public static int count;
5.
6. public Student(String n){
7. name = n; gpa = 0;
8. count++;
9. }
10. public Student(Student s){
11. this(s.name);
12. }
13. } What is the output?
Student s1 = new Student(“Sami”);
Student s2 = new Student(“Sara”);
Student s3 = new Student(s2);
Student s4 = s1;
System.out.println (“Count =“+ Student.count); //3
System.out.println (“Count =“+ s4.count); //3
Question
What is the output?
Ahmed
Sami
Ahmed
=======================
Kamal
Sami
Kamal
=======================
Kamal
Sami
Sami
=======================
Jamal
Sami
Sami
=======================
Jamal
Jamal
Sami
Unified Modeling Language (UML)
Class Diagrams
26
Unified Modeling Language (UML)
Class Diagrams
27
Exercise - 3
Redesign exercise 2 by:
Implementing ‘this’ keyword
Adding static variable (numOfClocks) to count the
number of clocks created
Test your implementation
https://onlinegdb.com/cIO5aZM8L
In Class Exercise - 4
Implement a Car Class to simulate driving:
• When constructing a Car, you supply the fuel efficiency
(Kilometer per Litre) to indicate consumption of fuel, and
the Tank Capacity to indicate the maximum fuel possible.
• Driving causes petrol level to go down and odometer to
increase.
• The class has methods for:
• add/check petrol level
• check/set the odometer (i.e., total KM driven)
• driving for a number of KM.
• Estimate driving by petrol level.
• When the car runs out of petrol, it stops.
In Class Exercise - 4
First draw a UML diagram for the car class
Then implement the class with the variables and
methods
Finally implement the test class (next slide)
Answer: https://onlinegdb.com/KhHwA-DOk
/**
In Class Exercise - 4
Test the car class
*/
public class Main{
public static void main(String [] args)
{
Car myCar = new Car(10, 70);
// 10 KM per litre , 70 litres capacity
Code: https://onlinegdb.com/u_Z_ihLh-
In Class Exercise - 6
Trace the following program
Code: https://onlinegdb.com/VLzd5Mp1X