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

CPE207 Object Oriented Programming (Week 6)

Here is the Person class with 4 constructors as specified: class Person { int id; String firstName; String lastName; int age; Person(int id, String firstName, String lastName, int age) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; } Person(String firstName, String lastName) { this.id = id++; this.firstName = firstName; this.lastName = lastName; this.age = 0; } Person(int id) { this.id = id; this.firstName =

Uploaded by

arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

CPE207 Object Oriented Programming (Week 6)

Here is the Person class with 4 constructors as specified: class Person { int id; String firstName; String lastName; int age; Person(int id, String firstName, String lastName, int age) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; } Person(String firstName, String lastName) { this.id = id++; this.firstName = firstName; this.lastName = lastName; this.age = 0; } Person(int id) { this.id = id; this.firstName =

Uploaded by

arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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 11/7/2021 2


3
this() constr uctor s are used to call (invoke) an alter nate constr uctor of the same class.

4
 If you want to represent any object as a class Student{
int rollno;
string, toString() method comes into 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(){


return rollno+" "+name+" "+city;
string that "textually represents" this }
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

11/7/2021 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

You might also like