Prelab 7: Java Types & Methods


Part 1: Background

Part 2: Minimum.java

The code below is a Java program in applet form for computing the minimum of two integers. You should recognize the minimum subroutine from class. The line numbers are for reference only and do not represent part of the actual code!

1 import java.awt.*; 2 import java.awt.event.*; 3 4 public class Minimum extends java.applet.Applet 5 implements ActionListener 6 { 7 Label label; 8 TextField t1, t2, t3; 9 Button b; 10 11 public void init() 12 { 13 label = new Label("Enter two integers:"); 14 t1 = new TextField(15); 15 t2 = new TextField(15); 16 t3 = new TextField(50); 17 b = new Button("Minimum"); 18 add(label); 19 add(t1); 20 add(t2); 21 add(b); 22 add(t3); 23 b.addActionListener(this); 24 } 25 26 public void actionPerformed(ActionEvent event) 27 { 28 Object cause = event.getSource(); 29 int x = Integer.parseInt(t1.getText()); 30 int y = Integer.parseInt(t2.getText()); 31 32 if(cause == b) 33 { 34 int min = minimum(x,y); 35 t3.setText("The minimum is " + min ); 36 } 37 } 38 39 int minimum(int a, int b) 40 { 41 if(a < b) 42 return a; 43 else 44 return b; 45 } 46 }

Here is what the applet looks like when run from a web page:

  1. What is an applet?
    
    
    

  2. How hard would it be to write a Java applet to compute the sum of two integers instead? Look carefully at the Minimum.java program. Notice that most of the code would be the same. Describe how you could could write a new program "Add.java" to do this by modifying/altering the Java code above. You don't need to actually write the code, a paragraph or list of steps is fine. You can refer to specific lines in the code by number.
    
    
    

  3. Describe how you could write a new program "MinMax.java" that computes both the minimum and the maximum of three integers.
    
    
    

Part 3: Debugging Exercise

To prepare for this part of the assignment, you should perform the following steps on your computer or use a computer in a lab.
  1. Follow these instructions to download Eclipse with the Ambient plugin.

The program you will debug in this section is called AddTimeDebug.java. Debugging this program will be more challenging than the programs from class. AddTimeDebug.java adds two time values using only the Java operators mod (%) and div (/). You should be familiar with mod and div computations from class. Here is a quick overview:

Command Explanation
% Modulus calculates the remainder when dividing two integers.   Example: 190 % 60 = 10
/ In integer division, the remainder is truncated.   Example: 190 / 60 = 3

The input and output for AddTimeDebug.java are as follows:

For this task, and some future assignments, you will be asked to turn in test data that verifies your program works. To create test data for your program, you should think of all the possible different cases your program may encounter. For this program, you should create a file in your editor called TestAddTime.txt that contains test data for the following cases:

You should make a single line in your file for each test case. Each line should contain four numbers: the first time in minutes and seconds followed by the second time in minutes and seconds. There should be a tab separating the two times on a single line. For example, the first line of your file might look like the following:

    0 0     0 0

Once you have thought of values for each test case, save your file TestAddTime.txt. These will be the values with which you test your program. Before continuing, you should try to identify what part of the code corresponds to each test case. You will have to enter the times into your compiled program manually, but this is just to let your grader know what values you had used to make sure your code works.

Now try to compile the code. If it does not compile, check for syntax errors and correct them. Run the code and test for logic errors using your test cases. At this point, you should know which case causes the incorrect output from your program. You should now fix the logic error before continuing. To fix the error, do not rewrite the program, but rather make as few changes as possible to correct the program.

Submitting

Bring your answers and the files TestAddTime.txt and AddTimeDebug.java with you to lab