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 Scanner class. At the end of the lab, you will write an application 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 3.1 - 3.5 (for objects and methods) and appendix F (for graphics).
Exercise #1: The following program illustrates the use of some of the methods in the String class (see the list on page 79 of the text). Trace the program by hand and show what is printed.
//*********************************************************** // Test several methods for manipulating String objects // //*********************************************************** public class StringManips { //************************************************** // Use methods from the String class to manipulate // String objects. //************************************************** public static void main(String[] args) { String phrase = new String("Learning about Strings"); int phraseLength; int index; String one; String two; String newString; // Use some method from the String class phraseLength = phrase.length(); index = phraseLength / 2; one = phrase.substring(0, index); two = phrase.substring(index, phraseLength); // concatenate newString = two.concat(one); // print information about the phrase System.out.println(); System.out.println("Original phrase: " + phrase); System.out.println("Length of the phrase: " + phraseLength + " characters"); System.out.println("Index: " + index); System.out.println("Character at the index: " + phrase.charAt(index)); System.out.println("New string: " + newString); System.out.println(); } }
Exercise #2: Fill in the blanks in the program below as follows: (Section 3.2, especially the example on page 80, should be helpful):
//******************************************* // Play with String objects // // Author: You //******************************************* public class StringPlay { //************************************** // Use methods in the String class to // create new strings from old. //************************************** 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 strings are "); System.out.println (college); ______________________________ System.out.println (town); ______________________________ System.out.println (change1); ______________________________ System.out.println (change2); ______________________________ System.out.println (change3); ______________________________ } }
Exercise #3: 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 assign the values to side1 and side2 variables and to compute the length of the hypotenuse (you need to use a Math class method for that).
import java.util.Scanner; //************************************************************** // Compute the length of the hypotenuse of a right triangle // given the length of the sides // // Author _________________ //************************************************************** public class RightTriangle { //********************************************************** // Compute the length of the hypotenuse of a right triangle // given the length of the sides, using Math class methods. //********************************************************** public static void main (String[] args) { double side1, side2; // lengths of the sides of a right triangle double hypotenuse; // length of the hypotenuse Scanner scan = new Scanner (System.in); // 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 #4: 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 nextDouble methods, respectively. These are described on page 85 of the text; however, note that nextFloat rather than nextDouble is in the list of methods. There is a nextDouble and it should be used since double is more accurate than float. Most often the goal of a program is to generate a random integer in some particular range, say 30 to 99 (inclusive). There are several ways to do this - here are two.
generator.nextInt(70)returns an integer between 0 and 69 (inclusive). So, the expression
generator.nextInt(70) + 30gives a number between 30 and 99 (inclusive).
generator.nextDouble() * 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.nextDouble() * 70)The result of this is an integer between 0 and 69 inclusive, so
(int) (generator.nextDouble() * 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.
import java.util.Random; //*********************************************** // Generates two random "lucky numbers" // // Author _________________ //*********************************************** public class LuckyNumbers { //******************************************* // Instantiate a Random object and use it to // generates two random "lucky numbers" //******************************************* public static void main(String[] args) { Random generator = new Random(); int lucky1, lucky2; // Generate lucky1 (a random integer in the range 60 to 79) using // the nextInt method lucky1 = ______________________________________________________; // Generate lucky2 (a random integer in the range 31 to 40) // using nextDouble lucky2 = ______________________________________________________; System.out.println("Your lucky numbers are " + lucky1 + " and " + lucky2); } }
Exercise #5: In lab you will write an application that does some simple graphics (these concepts are introduced in Appendix F of the text). Suppose you write an application with a window of width 400 and height 300. Then the Java coordinate system looks as follows (not to scale!):
(0,0) -------------------------------------------- | | | | | | | | | | | | | | | | -------------------------------------------- (400, 300)Label the 2 unlabeled corners with their coordinates and show (approximately) where the point (200, 100) is.