Programming in Java Lab 1
- Type in the hello world application that was distributed during class.
Create a Cafe
project for this application in directory A:\HelloWorld. Build and
execute the application
using Cafe.
- Use the Java Development Kit (from the DOS prompt the icon
for the DOS prompt is
called Java Prompt in the lab) to compile and execute the hello
world application.
- Create a Cafe project for the lock classes you will
write in this lab. The
directory for this project should be called ComboLock.
- Write a Java class for combination locks. Use a file named
ComboLock.java in the ComboLock directory on your A: disk. Your class
should have the following fields (all are private, all are type int):
- first number in the combination
- second number in the combination
- third number in the combination
- flag that tells whether the lock is open or closed
- Your class should have the following methods:
- default constructor: set the combination to 1 1 1 and set the
flag to closed
- constructor that accepts initial values for the combination: set
the combination to the values given in the parms and set the flag to
closed
- open (has parms for the combination used to open the lock): if
the parms given for the combination match the combination, set the
flag to open and print a message stating that the lock is open,
otherwise print a message stating that the combination was incorrect
- close (no parms): change the flag to closed, print a message
stating that the lock is closed
- openClosed (no parms): print a message stating whether the lock
is open or closed
- changeCombo (parms for the old combination and parms for the
new combination): if the parms given for the old combination are
correct, change the combination to the values given in the parms
for the new combination, print a message stating whether or not the
combination was changed
- Write a Java class called LockTest . This class will contain a main
function to test the ComboLock class. Use a file named LockTest.java
in the ComboLock directory. Your main function should do the following:
- Create a ComboLock with combination 5 10 15.
- Check whether the lock is open.
- Print a message stating that the combination used is 1 2 3,
and then open the lock using combination 1 2 3.
- Print a message stating that the combination used is 5 10 15, and
then open the lock using combination 5 10 15.
- Close the lock.
- Print a message stating that the combination is being changed to
30 40 50, and then change the combination to 30 40 50.
- Print a message stating that the combination used is 5 10 15,
and then open the lock using combination 5 10 15.
- Print a message stating that the combination used is 30 40 50,
and then open the lock using combination 30 40 50.
- Build and execute your project under Cafe. Then compile and execute
it using the JDK commands. To use the JDK, you need to compile each
file, and then run the interpreter on the test class:
javac ComboLock.java
javac LockTest.java
java LockTest
Note the following:
- It is hard to see your output in Cafe. The output window may disappear
before you have a chance to really see it. This is normal. Running the
program under the JDK will allow you to look at the output for as long as
you like.
- To run your program under the JDK, you must be inside a DOS window.
Use the icon called "Java Prompt" in the lab to get access to the DOS
prompt.
- In both of your Java files you need the following statement before
the class definition:
import java.io.PrintWriter;
- Make both of your classes public. Make all methods public and all
fields private.
- Hand in a listing of each of your classes and a copy of the output.
You can create a copy of the output by running the program under the
JDK as follows:
java LockTest > ComboLock.out
Output from your program will be redirected to the file ComboLock.out,
which you can print.
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page