Mad Lib

Mad Libs is a children's game in which the players provide a few words that are then incorporated into a silly story. The game helps children understand different parts of speech because they are asked to provide a few words that are then incorporated into a silly story. The game helps children understand different parts of speech because they are asked to provide specific types of words. For example, you might ask a child for a noun, another noun, an adjective, and a past-tense verb. The child might reply with such answers as table, book, silly, studied The newly created Mad Lib might be:

Mary had a little table
Its book was silly as snow
And everywhere that Mary studied
The table was sure to go.

Create a Mad Libs program that asks the user to provide at least four or five words, and then create and display a short story or nursery rhyme that uses them. Save the file as MadLib.java

Use the JOptionPane class to acquire a minimum of six words from the user to include in the Mad Lib.

Ask the user if they would like to play the game again with a Confirm Dialog box using the JOptionPane.YES_NO_OPTION.

If yes, ask the user for new words and redisplay the new Mad Lib using them.

Two Dice

Dice are used in many games. One die can be thrown to randomly show a value from 1 through 6. Design a Die class that can hold an integer data field for a value (from 1 to 6). Include a constructor that randomly assigns a value to a die object. Appendix D contains information on generating random numbers. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statement to generate a random number between 1 and 6 and assign it to a variable. Using this statement assumes you have assigned appropriate values to the static constants.

randomValue = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);

Also include a method in the class to return a dies value. Save the class as Die.java.

Write an application that randomly throws two dice and displays their values. After you read the chapter Making Decisions, you will be able to have the game determine the higher die. For now, just observe how the values change as you execute the program multiple times. Save the application as TwoDice.java.

Five Dice

Using the Die class, write an application that randomly throws five dice for the computer and five dice for the player. Display the values and then, by observing the results, decide who wins based on the following hierarchy of Die values. (The computer will not decide the winner; the player will determine the winner based on observation.) Any higher combination beats a lower one; for example, five of a kind beats four of a kind.

  • Five of a kind
  • Four of a kind
  • Three of a kind
  • A pair

After you learn about decision making in the next chapter, you will be able to make the program determine whether you or the computer had the better roll, and after you read the chapter Arrays, you will be able to make the determination more efficient. For now, just observe how the values change as you execute the program multiple times. Save the application as FiveDice.java.

Enhance the FiveDice class so that it stores the randomly thrown dice in an array of Die objects. Display the results for both the player and computer for eachround.

Pay

Barnhill Fastener Company runs a small factory. The company employs workers who are paid one of three hourly rates depending on skill level:

Skill Level Hourly Pay Rate($)
1 17.00
2 20.00
3 22.00

Each factory worker might work any number of hours per week; any hours over 40 are paid at one and one-half times the usual rate.

In addition, workers in skill levels 2 and 3 can elect the following insurance options:

Option Explanation Weekly Cost to Employee ($)
1 Medical insurance 32.50
2 Dental insurance 20.00
3 Long-term disability insurance 10.00

Also, workers in skill level 3 can elect to participate in the retirement plan at 3% of their gross pay.

Write an interactive Java payroll application that calculates the net pay for a factory worker. The program prompts the user for skill level and hours worked, as well as appropriate insurance and retirement options for the employee's skill level category. The application displays: (1) the hours worked, (2) the hourly pay rate, (3) the regular pay for 40 hours, (4) the overtime pay, (5) the total regular and overtime pay, and (6) the total itemized deductions. If the deductions exceed the gross pay, display an error message; otherwise, calculate and display (7) the net pay after all deduction have been subtracted from the gross. Save the file as Pay.java.

Use three JOptionPane Confirm Dialog Boxes (using the JOptionPane.YES_NO_OPTION) to ask the worker if they want medical insurance, dental insurance, and long-term disability insurance, as the worker can enroll in more than one insurance option.

Use a do...while() loop to ask the user for their skill level. Keep looping until a valid skill level is provided.

Use a JOptionPane to show the workers gross pay.

Random Guess 3

In Chapter 1, you created a class called RandomGuess. In this game, player guess a number, the application generates a random number, and players determine whether they were correct. In chapter 5, you improved the application to display a message indicating whether the players guess was correct, too high, or too low. Now, add a loop that continuously prompts the user for the number, indicating whether the guess is high or low, until the user enters the correct value. After the user correctly guesses the number, display a count of the number of attempts it took. Save the file as RandomGuess3.java.

Enhance the game so the player has to pick a number between 1 and 1,000.

Once the player has figured out the correct number, ask if they would like to play again using the Scanner class. If yes, restart the game with a new random number. If not, use the System.out.println(); method to display a message that thanks the user for playing the game.

Service and Salon Report

a. Table9-6 shows the various services offered by a hair salon, including its prices and times required:

Service Description Price ($) Time (Minutes)
Cut 8.00 15
Shampoo 4.00 10
Manicure 18.00 30
Style 48.00 55
Permanent 18.00 35
Trim 6.00 5

Create a class that holds the service description, price, and number of minutes it takes to perform the service. Include a constructor that requires arguments for all three data fields and three get methods that each return one of the data fields values. Save the class as Service.java.

b. Write an application named SalonReport that contains an array to hold six Service objects, and fill it with the data from Table 9-6. Include methods to sort the array in ascending order by each of the data fields. Prompt the user for the preferred sort order, and display the list of services in the requested order. Save the program as SalonReport.java.

Enhance the program by displaying a menu that asks the user how they want to sort the services menu. 1) Sort by Service Description, 2) Sort by Price, 3) Sort by Time (Minutes), or 0) to Exit. Add a do...while() loop that keeps prompting the user for the next preferred sort order until the user finally chooses 0 to exit.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.