These exercises focus on the String, Random, and Math classes defined in the Java Standard Class Library. The main concepts are in the text in sections 3.2-3.5. 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)
and
Math.pow(3, 2) (which returns 32)
// **************************************************
// StringPlay.java
//
// Play with String objects
// **************************************************
public class StringPlay
{
public static void main (String[] args)
{
String college = new String ("PoDunk College");
String town = new String("Anytown, USA"); // part (a)
int stringLength;
String change1, change2, change3;
stringLength = college.length(); // part (b)
System.out.println (college + " contains " + stringLength + " characters.");
change1 = college.toUpperCase(); // part (c)
change2 = change1.replace('O','*'); // part (d)
change3 = college.concat(town); // 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
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): ");
side1 = scan.nextDouble();
side2 = scan.nextDouble();
// Compute the length of the hypotenuse
hypotenuse = Math.sqrt(Math.pow(side1,2) + Math.pow(side2,2));
// Print the result
System.out.println ("Length of the hypotenuse: " + hypotenuse);
}
{
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 between 0 and 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 between 30 and 99.
generator.nextInt(70)will return numbers between 0 and 69 (inclusive). Next the numbers must be shifted to the desired range by adding the appropriate number. So, the expression
generator.nextInt(70) + 30will generate numbers between 30 and 99.
generator.nextFloat() * 70
returns 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, so
(int) (generator.nextFloat() * 70) + 30shifts the numbers by 30 resulting in numbers between 30 and 99.
The method nextFloat can be replaced by nextDouble to get double precision floating point numbers rather than single precision.
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.
// **************************************************
// LuckyNumbers.java
//
// 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, lucky3;
// Generate lucky1 (a random integer between 50 and 79) using the
// nextInt method (no parameter)
lucky1 = Math.abs(generator.nextInt()) % 30 + 50;
// Generate lucky2 (a random integer between 90 and 100) using the
// nextInt method with an integer parameter
lucky2 = generator.nextInt(11) + 90;
// Generate lucky3 (a random integer between 11 and 30) using nextFloat
lucky2 = (int) (generator.nextFloat() * 20) + 11;
System.out.println ("Your lucky numbers are " + lucky1 + ", " + lucky2
+ ", and " + lucky3);
}
}