Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

Faculty of Computer Science and Information Technology

SSK 3101 (Computer Programming II)


Semester I 2010/2011

Lab 1 (Individual)

Learning Objective:
 Construct A Simple Object-Oriented Program (P4)

Instructions:
1. Find solution to each problem and get help from your demonstrator whenever you
encounter any problem.
2. Demonstrate your source program and the output to your demonstrator.
3. Submit UML diagram for each problem after the lab session.

Questions:

1. Create a class named Household that includes:


 data fields for the number of occupants and the annual income
 methods named setOccupant(), setIncome(), getOccupants(), and
getIncome() that set and return those values, respectively.
 a constructor that requires no arguments and automatically sets the occupants
field to 1 and the income field to 0.
 an additional overloaded constructor for the Household class that receives an
integer argument and assigns the value to the occupants.
 a third overloaded constructor for the Household class that receives two
arguments, the values of which are assigned to the occupants and income
fields, respectively.

Constructur a program named TestHousehold that demonstrates that each method


works correctly. Add any needed statements to TestHousehold to ensure that the
overloaded constructor works correctly.Print all the data fields in the Household.

2. Create a class named Car that has the following:


 Integer value yearModel that holds the car’s year model,
 field make references a String that holds the make of the car
 the speed field is an integer that holds the car’s current speed.
 Constructor should accept the car’s year model and make as arguments.
These values should be assigned to the object’s yearModel and make fields.
The constructor should also assign 0 to the speed field.
 Appropriate accessor methods that get values stored in an object’s
yearModel, make and speed fields.
 Accelerate method that should add 5 to the speed field each time it is called.
 Brake method that should subtract 5 from the speed field each time it is
called.

Construct a program named TestCar that demonstrates that each methods work.
Calls accelerate() five times and after each call, get the current speed of the car and
display it. Then call brake() five times and display the current speed.

You might also like