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:
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()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)
// ************************************************** // 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); } }
// ******************************************************************* // 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); } {