Prelab Exercises
Sections 2.5-2.7

To hand in: You should print this page. Modify the pages as indicated by the instructions, and hand in the modified pages. Make sure your name is clearly indicated.

These exercises focus on the String, Random, Scanner, and Math classes defined in the Java Standard Class Library. The main concepts are in the text in sections 2.5 - 2.7 (for objects and methods) and 1.6, 2.9 and 2.10 (for applets and graphics). The goals of the lab are for you to gain experience with the following concepts:

Exercises

  1. Fill in the blanks in the program as follows: (Section 2.5, especially the example in Listing 2.8, should be helpful):
    (a) declare the variable town as a reference to a String object and initialize it to "Anytown, USA".
    (b) write an assignment statement that invokes the length method of the string class to find the length of the college String object and assigns the result to the stringLength variable
    (c) complete the assignment statement so that change1 contains the same characters as college but all in upper case
    (d) complete the assignment statement so that change2 is the same as change1 except all capital O's are replaced with the asterisk (*) character.
    (e) complete the assignment statement so that change3 is the concatenation of college and town (use the concat method of the String class rather than the + operator)
    // **************************************************
    //   StringPlay.java
    //
    //   Play with String objects
    // **************************************************
    public class StringPlay
    {
       public static void main (String[] args)
       {
          String college = new String ("PoDunk College");
    
          ________________________________________________________; // part (a)
    
          int stringLength;
          String change1, change2, change3; 
    
          ________________________________________________________; // part (b)
    
          System.out.println (college + " contains " + stringLength + " characters.");
    
          change1 = ______________________________________________; // part (c)
    
          change2 = ______________________________________________; // part (d)
    
          change3 = ______________________________________________; // part (e)
    
          System.out.println ("The final string is " + change3);
        }
    }
    

  2. The following program should read in the lengths of two sides of a right triangle and compute the length of the hypotenuse (recall that the length of the hypotenuse is the square root of side 1 squared plus side 2 squared). Complete it by adding statements to read the input from the keyboard and to compute the length of the hypotenuse (you need to use a Math class method for that).
    // *******************************************************************
    //   RightTriangle.java
    //
    //   Compute the length of the hypotenuse of a right triangle
    //   given the lengths of the sides     
    // *******************************************************************
    import java.util.Scanner;
    
    public class RightTriangle
    {
       public static void main (String[] args)
       {
          double side1, side2;  // lengths of the sides of a right triangle
          double hypotenuse;    // length of the hypotenuse
    
          // Get the lengths of the sides as input
          System.out.print ("Please enter the lengths of the two sides of " +
                              "a right triangle (separate by a blank space): ");
          
          _____________________________________________________________;
    
          _____________________________________________________________;
    
          // Compute the length of the hypotenuse
    
          _____________________________________________________________;
    
          // Print the result
          System.out.println ("Length of the hypotenuse: " + hypotenuse);
        }
    {