CS 1054 Introduction to Programming in Java (Spring 2008)
Lab 5: Introduction to Loops and Console Input
Lab Objectives
- Write a complete class that uses decision statements
in its methods to control program flow.
- Understand the use of the Scanner class for console input.
- Understand the use of while loops in program execution.
Instructions
For this lab,
you will write a class from scratch and you will complete the implementation
of another class
that uses a loop and carries out console input.
The classes are called CellPhone and CellPhoneConsole.
Both classes are due together by the end of your lab section for the week of February 24th.
CellPhone will be submitted to web-cat and CellPhoneConsole will be
demonstrated to and handgraded by a TA.
Begin by downloading
Lab5.zip,
save the file, and then extract it in a folder you can locate.
Open this project in BlueJ.
- Check out the CellPhone and CellPhoneConsole classes.
The CellPhone class itself is empty.
This means that the CellPhoneConsole class (which has been provided
almost complete for your convenience)
cannot be compiled until the methods of the CellPhone class have been sufficiently defined.
- Define the CellPhone class.
A cell phone represented by this class is a prepaid phone
and supports very minimal functions.
The cell phone can make a call, send a text message,
add minutes to your cell phone and report how many minutes you have
left as well as the number of texts and number of calls you have made.
When a call is made you will need to pass a parameter specifying the
duration of the call in minutes.
A text will always be 1 minute worth of call time.
Adding minutes will require a parameter indicating the number of
minutes you are adding.
You will need to implement the following methods for a CellPhone object:
- public void addMinutes(int minutesToAdd): Add minutes to your cell phone.
- public void sendText(): Send one text message, make sure you have at least
1 minute left.
- public void makeCall(int duration): Make a phone call that lasts the duration of the parameter passed to
the method. Make sure you have enough minutes to make the call.
- public int getMinutes(): Return the number of
minutes left on your cell phone.
- public int getTextCount(): Return the
number of texts made from this cell phone.
- public int getCallCount(): Return
the number of calls made from this cell phone.
For a complete description of these methods, please visit this
Java documentation page .
- Compile both classes and then execute CellPhoneConsole.
(CellPhoneConsole is a tester program that creates a CellPhone object
and uses the Scanner class and a loop to facilitate interactive input.
Much of this program has been written for you.)
- Everything should compile, but the text message functionality of the
CellPhoneConsole is not yet included in this class, which brings us to your next task.
- Read through the CellPhoneConsole class.
Take note of the while loop: It will keep the program running until the command "quit"
is typed and the boolean stop variable is set to true.
- Notice that the text message aspect of the CellPhoneConsole has not been implemented.
Implement this so that the sendText() and getTextCount()
methods are called on the CellPhone object.
- Don't forget to comply with the formatting guidelines and include
your Javadoc-formatted method comments, particularly for the CellPhone class.
Submission
- This lab will only require the CellPhone.java file to be submitted to
web-cat
- Demonstrate the CellPhoneConsole class to your TA.