Spring Semester 2005


Lab Two: Values, operators, expressions, variables. Our first programs.
Here is your

A201/A597 LAB ASSIGNMENT TWO

Please work out the answers to the following questions:
  1. Write the following mathematical expressions in Java.




  2. Write the following Java expressions in mathematical notation.
    dm = m * ((Math.sqrt(1 + v / c) / Math.sqrt(1 - v / c)) - 1);
    
    volume = Math.PI * r * r * h;
    
    volume = 4 * Math.PI * Math.pow(r, 3) / 3; 

  3. What is wrong with this version of the quadratic formula?
    x1 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 * a; 
    
    x2 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;

  4. Take a look at page 83 in the text (Soliloquy) and give an example of integer overflow.

    Would the same example work correctly if you used floating-point?

    Give an example of a floating-point roundoff error. Would the same example work correctly if you used integers?

    When using integers, you would of course need to switch to a smaller unit, such as cents instead of dollars or milliliters instead of liters.

  5. Let n be an integer and x a floating-point number. Explain the difference between
    n = (int)x;
    and
    n = (int)Math.round(x);
    For what values of x do they give the same result? For what values of x do they give different results? What happens if x is negative?

  6. Find at least five syntax errors in the following program.
    public class WarmUpSix { 
      public static void main(String[] args) { 
        System.out.print("This program adds two numbers.),
        x = 5; 
        int y = 3.5;
        System.out.print("The sum of " + x + " and " + y " is: "); 
        System.out.println(x + y)
      }
    }

  7. Find at least three logic errors in the following program.
    public class WarmUpSeven { 
      public static void main(String[] args) { 
        ConsoleReader console = new ConsoleReader(System.in);
        int total = 1; 
        System.out.println("Please enter a number:"); 
        int x1 = Integer.parseInt(console.readLine()); 
        total = total + x1; 
        System.out.println("Please enter another number:"); 
        int x2 = Integer.parseInt(console.readLine()); 
        total = total + x1; 
        double average = total / 2;
        System.out.println("The average of two numbers is " 
          + average); 
      }
    }

  8. Explain the difference between 2, 2.0, "2", and "2.0".

  9. Explain what each of the following two program segments computes:
    x = 2; 
    
    y = x + x;
    and
    s = "2";
    
    t = s + s; 

  10. Uninitialized variables can be a serious problem.

    Should you always initialize every variable with zero?

    Explain the advantages and disadvantages of such a strategy.

  11. True or false? (x is an int and s is a String)

    1. Integer.parseInt("" + x) is the same as x

    2. "" + Integer.parseInt(s) is the same as s

    3. s.substring(0, s.length()) is the same as s

  12. Give two ways for converting a number to a string. What is the advantage of each of these ways?

  13. How do you get the first character of a string? The last character?

    How do you remove the first character? The last character?

  14. How do you get the last digit of a number? The first digit?

    That is, if n is 23456, how do you find out 2 and 6?

    Do not convert the number to a string. Hint: %, Math.log

  15. What is a final variable?

    Can you define a final variable without supplying its value?

  16. For the purpose of this exercise, for each of the following cases assume:
    double x = 2.5;
    
    double y = -1.5;
    
    int m = 18;
    
    int n = 4;
    
    String s = "Hello";
    
    String t = "World"; 
    What are the values of the following expressions?

    1. x + n * y - (x + n) * y

    2. m / n + m % n

    3. 5 * x - n / 5

    4. Math.sqrt(Math.sqrt(n))

    5. (int)Math.round(x)

    6. (int)Math.round(x) + (int)Math.round(y)

    7. s + t

    8. s + n

    9. 1 - (1 - (1 - (1 - (1 - n))))

    10. s.substring(1, 3)

    11. s.length() + t.length()

  17. Read the documentation for Math.random().

    Then find a way to produce random integers in the interval [a, b].

    (a and b are given by the user, and a < b).

  18. Simple Programs (pp. 47-55 in the text, Soliloquy) contains an example like this somewhere:
    public class One
    { public static void main(String[] args) 
      { Rectangle a = new Rectangle(5, 10, 20, 30); 
        System.out.println(a); 
        a.translate(15, 25); 
        System.out.println(a); 
      }
    }
    Put this in a file called One.java, compile and run.

    As the notes say, your program won't compile. What's missing?

    Fix the program, compile and run it.

    Your output should look like this:

    frilled.cs.indiana.edu%java One
    java.awt.Rectangle[x=5,y=10,width=20,height=30]
    java.awt.Rectangle[x=20,y=35,width=20,height=30]
    frilled.cs.indiana.edu%
    Did you obtain the same output?

  19. (From the same set of notes) Create a program:
    import java.awt.Rectangle; 
    
    public class Two 
    { public static void main(String[] args) 
      { Rectangle a = new Rectangle(5, 10, 20, 30); 
        Rectangle b = a; 
        a.translate(10, 10);
        b.translate(5, 5); 
        System.out.println(a);
      }
    }
    Place it into a file called Two.java, compile and run it.

    I obtain the following output:

    frilled.cs.indiana.edu%java Two
    java.awt.Rectangle[x=25,y=30,width=20,height=30]
    frilled.cs.indiana.edu%
    What output do you obtain?

    Now suppose that instead of printing a at the end we print b.

    What would the output of the program be then, and why?

    Same question(s) for this program:

    public class Two {
      public static void main(String[] args) {
        int a, b; 
        a = 3; 
        b = a; 
        a = a + 10; 
        b = b + 5; 
        System.out.println(a); 
      } 
    }
  20. What is the output of this program?

    public class Five {
        public static void main(String[] args) {
    	String greeting = "Hello, Bill!"; 
            greeting.toLowerCase(); 
            System.out.println(greeting); 
        } 
    } 
    Please try to deduce the answer first.

    Then run the program to double check your answer.

  21. Modify just one line in the program above to obtain the following output:
    HELLO, BILL!
    You can only modify one line only.

  22. The following program does not compile. Why?
    public class Seven {
        public static void main(String[] args) {
    	int one = 2; 
    	int two = 3; 
            int two = two + one; 
    	System.out.println(two); 
        } 
    } 
  23. The following program does not compile. Why?
    public class Eight {
        public static void main(String[] args) {
    	int n; 
            int m; 
            n = 2; 
            m = m + 2; 
    	System.out.println(m); 
        } 
    } 
  24. In addition please solve the following problem:

    (Giving change) Implement a program that directs a cashier how to give change.

    The program has two inputs:

    • the amount due and
    • the amount received from the customer.
    Compute the difference, and compute the
    • dollars,
    • quarters,
    • dimes,
    • nickels, and
    • pennies
    that the customer should receive in return.

    Hint:

    1. First transform the difference into an integer balance, denominated in pennies.
    2. Then compute the whole dollar amount. Subtract it from the balance. Compute the number of quarters needed.
    3. Repeat for dimes and nickels. Display the remaining pennies.

    Here's a sample run with your program:

    frilled.cs.indiana.edu%java Six
    Type the amount due then press enter.
    3.72
    Type the amount received then press enter.
    5
    Give 1.28 in change as follows: 
       5 quarters
       0 dimes
       0 nickels
       3 cents
    frilled.cs.indiana.edu%java Six
    Type the amount due then press enter.
    0.08
    Type the amount received then press enter.
    
    0.5
    Give 0.42 in change as follows: 
       1 quarters
       1 dimes
       1 nickels
       2 cents
    frilled.cs.indiana.edu%

Next time be ready to discuss the answers to all these questions.


Last updated by Adrian German for A201/A597/I210 on Thu Jan 20 08:16:57 EST 2005