Question

In: Computer Science

Nice shirt! In Java, write a program that uses a function that takes in the user's...

Nice shirt! In Java, write a program that uses a function that takes in the user's name and prints out compliments directed to them. The program should provide a sentinel value by asking if they've had enough compliments and will continue until they type "yes". Include 3 different compliments pulled at random and BE SURE TO USE A FUNCTION/METHOD to take in the user's name.

Sample output:

Enter your name: John Smith

John Smith, you have a great smile!

Would you like another compliment? [yes/no] yes

John Smith, you are tall and handsome!

Would you like another compliment? [yes/no] yes

John Smith, you are a quick learner!

Would you like another compliment? [yes/no] no

Have a nice day!

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;

public class Compliments {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       Random rand = new Random();
       String name = getUserName(scan);

       // Store 3 compliments in an array
       String comp[] = { "you have a great smile!", "you are tall and handsome!", "you are a quick learner!" };
       String sentinel = "yes";

       // Iterate loop till user enters yes
       while (sentinel.equalsIgnoreCase("yes")) {
           // Print the compliment by generating a random number between 0-2
           System.out.println(name + ", " + comp[rand.nextInt(3)]);

           // Ask user if they want another compliment
           System.out.print("Would you like another compliment? [yes/no] ");
           sentinel = scan.nextLine();
       }
       System.out.println("Have a nice day!");
   }

   public static String getUserName(Scanner scan) {
       // Ask user to enter a name and then return name
       System.out.print("Enter your name: ");
       String name = scan.nextLine();
       return name;
   }
}

OUTPUT


Related Solutions

Write a Java program that uses printf, and takes user input to find the name of...
Write a Java program that uses printf, and takes user input to find the name of the File. FileCompare (20) Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input. main Interactively requests the names of the two files, after creating the Scanner for the keyboard. Creates a Scanner for...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write a program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
Create a webpage with HTML and JAVA Script that takes a user's 4 inputs (first name,...
Create a webpage with HTML and JAVA Script that takes a user's 4 inputs (first name, last name, phone number, and sex[using checkbox, must set on load and change whenever the checkbox is selected, only 1 box can be selected, selecting the other automatically deselects the latter]) The first button saves all the inputs into LOCAL STORAGE The Second button retrieves all the data in LOCAL STORAGE and displays them on the same page. Alert box to inform the user...
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT