Question

For this program I ran it and it said illegal start of the expression for line...

For this program I ran it and it said illegal start of the expression for line 49 where I have public static double how do I fix it?

A bakery sells three types of muffins whose prices are as follows: 1- blueberry muffin, $2.98 2- chocolate chip muffin, $4.50 and 3- banana muffin, $9.98. You will write an application which calculates the total retail value of the sold muffins. Please check the following requirements: • Create a sentinel-controlled while loop. The flag value will be 0. Display flag value in your messages. • Display the menu and ask for the muffin number (int) (1 for blueberry one, 2 for chocolate chip one, or 3 for banana one). • Then, ask for the quantity sold for the related muffin (int). • Create a switch a statement to calculate the retail price for each muffin. • When the user terminates the application, display the total retail value (double) of all muffins. • Additionally, add an if-else statement to check whether the user only enters 1, 2, 3 or 0. If the user enters invalid input, display a message that the user can only enter 1, 2, 3 or 0. Then, ask for new input. (8 pts) UPLOAD Sales.java

5

Sample output: Total retail value of all muffins sold is 10*4.50 + 10*9.98 + 15*9.98=294.50

// Sales.java
// Determining the price of the muffins
import java.util.Scanner;

public class Sales {
  
   public static void main (String[] args) {
       // Initiating variables
       double price;
       int quantity;
       int item = 1;
       double totalValue = 0;
      
       // Create Scanner to obtain input from the window
       Scanner userInput = new Scanner(System.in);
      
       // Ending the loop if you select 0
       while (item != 0) {
           System.out.println ("0 = Exit");
           System.out.println ("1 = Blueberry Muffin");
           System.out.println ("2 = Chocolate Chip Muffin");
           System.out.println ("3 = Banana Muffin");
           System.out.print ("Enter the muffin number");
          
           // Converting to int
           item = userInput.nextInt();
           // Verifying that youre selecting 0, 1, 2, or 3
       if (item < 0 || item > 3) {
           System.out.println ("Choose a valid number%n");
       }
       // If you choose any number other than 0, enter the quantity
       else if (item != 0) {
           System.out.print ("Enter the quantity:");
           quantity = userInput.nextInt();
          
           // Getting the price of the muffin
           price = getValue(item);
          
           // Getting the total value of the muffins
           totalValue += (quantity * price);
          
           System.out.println();
       }
      
       //output
       System.out.printf("%nTotal retail value of all muffins sold is: $%.2f%n", totalValue);
   }
      
public static double getValue(int itemType) {
   switch(itemType) {
       case 1:
           return 2.98;//blueberry
       case 2:
           return 4.50;//chocolate chip
       case 3:
           return 9.98;//banana
       default:
           return 0.0;
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Explanation:

By mistake, you have written the static function inside the main function only and one curly brace was missing. Otherwise the code is perfectly fine. Fixed that error by taking the getValue function out of the main function curly braces.

Code:

import java.util.Scanner;

public class Main {
  
public static double getValue(int itemType) {
switch(itemType) {
case 1:
return 2.98;//blueberry
case 2:
return 4.50;//chocolate chip
case 3:
return 9.98;//banana
default:
return 0.0;
}
}
  
public static void main (String[] args) {
// Initiating variables
double price;
int quantity;
int item = 1;
double totalValue = 0;
  
// Create Scanner to obtain input from the window
Scanner userInput = new Scanner(System.in);
  
// Ending the loop if you select 0
while (item != 0) {
System.out.println ("0 = Exit");
System.out.println ("1 = Blueberry Muffin");
System.out.println ("2 = Chocolate Chip Muffin");
System.out.println ("3 = Banana Muffin");
System.out.print ("Enter the muffin number");
  
// Converting to int
item = userInput.nextInt();
// Verifying that youre selecting 0, 1, 2, or 3
if (item < 0 || item > 3) {
System.out.println ("Choose a valid number%n");
}
// If you choose any number other than 0, enter the quantity
else if (item != 0) {
System.out.print ("Enter the quantity:");
quantity = userInput.nextInt();
  
// Getting the price of the muffin
price = getValue(item);
  
// Getting the total value of the muffins
totalValue += (quantity * price);
  
System.out.println();
}
  
//output
System.out.printf("%nTotal retail value of all muffins sold is: $%.2f%n", totalValue);
}
  

}

}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
For this program I ran it and it said illegal start of the expression for line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java programm that calculates the total of a retail sale should ask the user...

    Write a java programm that calculates the total of a retail sale should ask the user for the following: The retail price of the item being purchased The sales tax rate Once these items have been entered, the program should calculate and display the following: The sales tax for the purchase The total of the sale I tried doing it here. but it is not giving me the right answer. kindly help correct the errors. import java.util.Scanner; public class SalesTax...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • Let's fix the displayMenu() method! First, edit the method to do the following: We want to...

    Let's fix the displayMenu() method! First, edit the method to do the following: We want to also allow the user to quit. Include a "Quit" option in the print statements! Have it get the user input from within the method itself and return an int based on the user's choice. (Make sure to also edit the method header and how it is called back in the main() method!) What is the method's return type now?                  ...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment...

    [IN JAVA] Finish the program. There are instructions in the comments. You may want to comment out the function calls in main, and get one function to work at a time. Thanks in advance! Template import java.util.Scanner; public class Main{ public static Scanner kb = new Scanner(System.in); public static void main(String [] args) { String productName; double productPrice; int productQuantity; double shipping; int quantityDiscount; double finalTotal; productName = queryName(); productPrice = queryPrice(); productQuantity = queryQuantity(); System.out.println(); quantityDiscount = computeQuantityDiscount(productQuantity); shipping...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public...

    I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase {    // instance variables    private String itemName;    private String itemDescription;    private int itemPrice;    private int itemQuantity;    // default constructor    public ItemToPurchase() {        this.itemName = "none";        this.itemDescription = "none";        this.itemPrice = 0;        this.itemQuantity = 0;    }    public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT