Using Objects

In this exercise you will write some short programs to practice the use of the Math, Random and String classes.

  1. Write a program Dice.java to simulate a dice roll. Choose two random integers, each from 1 to 6. Print out each of them, and also their sum. The program takes no input. Like this:
    Roll 4 and 6 is 10
    Roll 3 and 1 is 4
    Roll 1 and 6 is 7
    Do not use Math.random(). Use new to create a Random object, and use its nextInt method to generate the random numbers. Make sure you simulate two separate rolls instead of trying to use a single random call. (It matters if you're on Ventnor, and little brother has a hotel on Boardwalk.) Java Random class.
  2. Write a program RandomLet.java to read a line of text, choose a single character at random, remove it from the string, then print the removed character (surrounded by tic marks), its previous position in the string, and the line without it. Like this:
    Enter a line of text: This is a line of text. Removed 's' at position 6. String is now: This i a line of text.
    Enter a line of text: This is a line of text. Removed 'o' at position 15. String is now: This is a line f text.
    Enter a line of text: This is not really a line of text, it's only faking. Removed 'e' at position 30. String is now: This is not really a line of txt, it's only faking.
    Note that the assignment chooses a letter in the string, and removes that single occurrence. If the letter is repeated, only the chosen one is removed. See the first example above, where the program removes the s at position 6, but does not remove the other s at position 3.

    Java String has no method to erase a single character. You will want to form the result by concatenating two substrings: The portion before the deletion, and the portion afterwards.

  3. Write a program BuildingHeight.java to estimate the height of a distant building. Input the distance, and the elevation angle of the line of sight to the top of the building, in degrees. Looks like this:
    How far is the building (meters)? 100 What is the elevation angle of the top? 22.5 The building is 41.4213562373095 meters high.
    How far is the building (meters)? 500 What is the elevation angle of the top? 17.6 The building is 158.60934279317013 meters high.
    How far is the building (meters)? 66.5 What is the elevation angle of the top? 20 The building is 24.204020578702455 meters high.
    If your trig is little rusty, this is a straightforward triangle problem. The height is just the product of the distance and the tangent of the elevation angle. Happily, Java's Math class provides a tangent function that will do the trick. Unfortunately, it wants its argument in radians instead of degrees, but, of course, you just have to multiply the degrees by π over 180. So, this:
    height=distance×tan
    (
    elevation×
    π

    180
    )
    Java's Math object has all the bits you need, including the tangent function and the value of π. All values should be doubles.

When you are finished, turn in your corrected StringOops.java and completed ProcessName.java from the first instruction set, and Dice.java, RandomLet.java and BuildingHeight.java from here.