0% found this document useful (0 votes)
9 views

CPE207 Object Oriented Programming (Week 6)

Uploaded by

mertbalaban58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CPE207 Object Oriented Programming (Week 6)

Uploaded by

mertbalaban58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Week 6

Deeper in classes:
this() & toString()

Dr. Nehad Ramaha,


Computer Engineering Department
Karabük Universities These Slides mainly adopted from Assist. Prof. Dr. Ozacar Kasim lecture notes
1
The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
• Class exercises
• Writing the PostOffice class
• Using this() methods
Table of • Using toString() methods
Contents • Garbage Collection and
Method finalize
• Another Exercise
• Rock Scissor Paper Game

CME225 OOP- Week 5 2/10/2023 2


3
this() constructors are used to call (invoke) an alternate constructor of the same class.

4
 If you want to represent any object as a class Student{

string, toString() method comes into


int rollno;
String name;
existence. Student(int rollno, String name){
this.rollno=rollno;
 toString() returns a string representation of this.name=name;
the object. }

 In general, the toString method returns a public String toString(){


string that "textually represents" this }
return rollno+" "+name+" "+city;

object.
public static void main(String args[]){
 If you print any object, java compiler Student s1=new Student(101,“Jack",“Sparrow");
internally invokes the toString() method on Student s2=new Student(102,“Johnny",“Cash");

the object. System.out.println(s1);


 We will mention it again in polymorphism. //compiler writes here s1.toString()
System.out.println(s2);
//compiler writes here s2.toString()
}
}
OUTPUT:

5
 Every object uses system resources, such as memory.
• Wee need to give resources back to the system when they’re no
longer needed; otherwise, “resource leaks” might occur.
(OutOfMemoryErrors)

• In C/C++, programmer is responsible for both creation and destruction of objects.


• [int *ptr; ptr = (int *)malloc(sizeof(int)); *ptr = 25; free(ptr); ]

 But, The JVM performs automatic garbage collection to destroys the objects no longer in
use.
• When there are no more references to an object, the object is
eligible to be collected. Collection typically occurs when the
JVM executes its garbage collector.
• Main objective of Garbage Collector is to free heap memory by
destroying unreachable objects.

6
 Every class in Java has the methods of class Object (package
java.lang), one of which is method finalize.
 finalize allows the garbage collector to perform termination
housekeeping on an object just before reclaiming the object’s
memory.
 You should never use method finalize, because it can cause many
problems and there’s uncertainty as to whether it will ever get called
before a program terminates.

7
8
Rock Scissor Paper Game

 If the pair is player1 is paper and player2 is rock then player1


wins
 If the pair is player1 is scissors and player2 is paper then
player1 wins
 If the pair is player1 is rock and player2 is scissors then
player1 wins
 If Player1 is same as player2, then draw!
 Else player2 wins

9
Rock Scissor Paper Game

10
Rock Scissor Paper Game

Arena Class
RockScissorPaper Class (main class)

11
Rock Scissor Paper Game Gamer Class

2/10/2023 12
Rock Scissor Paper Game
StatMaker Class Gamer 1 Gamer 2

Rock

Rock

Let's make this game. 13


14
 Create a class called Person where, id, firstName,
lastName, and age attributes are declared.
 Create four constructors
◦ In first, set all attributes;
◦ In the second, assign only first and last names,
automatically increment id and age will be set to zero;
◦ In the third, only set id, and set rest to default values;
◦ Int the last only increment id, set the rest to default
values.

15
 a) Create Patient, Hospital(Main) Classes according to the given
UML class diagrams.
 b) In the main method(at the Hospital), create 4 objects from
Patient class and give the name of the patient, the patientId
should be a counter that increased by 1 for each new patient. Put
them all in a array of type Patient called “patients”.
 c) Print all patients information in the main method, using the
slandered print to print the object information formatted at
toString() method.

16

You might also like