Sie sind auf Seite 1von 21

Course Supervisor: Dr.

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};

+ main (args : String[]) : void << CONSTRUCTORS>>


+ car( ) //null constructor

// parameterized constructor
+ Car(make:String, kmPerLiter : double,
maxFuelCapacity:int, currentStatus: Status)

//copy Constructor
+ public Car(Car originalCar);
<< set/get functions>>

<< show( )/toString( ) function>>

<< Other functions >>


Open new Project, add new class Car.java and start coding
Write Code for Parameterized and Copy Constructor
inside car.java

Observe the use of


this key word
What is the Rationale
behind writing setters
Write/Insert Code for and getters ???
setters and getters
inside car.java
Now we are trying to code other functions
inside car.java

See nothing is written


inside function bodies
right now! And
compiler has no
objection
Always write Show Method and override toString( )
in your class. We will discuss in class that what is
overriding and why we are writing both ?

Try short cut sout

What is the Rationale


behind writing show
and overriding
toString( ) method ???
Its time to code inside Main.java to drive and test
the code which you written in car.java (client view)
After Completing class Car.java. Do the following to
test your understanding:
1- Write another copy constructor inside car.java without using this key word.

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( );

4- Add variable enum DIRECTION ={LEFT,RIGHT,UP,DOWN} inside car.java and add


suitable function that work with this attribute for e.g. move( )

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

+ main (args : String[]) : void + Scene( )


+ getTicketes( ) : Ticket - createWrestlers():void
+ watchFight( ): void - allocatePotsToWrestler():void
+ viewResults( ) : String - startFighting(Wrestler w1, Wrestler w2);
- startFighting(Wrestler w2);
+ printEventDetails( );
+ toString() : String
SimpleFInput
Wrestler
BufferedReader kb;
String inputLine; - name : String
InputStreamReader - Streak : String
FileInputStreamReader - salary : double Timer
moves: enum
- int : hour
+ <<static>>getInt(): int - int : minute
+ toString() : String
+ <<static>>getFloat(): float - Int : seconds
+ <<static>>getString(): String
+ toString() : String
Wrestler Private Data
Members
- name : String
- streakRecord : int
- salary : double

<<constructors>>
<<set and get >>
<< show( )/ toString( )>>

<< other appropriate


functions to represent
event >>

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 ?

Bonus Work is getting


time from systems
clock and use it in
your program.
EliminationChamber.java
Scene.java
Wrestler.java
1- Modify sequence of events inside main( ) function as per your
knowledge about wwe elimination chamber event.

2- Try to Convert your thinking into code to execute


eliminationChamber.java

OR

If you dont like Wrestling, do the above for your favorite sports / event.

Das könnte Ihnen auch gefallen