In this week's lab you will write Java programs that use the classes String, Random, and Math defined in the Java Standard Class Library in addition to the Keyboard class defined by the authors of the textbook. At the end of the lab, you will write an applet that uses the Graphics and Color classes which are also part of the Java library. The main concepts we will focus on are in the text in sections 2.6 - 2.9 (for objects and methods) and 1.5, 2.10 and 2.111 (for applets and graphics). The goals of lab are for you to gain experience with the following concepts:
String quotation;
String quotation = new String("I think, therefore I am."); Random generator = new Random();
quotation.length()invokes the length method which returns the length of the quotation String or
quotation.toLowerCase()which returns a new String object that is the same as quotation except all letters are lower case. These invocations would be used in a program in a place appropriate for an integer (in the first case) or a String (in the second case) such as an assignment statement or a println statement.
Math.sqrt(2) (which returns the square root of 2) and Keyboard.readInt() (which reads an integer from the Keyboard and returns it)
Exercise #1: Fill in the blanks in the program below as follows:
(Section 2.6, especially the example on page 90, should be helpful):
(a) declare the variable town as a reference to a String
object and initialize it to "Salem, VA".
(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)
// ************************************************** // FILE: StringPlay.java // Author: J. Ingram // Purpose: Play with String objects // ************************************************** public class StringPlay { public static void main (String[] args) { String college = new String ("Roanoke 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); } }
Exercise #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 the sum 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).
// ******************************************************************* // FILE: RightTriangle.java // Purpose: Compute the length of the hypotenuse of a right triangle // given the lengths of the sides // ******************************************************************* import cs1.Keyboard; 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); } {
Exercise #3: In many situations a program needs to generate a random number in a certain range. The Java Random class lets the programmer create objects of type Random and use them to generate a stream of random numbers (one at a time). The following declares the variable generator to be an object of type Random and instantiates it with the new operator.
Random generator = new Random();The generator object can be used to generate either integer or floating point random numbers using either the nextInt() or nextFloat() methods, respectively. The integer returned by nextInt could be any valid integer (positive or negative) whereas the number returned by nextFloat is between 0 and 1 (up to but not including the 1). Most often the goal of a program is to generate a random integer in some particular range, say 30 to 99 (inclusive). There are two ways to do this.
Math.abs(generator.nextInt()) % 70will return numbers in the range 0 to 69 (because those are the only possible remainders when an integer is divided by 70 - note that the absolute value of the integer is first taken using the abs method from the Math class). In general, using % N will give numbers in the range 0 to N - 1. Next the numbers must be shifted to the desired range by adding the appropriate number. So, the expression
Math.abs(generator.nextInt()) % 70 + 30will generate numbers in the range 30 to 99.
generator.nextFloat() * 70returns a floating point number between 0 and 70 (up to but not including 70). To get the integer part of the number we use the cast operator:
(int) (generator.nextFloat() * 70)The result of this is an integer between 0 and 69 inclusive, so
(int) (generator.nextFloat() * 70) + 30shifts the numbers by 30 resulting in numbers between 30 and 99 (inclusive).
Fill in the blanks in the following program to generate the random numbers as described in the documentation. NOTE that that java.util.Random must be imported to use the Random class.
// ************************************************** // FILE: LuckyNumbers.java // Purpose: To generate two random "lucky" numbers // ************************************************** import java.util.Random; public class LuckyNumbers { public static void main (String[] args) { Random generator = new Random(); int lucky1, lucky2; // Generate lucky1 (a random integer between 50 and 79) using the nextInt method lucky1 = ______________________________________________________; // Generate lucky2 (a random integer between 11 and 30) using nextFloat lucky2 = ______________________________________________________; System.out.println ("Your lucky numbers are " + lucky1 + " and " + lucky2); } }
Exercise #4: In lab you will write an applet that does some simple graphics (these concepts are introduced in Sections 1.5, 2.10, and 2.11 of the text). What is the difference between an applet and an application?