1. 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.

    import javax.swing.JOptionPane; public class MadLib { public static void main(String[] args) { String noun1, noun2, adjective, pastVerb; noun1 = JOptionPane.showInputDialog("Enter a noun"); noun2 = JOptionPane.showInputDialog("Enter another noun"); adjective = JOptionPane.showInputDialog("Enter an adjective"); pastVerb = JOptionPane.showInputDialog("Enter a past tense verb"); JOptionPane.showMessageDialog(null,"Mary had a little " + noun1 + "\nIts " + noun2 + " was " + adjective + " as snow" + "\nAnd everywhere that Mary " + pastVerb + "\nThe " + noun1 + " was sure to go"); } }
  2. Write a program that selects a random number between 1 and 5 and asks the user to guess the number. Display a message that indicates the difference between the random number and the user’s guess. Display another message that displays the random number and the Boolean value true or false depending on whether the user’s guess equals the random number. Save the file as RandomGuessMatch.java.

    import javax.swing.JOptionPane; public class RandomGuessMatch { public static void main(String[] args) { String guessString; int random; int guess; final int MIN = 1; final int MAX = 5; boolean isMatch; random = MIN + (int)(Math.random() * MAX); guessString = JOptionPane.showInputDialog(null, "Guess my number between " + MIN + " and " + MAX, "Random guess", JOptionPane.INFORMATION_MESSAGE); guess = Integer.parseInt(guessString); isMatch = guess == random; JOptionPane.showMessageDialog(null, "Your guess was " + (random - guess) + " numbers away from the random number."); JOptionPane.showMessageDialog(null, "The number was " + random + ". The result is " + isMatch + "."); } }