Java Reference
In-Depth Information
Lab 5.2: Using Constructors
The purpose of this lab is to become familiar with working with construc-
tors. You will add two constructors to your Elevator class.
1. Begin by opening your Elevator class from Lab 5.1 in your text editor.
2. Add a constructor that has a single parameter of type int to repre-
sent the current floor of the elevator. Within the constructor, assign
the parameter to the appropriate field in your class. Also, display a
message using System.out.println() that shows which constructor
you are currently in.
3. Add a no-argument constructor that uses the this keyword to invoke
your constructor in the previous step, passing in a 1 for the initial floor.
Display a message that states you are currently in the no-argument
constructor.
4. Modify your ElevatorProgram in Lab 5.1. Instantiate one of the
Elevator objects using the no-argument constructor, and instantiate
the other Elevator object using the one-argument constructor.
5. Run the ElevatorProgram and ensure that the constructors are work-
ing successfully.
Lab 5.3: Redesigning the Powerball Lottery
In this lab, you will make your Powerball program object oriented by
writing a class called Powerball.
1. Write a class named Powerball that contains six fields to represent
the five white balls and one red ball.
2. Add a method named play() that simulates the playing of Powerball.
(See Lab 3.5 for details of the lottery.) This method should assign a
value to each of the six fields.
3. Add a method named displayResults() that prints out the values of
the five white balls and one red ball.
4. Save and compile your Powerball class.
5. Write a class named PlayLottery that contains main(). Within main(),
instantiate a Powerball object and invoke the methods of the Power-
ball class to ensure that they are working successfully.
Search WWH ::




Custom Search