Question
If, if-else, and switch Statements Objective: Create an interactive Java program that reads in user input and outputs a message dependent on user input, demonstrating
If, if-else, and switch Statements
Objective: Create an interactive Java program that reads in user input and outputs a message dependent on user input, demonstrating the student's understanding of conditional statements.
Related SLOs:
SLO #1: Use an appropriate programming environment to design, code, compile, run, and debug computer programs.
SLO #4: Demonstrate working with primitive data types, strings, and arrays.
Instructions:
Using jGRASP, write a Java program named LastnameFirstname04.java, using your last name and your first name, that does the following:
Asks the user if they'd like to hear about one of your favorite books/movies/restaurants/(whatever you want). If the user agrees, continue with the program. If not, let the program end.
Note: the program must end completely. In other words, you should see " ----jGRASP: operation complete."
If the user says they want to hear about one of your favorite things, present the user with a list of at least 3 choices. After the user chooses one item to hear about, print a few sentences about that item.
After you have told the user about the thing they chose, ask the user for feedback on the item, specifying what item they had chosen. (See Example Output) Have different responses depending on what they type.
Note: Your program output does not have to follow the Example Output. The dialogue that your program has with the user can be completely different - be as creative as you wish. :)
Hint: Use nested ifs to control the flow of your program. Think about what you want to happen only if a certain condition is true.
Your program MUST:
Use an if or if-else statement
Use a nested or multi-way statement
Use a switch statement
Consider the possibility of a user not following directions. For example, if you tell them to press 1, 2, or 3, what should your program do if they press 4? As another example, if you tell them to "type yes or no" and they type YES, make sure your program will still accept that as valid input.
Actively use in-line comments stating what each section of code does
Your program must conform to the Java coding standards.
Your program should not use code/concepts we have not yet covered. You must demonstrate that you have mastered the concepts covered in class.
Remember to always begin your code with the following documentation comments:
/**
* Short description of the program.
*
* @author Last Name, First Name
* @assignment ICS 111 Assignment XX
* @date Today's Date
* @bugs Short description of bugs in the program, if any.
*/
Expected Output:
This is an example of what your program should output:
----jGRASP exec: java DoeJohn04
Hi there, what's your name?
Nikki
Hey, John! Do you want to hear about some of my favorite video games at the moment?
Enter yes to continue: yes
Okay! Which game would you like to hear about?
Press 1 to hear about Call of Duty
Press 2 to hear about Final Fantasy XIV
Press 3 to hear about Animal Crossing: New Horizons
3
Animal Crossing: New Horizons is a game for the Nintendo Switch.
In the game, you design an island and make friends with a bunch of cute animals.
You can do different activities like fish, catch bugs, and grow fruit trees.
Compared to a lot of other games, it's pretty relaxing because there isn't a ton of action or
events and you aren't pressured to do a lot of tasks in a short time.
You basically just chill with your animal friends on your little island. :)
So, what do you think?
Would you be interested in checking out Animal Crossing: New Horizons sometime?
yup, sounds great!
Really?! Awesome! Here, you can borrow my Switch and try it out! :)
Goodbye! :)
----jGRASP: operation complete.
This is another example of what your program should output:
----jGRASP exec: java DoeJohn04
Hi there, what's your name?
John
Hey, John! Do you want to hear about some of my favorite video games at the moment?
Enter yes to continue: yes
Okay! Which game would you like to hear about?
Press 1 to hear about Call of Duty
Press 2 to hear about Final Fantasy XIV
Press 3 to hear about Animal Crossing: New Horizons
100
100 ???
Uhh... Sorry, invalid input. You're supposed to type 1, 2, or 3.
If you REALLY want to hear about my 100th favorite video game, message me instead!
Goodbye! :)
----jGRASP: operation complete.
This is another example of what your program should output:
----jGRASP exec: java DoeJohn04
Hi there, what's your name?
John
Hey, John! Do you want to hear about some of my favorite video games at the moment?
Enter yes to continue: YEAAAAAH
Sorry, I needed you to type in "yes"..
Oh, well. We can try again another time!
----jGRASP: operation complete.
Criteria:
-Correct use of ifs to control program flow
-Use of nested or multiway if-else
-Switch
-Providing a different response depending on user's feedback
-Complying with Java coding standards.
--------------------------------------------------------------------------------------------------
Here is a code below that someone on SolutionInn did but it has tons of errors and do not work:
code:
import java.util.*;
// Please change name of the file and Class as per your lastname and firstname
public class LastNameFirstName05 {
// main method starts from here
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); // creating Scanner object to read user input
// printing welcome message and asking for name
System.out.println("Hi there, what's your name?");
String name = scan.nextLine(); // reading name
// printing message
System.out.println("Hey, " + name + "! Do you want to hear some super cool animal facts?");
System.out.println("Enter yes to Continue: "); // asking user to continue or not
String yesString = scan.nextLine(); // reading input
// checking if user enters yes the continue
if (yesString.equalsIgnoreCase("yes")) {
// if user enter yes then printing the messages
System.out.println("Okay! What would you like to hear about?");
System.out.println("Press 1 to hear about sea bass");
System.out.println("Press 2 to hear about atlas moths");
System.out.println("Press 3 to hear about scallops");
// reading user input
int userChoice = scan.nextInt();
// using switch case to print the case
switch (userChoice) {
// if uses enters 1 then priting this case
case 1:
System.out.println("Giant sea bass, also known as black sea bass, is large fish"
+ "that belongs to the wreckfish family. It can be found in"
+ "the northwest Pacific Ocean. Giant sea bass is bottom-dwelling"
+ "creature that inhabits rocky and sandy bottoms on a depth from"
+ "110 to 150 feet.Hope you liked learning about sea bass!");
break;
// if user enters 2 then printing this
case 2:
System.out.println("The atlas moth has no mouth, and they do not eat once they have"
+ "emerged from the cocoon, relying on fat storage for energy."
+ "Every flight takes valuable energy and can take days off their "
+ "already short lives, as it has a very short life span of only one"
+ "to two weeks. They conserve energy by flying as little as possible."
+ "A female will wait for a male to come along and be fertilised,"
+ "lay eggs and die.Hope you liked learning about atlas moths!");
break;
// if user enters 3 thin printing this
case 3:
System.out.println("Ah, the scallop! It just goes to show, you can't keep a good bivalve down."
+ "While most members of its family are known for staying put,"
+ "the scallop is a nimble fellow indeed. When a hungry sea star approaches,"
+ "the scallop can swim away by clap-clap-clapping its shells together!"
+ ""How does a scallop know when a predator approaches?" you ask."
+ "Why, it has 200 eyes to see with!"
+ "It makes one wonder what else the scallop sees with all those eyes..."
+ "I fear it sees into my very soul!Hope you liked learning about scallops!");
break;
// if user enters anything else
default:
// printing error message
System.out.println("Umm... that wasn't one of the choices!");
// printing rating details
System.out.println("How would you rate your experience with this program today?");
System.out.println("1 - 2 - 3 - 4 - 5");
System.out.println("bad ---------- ok --------- BEST EVER");
// taking rating input
int rating = scan.nextInt();
// checking if rating is correct
if (rating >= 1 && rating
// printing the rating using if else
if (rating == 1) {
System.out.print("*");
else if (rating == 2) {
System.out.print("");
else if (rating == 3) {
System.out.print("*");
else if (rating == 4) {
System.out.print("");
else if (rating == 5) {
System.out.print("*");
// printing other message
System.out.println(" Â out of five starAwesome! Thank you so much!");
else {
System.out.println("Umm... that wasn't one of the choices, but thanks anyway!");
else {
// if user doesn't enter yes
System.out.println("Sorry, I needed you to type in "yes"..");
System.out.println("Oh, well. We can try again another time!");
// printing goodbye
System.out.println("Goodbye! :)");
scan.close(); // closing scanner object
Step by Step Solution
3.51 Rating (154 Votes )
There are 3 Steps involved in it
Step: 1
The code provided has some syntax errors and formatting issues Ill provide you with a corrected version of the code Please make sure to replace LastNa...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started