OOPs Lab 2 Constructors and ToString
OOPs Lab 2 Constructors and ToString
Humera Tariq
Hands on Lab Sessions: Ms. Sanya Yousuf
UML to represent a single object
Practicing Constructors
Practicing Setters and Getters
Writing and testing following functions:
show( )
Lab and Bonus Work
toString( )
Practice Bridging/Linking attribute with
relevant functions
Think about your favorite event
class Wrestler
class Time
Task: Complete and Modify
Car.java.
Read instructions and slides
for Lab completion
Observe UML diagram for single object car
Car
<<ATTRIBUTES >>
- String make;
- double kmPerLitre;
DriveCar - double maxFuelCapacity;
+ enum Status {ENOUGH,FULL,EMPTY};
// parameterized constructor
+ Car(make:String, kmPerLiter : double,
maxFuelCapacity:int, currentStatus: Status)
//copy Constructor
+ public Car(Car originalCar);
<< set/get functions>>
2- Make the enum Status inside car.java to Private and remove the errors that were
introduced in main.java
3- Add variable for attribute speed to car.java and write functions that works on this
attribute. For e.g.
void start( ) {
Test/Run/call
if ( ???? )
this.speed = 20;} each function of
} car.java inside
void stop( ) { if (applybrake( ) == true; main.java
this.speed = 0;
}
void accelerate ( ); void changeGear( );
5- Add some innovative/different attribute to your class car.java and also add
suitable functions that work on that attribute.
UML diagram for the Event Elimination Chamber
scene
EliminationChamber
<<constructors>>
<<set and get >>
<< show( )/ toString( )>>
Two
Versions of
Constructor
Both performs same
job but without
toString
system.out.println
(myObject) not works
Recall what is
Encapsulation
Write code for Time.java and test it inside main( )
1- Time.java
2- Can you write/find code to get Time from systems clock and show it at output ?
OR
If you dont like Wrestling, do the above for your favorite sports / event.