CS 120 Lab 04

Tom Linton, http://www.cs.moravian.edu/~linton
Moravian College, Spring 1999

Introduction:

All non-trivial programs rely on the ability to use and display data. A solid basis in these foundational program elements will allow us to explore truly object-oriented programs in the near future. Unlike most of the GUI programs we design, this lab creates two which have no "support class", just an application class. We look at various primitive data types, the basic I/O features of a SimpleGUI object and get a feel for "integer arithmetic" in Java.

Working with Data

  1. Copy the following program, saving it in a file called Data.java, and add your names at the top of the file.
  2. Compile and execute the program. What is the exact output of the third line that is printed?
  3. Add the assignment statement num1 = 777; after the declaration of letter. Compile and execute the program to see that the value of num1 is changed when printed. What happened to the original value in num1?
  4. Declare a variable called num4 to be a double precision floating point value (double). Initialize it to the value in num1 (use the variable name, not the numeric value, to do the initialization). Add a line of output that prints the value of num4. Compile and execute the program. What is printed by the new line?
  5. Declare a boolean variable called flag and initialize it to false.  Add a line to print its value.
  6. Add an integer constant called FIXED to the program (declare it to have a visibility of final), initializing it to the value 2020. Have your program print its value. Compile and execute the program to verify the change.
  7. Add an assignment statement after the declarations that assigns the value 3030 to FIXED. Compile the program. What error does the compiler issue? Why?
  8. Make the erroneous assignment statement into a comment by inserting two slashes before it. Add another assignment statement after that line, that assigns the value stored in flag to the variable num1. Compile the program. What error does the compiler issue? Why?
  9. Make this newest assignment statement into a comment. Compile and execute the program to verify that it runs properly once again. Using grade, submit the final version of Data.java.

Performing I/O and Simple Arithmetic:

For the remaining parts of this lab, you will design an application file which creates an instance of a SimpleGUI object and uses its basic I/O commands to explore basic arithmetic in Java. You can base your new file on the basic structure of Data.java, as that file served a similar purpose. Call your new file SimpleMath.java (this mandates that your file defines a class named SimpleMath). The main method of your SimpleMath class should declare two integer variables (with no initial values), two variables of type double (no initial value), and a String variable (no initial value). Assuming that fred is an instance of a SimpleGUI, d1 has been declared as a double, i2 was declared as an int and name was declared as a String, the following are legal calls to various input methods from the SimpleGUI class:

    d1 = fred.getDouble("Any String here: ");
     
    i2 = fred.getInt("Meaningful prompt String: ");
     
    name = fred.getString("What is your name? ");
     
     
  1. For starters, your program should prompt the user for two integers, their name and then, in a nicely formatted manner, display the result of dividing the first integer by the second (don't worry about someone entering zero for the second value). For example, if the user Barney gave 5 and 3 as the integer inputs, your output should look similar to:
     
    Hey Barney, did you know that in Java, 5 / 3 = 1?
     
    To accomplish this, you'll need to concatenate Strings (with + ) and in the example above, the 1 should be designated by i1/i2 (if you called the input integers i1 and i2 ), in your call to displayResult( ). Run your program on a variety of inputs, until you think you can describe how Java does integer division. Print out this version of SimpleMath.java and on the printout (using pen or pencil) describe how you think Java does integer division.
  2. Modify your program from part 10 so that it prompts for, and displays a similar result for division of two variables of type double. Describe the differences between Java's integer division and Java's double division.
  3. Using comments (// or /* ... */), "turn off" the input calls of your program and initialize each of your number variables to have the values 7, 3, 7.0 and 3.0. Using calls to the displayResult( ) method, try to decide what type ( int or double) results from adding an int and a double, multiplying a double by an int, and dividing an int by a double. Describe your findings below, and submit your SimpleMath.java file using grade.