Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here's a code for the question below which is incorrect and giving me 8 errors: import java.util.Scanner; import java.util.Random; public class WilliamsStanley05 { public static

Here's a code for the question below which is incorrect and giving me 8 errors:

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

public class WilliamsStanley05 {

public static void main(String[] args) {
// Initialize scanner for user input
Scanner scanner = new Scanner(System.in);

// Ask for user's name
System.out.println("Hi there, what's your name?");
String name = scanner.nextLine();

// Display a personalized welcome message
System.out.println("Hey, " + name + "!");
System.out.println("My name starts with 'S' too. :)");
System.out.println("But it doesn't end with 'y'... :(");

// Welcome message to the restaurant
System.out.println("Anyway! Welcome to my restaurant!");
System.out.println("Each day, I offer a special dish at a special price.");

// Generate random special dish
String specialDish = generateSpecialDish();
double cost = generateCost();

// Display today's special dish and its cost
System.out.println("Today's dish is:");
System.out.println("    " + specialDish);
System.out.printf("    cost:  $%.2f", cost);

// Ask the user how much money they have
System.out.println("How much money do you have?");
String moneyInput = scanner.nextLine();

// Try to convert the money input to a double
try {
double money = parseMoneyInput(moneyInput);

// Check if the user has enough money
if (money >= cost) {
// Calculate change
double change = money - cost;

// Display success message with change
System.out.printf("Perfect! Here you go! And here is $%.2f in change.", change);
System.out.println("Enjoy your " + specialDish + "! :)");
else {
// Display insufficient funds message
System.out.println("Uhh.. you don't have enough money.");
System.out.println("Sorry, try again next time! :)");

catch (NumberFormatException e) {
// Handle invalid input exception
System.out.println("Uhh.. you're supposed to write a monetary amount, like: $1.25");
System.out.println("Sorry, try again next time! :)");


// Close scanner
scanner.close();


// Method to generate a random special dish
private static String generateSpecialDish() {
String[] dishes = {"chicken curry with rice", "double cheeseburger", "steak and lobster";
Random random = new Random();
int index = random.nextInt(dishes.length);
return dishes[index];


// Method to generate a random cost for the special dish
private static double generateCost() {
Random random = new Random();
return 5 + random.nextDouble() * 20; // Random cost between $5 and $25


// Method to parse the money input from the user
private static double parseMoneyInput(String input) {
// Remove any non-numeric characters except for a decimal point
String cleanedInput = input.replaceAll("[^\\\\d.]", "");
return Double.parseDouble(cleanedInput);

--------------------------------------------------------------------------

Here is the 8 error codes that jGRASP is giving:


 ----jGRASP exec: javac -g WilliamsStanley05.java
WilliamsStanley05.java:64: error: illegal start of expression
private static String generateSpecialDish() {
^
WilliamsStanley05.java:53: error: 'catch' without 'try'
catch (NumberFormatException e) {
^
WilliamsStanley05.java:48: error: 'else' without 'if'
else {
^
WilliamsStanley05.java:37: error: 'try' without 'catch', 'finally' or resource declarations
try {
^
WilliamsStanley05.java:65: error: '}' expected
String[] dishes = {"chicken curry with rice", "double cheeseburger", "steak and lobster";
                                                                                        ^
WilliamsStanley05.java:72: error: illegal start of expression
private static double generateCost() {
^
WilliamsStanley05.java:78: error: illegal start of expression
private static double parseMoneyInput(String input) {
^
WilliamsStanley05.java:81: error: reached end of file while parsing
return Double.parseDouble(cleanedInput);
                                        ^
8 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


------------------------------------------------------------

Below is the original assignment question:

Objective: Create an interactive Java program that models a restaurant by asking the user to enter input, generates a random response, and demonstrates the student's understanding of handling exceptions.

Related SLOs:

    SLO #1: Use an appropriate programming environment to design, code, compile, run, and debug computer programs.
    SLO #3: Illustrate basic programming concepts such as program flow and syntax of a high-level general-purpose language and basic security practices.
    SLO #4: Demonstrate working with primitive data types, strings, and arrays.

Instructions:

    Using jGRASP, write a Java program named LastnameFirstname05.java, using your last name and your first name, that does the following:
        Asks the user for their name, then addresses the user and welcomes them to a restaurant.
            Use at least two String methods on their name throughout the program. You can do whatever you want, I'm just looking to see that you know how to use the String methods.
        Generates a random number to determine the restaurant's special dish for the day.
            Use a Math method to generate the random number
            Have at least 3 dishes that your program can randomly choose
            Highlight the following text if you need a hint:  One way to do this is to make the computer generate a random number: 1, 2, or 3. If the computer generates a 1, then the special dish is "chicken katsu"; if it's a 2, then it's "double cheeseburger"; if it's a 3, then it's "steak and lobster".
        Generate a random number to determine the cost of the special dish for the day.
            Remember to use printf to correctly format monetary values to 2 decimal places
        Ask the user how much money they have. If they have enough money to pay for the dish, give them the dish and change, if applicable. Otherwise, inform them they don't have enough and say goodbye.
            Remember to use printf to correctly format monetary values to 2 decimal places
        Anticipate exceptions and handle them so the program does not crash. (See Example Output below.)
        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. :)
    Your program MUST:
        Generate a random special dish/food each time the program runs
        Anticipate and handle any possible exceptions
        Actively use line comments stating what each section of code does
        Consider the possibility of a user not following directions. For example, if you tell them to type 1, 2, or 3, what should your program do if they type -5? 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.
    Your program must conform to the Java coding standards reviewed in class.
    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 DoeJohn05
Hi there, what's your name?
JOHN
Hey, John!
My name starts with 'J' too. :)
But it doesn't end with ''... :(n

Anyway! Welcome to my restaurant!
Each day, I offer a special dish at a special price.

Today's dish is:
     chicken curry with rice
     cost:  $100

How much money do you have?
$100.25

Perfect! Here you go! And here is $00.25 in change.
Enjoy your chicken curry with rice! :)
 ----jGRASP: operation complete.

    This is another example of what your program should output:

 ----jGRASP exec: java DoeJohn05
Hi there, what's your name?
JOHN
Hey, john!
My name starts with 'J' too. :)
But it doesn't end with 'n'... :(

Anyway! Welcome to my restaurant!
Each day, I offer a special dish at a special price.
Today's dish is:
     blueberry pancakes with maple syrup
     cost:  $2

How much money do you have?
$ONE MILLION BUCKS

Uhh.. you're supposed to write a monetary amount, like: $1.25
Sorry, try again next time! :)
 ----jGRASP: operation complete.

Criteria:

    Properly anticipating and handling invalid user input
    Complying with Java coding standards
    Properly choosing and displaying random choices and price
    Program flow and clear output/instructions to the user
    Assignment submitted to specifications. For example, documentation comments are included or are complete/correct, file name follow the specified format, your name included in the file name, correct file attached, etc.
   No miscellaneous mistakes, bugs, or problems.

Step by Step Solution

3.38 Rating (167 Votes )

There are 3 Steps involved in it

Step: 1

The errors in your code are caused by various syntax and logical errors Heres a corrected version of your code java import javautilScanner import java... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions

Question

14. What are the functions of the prefrontal cortexpg109

Answered: 1 week ago

Question

22. How do opiates influence dopamine synapsespg109

Answered: 1 week ago