Question

I receive an error illegal start of expression starting at "public class Ingredient {". How would...

I receive an error illegal start of expression starting at "public class Ingredient {". How would I fix this error?

*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package SteppingStones;

/**

*

*

*

* @author jennifer.cook_snhu

*

*/

public class SteppingStone2_IngredientCalculator {

    /**

     *

     * @param args the command line arguments

     *

     */

    public static void main(String[] args) {

public class Ingredient {

        private String nameOfIngredient = "";

        private int numberCups = 0;

        private int numberCaloriesPerCup = 0;

        public Ingredient(String nameOfIngredient, int numberCups,

                int numberCaloriesPerCup) {

            this.nameOfIngredient = nameOfIngredient;

            this.numberCups = numberCups;

            this.numberCaloriesPerCup = numberCaloriesPerCup;

        }

        public String getNameOfIngredient() {

            return nameOfIngredient;

        }

        public int getNumberCups() {

            return numberCups;

        }

        public int getNumberCaloriesPerCup() {

           

            return numberCaloriesPerCup;

        }

        public double getTotalCalories() {

            return numberCaloriesPerCup * numberCups;

        }

        @Override

        public String toString() {

            return numberCups + " cups of " + nameOfIngredient + ", total Calories: " + getTotalCalories();

        }

       }    

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

The correct way of using this is:

Make two class one for the ingredients information and one for print the info or can use in one class also.

Ingredient.java:

public class Ingredient {

   private String nameOfIngredient = "";

   private int numberCups = 0;

   private int numberCaloriesPerCup = 0;

   public Ingredient(String nameOfIngredient, int numberCups, int numberCaloriesPerCup) {

       this.nameOfIngredient = nameOfIngredient;

       this.numberCups = numberCups;

       this.numberCaloriesPerCup = numberCaloriesPerCup;

   }

   public String getNameOfIngredient() {

       return nameOfIngredient;

   }

   public int getNumberCups() {

       return numberCups;

   }

   public int getNumberCaloriesPerCup() {

       return numberCaloriesPerCup;

   }

   public double getTotalCalories() {

       return numberCaloriesPerCup * numberCups;

   }

   @Override

   public String toString() {

       return numberCups + " cups of " + nameOfIngredient + ", total Calories: " + getTotalCalories();

   }

}

SteppingStone2_IngredientCalculator.java:


public class SteppingStone2_IngredientCalculator {
   public static void main(String[] args) {
       Ingredient ing = new Ingredient("Apple", 10, 20);
       System.out.println(ing.toString());
   }
}

sample output:

10 cups of Apple, total Calories: 200.0

Add a comment
Know the answer?
Add Answer to:
I receive an error illegal start of expression starting at "public class Ingredient {". How would...
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
  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the...

  • Hello, How can I make the program print out "Invalid data!!" and nothing else if the...

    Hello, How can I make the program print out "Invalid data!!" and nothing else if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero? I need this done by tomorrow if possible please. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student...

  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • I need help with my code. It keeps getting this error: The assignment is: Create a...

    I need help with my code. It keeps getting this error: The assignment is: Create a class to represent a Food object. Use the description provided below in UML. Food name : String calories : int Food(String, int) // The only constructor. Food name and calories must be // specified setName(String) : void // Sets the name of the Food getName() : String // Returns the name of the Food setCalories(int) : void // Sets the calories of the Food...

  • /* * To change this license header, choose License Headers in Project Properties. * To change...

    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkingaccounttest; // Chapter 9 Part II Solution public class CheckingAccountTest { public static void main(String[] args) {    CheckingAccount c1 = new CheckingAccount(879101,3000); c1.deposit(350); c1.deposit(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.withdraw(350); c1.withdraw(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.deposit(1000); System.out.println(c1); System.out.println("After calling computeMonthlyFee()"); c1.computeMonthlyFee(); System.out.println(c1); } } class BankAccount { private int...

  • COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field...

    COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field    */    private double cost;    private double budget;    private String destination;    /**    *    * @param cost    * @param budget    * @param destination    */    public Vacation(double cost, double budget, String destination) {        super();        this.cost = cost;        this.budget = budget;        this.destination = destination;    }    //getter and...

  • WRITE IN JAVA: I've been trying to do this for hours and I can't figure this...

    WRITE IN JAVA: I've been trying to do this for hours and I can't figure this out. I tried the toArray method and I'm stumped. I'm not sure if the remove method is right aswell. If you need anymore info, let me know. I need the following methods completed: toArray Remove ReverseArray /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template...

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
ADVERTISEMENT