Programming in Java Lab 1

 

  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.
  2. 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.
  3. Create a Cafe project for the lock classes you will write in this lab. The directory for this project should be called ComboLock.
  4. 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
  5. Your class should have the following methods:
    1. default constructor: set the combination to 1 1 1 and set the flag to closed
    2. constructor that accepts initial values for the combination: set the combination to the values given in the parms and set the flag to closed
    3. 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
    4. close (no parms): change the flag to closed, print a message stating that the lock is closed
    5. openClosed (no parms): print a message stating whether the lock is open or closed
    6. 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
  6. 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:
    1. Create a ComboLock with combination 5 10 15.
    2. Check whether the lock is open.
    3. Print a message stating that the combination used is 1 2 3, and then open the lock using combination 1 2 3.
    4. Print a message stating that the combination used is 5 10 15, and then open the lock using combination 5 10 15.
    5. Close the lock.
    6. Print a message stating that the combination is being changed to 30 40 50, and then change the combination to 30 40 50.
    7. Print a message stating that the combination used is 5 10 15, and then open the lock using combination 5 10 15.
    8. Print a message stating that the combination used is 30 40 50, and then open the lock using combination 30 40 50.
  7. 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:

  1. 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.
  2. 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.
  3. In both of your Java files you need the following statement before the class definition:
             import java.io.PrintWriter;
  4. Make both of your classes public. Make all methods public and all fields private.
  5. 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