CPE207 Object Oriented Programming (Week 6)
CPE207 Object Oriented Programming (Week 6)
Deeper in classes:
this() & toString()
4
If you want to represent any object as a class Student{
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");
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)
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
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
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