Lab Summary

Lab #   Topic
Graphics and Conditionals
2   Loops and Objects
3   Animation and Classes
4 Array and Complex Animated Objects
5   Arrays, strings, and file processing (on Moodle)
Project   Write a complete game
The web pages of the lab assignments run Java applets, which may not be supported some web browsers such as Chrome. In this case, please use Firefox, Internet Explorer, or Safari to open the lab web pages. You may also need to add "" to the Exception Site List in the settings of Java. Please see me or the lab instructor for help.

Lab Instructions

Each lab except for the first and the last labs requires two submissions, each due on the specified due date and time. For the first submission, you will hypothesize on the approach for solve the problem by giving the design of your program. The level of details required for the design will be discuss in class. For the second submission, you will submit the final solution to the lab and discuss how your hypotheses have been tested. Before submitting the final solution, you need to demo the program to the lab instructor.

Since each lab requires significant time to read and understand, you should regard every lab as part of your weekly reading assignment. To avoid wasting lab time, read the lab assignment thoroughly before coming to the lab session. The assignment may describe work that you must bring with you to the lab.

To hand in your lab

  1. Go to Lafayette's Moodle site.
  2. Log in using your Lafayette username and password.
  3. Go to the page for the course.
  4. Submit your assignment following the link for each lab in the list.
  5. Only submit the java files or the files specified in the lab description.
  6. The submission must occur before the deadline. The submission is closed after the deadline. Late submission will not be accepted, unless exceptions are given by the instructor.

To enable key presses

To allow the game to react to key presses, import java.awt.event.*, call addKeyListeners(); in the init or the run method, and use the following expression.

public void keyPressed(KeyEvent e){
    // press up botton, rotate
    if(e.getKeyCode() == KeyEvent.VK_UP) {
    // press left botton, shift left
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
		...    }
    // press right button, shift right
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
    // press down button, add speed 
    if(e.getKeyCode() == KeyEvent.VK_DOWN) {

In some cases, the key press does not work on Mac. If this happens, open a terminal window and input the following command:
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false

To publish your handiwork on a web page

These instructions are for the Linux machines in the CS laboratory. First, you need to turn your lab solution into a web applet, as follows:
  1. You want to work in the folder where your files are stored, so log into a Linux machine, and open a terminal window and enter cd ~/CS104/labn where n is the lab number.
  2. You're going to make an archive file ("jar file") containing all your class files. You can call it whatever you want. Assuming it's called "lab1.jar", you would enter
    jar -cf lab1.jar *.class
  3. You need to add the ACM Java library to the jar file too. To do so, copy acm.jar to the current folder and unzip it by typing unzip acm.jar. Then add the ACM Java library to the jar file by typing
    jar uf lab1.jar acm/
    If there are other files needed by your applet (like .au, .jpg, or .gif files) add them to the jar file too. Example:
    jar uf lab1.jar *.au
  4. The jar file is now (if all went well) a self-contained version of your whole program. You can copy it to the folder (directory) containing the web page where you want to show the applet.
Next you need to create your personal web site:
  1. Open a termial, and navigate to your home directory. Once there, type
    mkdir public_html
  2. Then type
    chmod 755 public_html
  3. Then enter this new directory by typing
    cd public_html
    Once inside the directory, download this file to this directory.
  4. Open this file with a text editor, you will see the html code, which should be fairly easy to understand. I'm going to give you the old-fashioned way, but eventually you'll want to learn the W3C-approved tags. For the first lab, the html code for the applet will look like this:
    <applet archive="lab1.jar" code="RobotFace.class" width="600" height="400"></applet>
    Adjust the size of the applet window by changing the width and height attributes.
  5. Now, open a browser and type in this url.
  6. If everything goes smoothly, you should be able to see your program in the web page. Now you are ready to show off your program to your friends and family by sending the above link, and they should be able to run your program in a browser as long as their computer has Java installed. If not, their browser will guide them through the process of installing Java.