Starting from:
$30

$24

Static methods Lab 06 Solution

    a. Create a new project Lab06a. Write a Java program that computes the volume of one slice of a birthday cake. The program first inputs two integers from the user, the radius of the cake (r) and the height of the cake (h). The program will compute and output the volume of a slice, based on the number of people (n).

Repeatedly ask the user the number of people and calculate the volume of a slice until the user enters 0.






Your program must include a static method that computes and returns the volume of one slice (double) given the height and radius of the cake and the number of people as parameters.

The volume of a cylinder is calculated using the following formula:






Hint: ​Math class has a constant: Math.PI.

Sample run is shown below:
    b. Create a new project Lab06b. Write a Java program that inputs a string containing only the letters {a, b, c, d}. The program will compute and output the number of substring "bc"s. Then it splits the string from the first occurrence of "bc". If the string doesn't contain any "bc"s, the program prints "The string doesn't contain any substring "bc"s!".

Your program must include two static methods:

        ◦ one that computes and returns number of a given SUB_SEQUENCE in the string passed as parameter.

        ◦ one that splits the string from the first occurrence of the SUB_SEQUENCE into two parts and displays them.

Notes:

    • Define a private global constant string, SUB_SEQUENCE that stores the String "bc".
    • You can only use the following ​String class methods: length(), equals(), charAt(), indexOf(), substring().




Sample runs:

    c. Create a new project Lab06c. Write a Java program that simulates a die rolling game. The program inputs the total money from the user (integer). At each time, user makes a prediction of "even" or "odd" for the face value of the rolled die, and bets some money in that prediction. If the program rolls a die whose category (even or odd) is predicted by the user, he/she wins 50% of his/her bet. If the prediction is wrong, he/she loses 25% of his/her bet. The bet must be smaller than or equal to the current total money. If not, print "Your money is not enough!" and ask the bet from user again. The program terminates when the user enters 0 or the user does not have any more money to bet.

Your program must include a static method that rolls the die, computes and returns the gain according to the face value of the die, given the prediction and the bet as parameters.

Sample run:

More products