Results 1 to 7 of 7

Thread: java class help

  1. #1
    latooka is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default java class help

    Hi, I need to create a supporting class that will be used by the MagicJavaChefDriver class. My supporting class must allow the MagicJavaChefDriver to be compiled and run with no changes and produce output as shown below.
    Output:
    Serving a meal for one. Pouring coffee. The main course is pancakes. Bon appetite!

    Serving 4 helpings. Pouring iced tea. Starting with iceberg lettuce and cucumber salad. The main course is tomato soup. Desert is a lovely chocolate chip cookie. Bon appetite!

    Serving 2 helpings. Pouring red wine. Starting with caprese salad. The main course of chicken cordon bleu is served with steamed brocoli and mashed potatoes. Desert is a lovely cheese cake. Bon appetite!

    MagicJavaChefDriver:
    public class MagicJavaChefDriver {

    public static void main(String[] args) {

    MagicJavaChef breakfast = new MagicJavaChef();

    MagicJavaChef lunch = new MagicJavaChef();

    MagicJavaChef romanticDinner = new MagicJavaChef();

    breakfast.setDrink("coffee");
    breakfast.setMain("pancakes");
    breakfast.setNumberServings(1);

    lunch.setDrink("iced tea");
    lunch.setSalad("iceberg lettuce and cucumber");
    lunch.setMain("tomato soup");
    lunch.setDesert("chocolate chip cookie");
    lunch.setNumberServings(4);

    romanticDinner.setDrink("red wine");
    romanticDinner.setSalad("caprese");
    romanticDinner.setVegetable("steamed brocoli");
    romanticDinner.setStarch("mashed potatoes");
    romanticDinner.setMain("chicken cordon bleu");
    romanticDinner.setDesert("cheese cake");
    romanticDinner.setNumberServings(2);

    System.out.println(breakfast);
    System.out.println();
    System.out.println(lunch);
    System.out.println();
    System.out.println(romanticDinner);
    System.out.println();

    //Uncomment this section for the extra-credit challenge worth 20 points on project 4
    /*
    breakfast.setDebug(true);
    lunch.setDebug(true);
    romanticDinner.setDebug(true);

    System.out.println(breakfast);
    System.out.println();
    System.out.println(lunch);
    System.out.println();
    System.out.println(romanticDinner);
    System.out.println();
    */

    } // end method main()


    } // end class MagicJavaChefDriver

    What I have so far:
    public class MagicJavaChef {

    private final int MAX_MIX_LEVEL = 10;
    private int NumberServings;
    private String[] Drink = {"", "coffee", "iced tea", "", "red wine"} ;
    private String[] Main = {"", "pancakes", "chicken cordon bleu", "", "tomato soup"};
    private String[] Salad = {"", "iceberg lettuce and cucumber", "caprese", "", ""};
    private String[] Vegetable = {"", "", "steamed brocoli", "", ""};
    private String[] Starch = {"", "", "mashed potatoes", "", ""};
    private String[] Desert = {"", "", "chocolate chip cookie","", "cheese cake"};

    public MagicJavaChef() {
    setDrink();
    setMain();
    setSalad();
    setVegetable();
    setStarch();
    setDesert();
    setNumberServings();
    }
    public void setNumberServings() {

    }
    public int getNumberServings() {
    return NumberServings;
    }
    public void setDrink() {

    }
    public String[] getDrink() {
    return Drink;
    }
    public void setMain() {

    }
    public String[] getMain() {
    return Main;
    }
    public void setSalad() {

    }
    public String[] getSalad() {
    return Salad;
    }
    public void setVegetable() {

    }
    public String[] getVegetable() {
    return Vegetable;
    }
    public void setStarch() {

    }
    public String[] getStarch() {
    return Starch;
    }
    public void setDesert() {

    }
    public String[] getDesert() {
    return Desert;
    }
    }

    Please help....

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: java class help

    Do you have a specific question? Just from glancing at your code, it looks like you should read this: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: java class help

    Please use [code] tags [/code] when posting code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    latooka is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default java class/method help

    I am trying to get the following output:
    Serving a meal for 1. Pouring coffee. The main course is pancakes. Bon appetite!
    I am having trouble getting it to work like I need to. This is what I have:

    Java Code:
    public class MagicJavaChef {
        int NumberServings;
        String Drink;
        String Main;
        public MagicJavaChef() {
        }
        public void setNumberServings(int setNumberServings) {
            NumberServings = setNumberServings;
        }
        public void setDrink(String setDrink) {
            Drink = setDrink;
        }
        public void setMain(String setMain) {
            Main = setMain;
        }
        public void breakfast() {
            System.out.println("Serving a meal for " + NumberServings + ". Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!");
        }
    }
    and

    Java Code:
    public class MagicJavaChefDriver {
     public static void main(String[] args) {
    
        MagicJavaChef breakfast = new MagicJavaChef();
    
        breakfast.setDrink("coffee");
        breakfast.setMain("pancakes");
        breakfast.setNumberServings(1);
        breakfast.breakfast(); // I get the correct readout if I use this line - I don't not want to use this
    
        System.out.println(breakfast); //I want to use this line instead
      }
    }
    This is my output with the line I do not want to use - output is as desired though
    run:
    Serving a meal for 1. Pouring coffee. The main course is pancakes. Bon appetite!
    BUILD SUCCESSFUL (total time: 0 seconds)
    This is the output with the line I DO want to use
    run:
    magicjavachef.MagicJavaChef@265f00f9
    BUILD SUCCESSFUL (total time: 0 seconds)
    How can I get this to work using my desired line? Thank you in advance.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: java class/method help

    Please go through the Forum Rules -- particularly the second and third paragraphs.

    I'm merging your two threads.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    latooka is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default Re: java class/method help

    If I add the following to my MagicJavaChef class I get the proper readout.
    Java Code:
    @Override
    public String toString() {
         return "Serving a meal for " + NumberServings + ". Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!";
    }
    How coud I rewrite this to allow for multiple returns?

    Say with my original methods:
    Java Code:
        public void breakfast() {
            System.out.println("Serving a meal for " + NumberServings + ". Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!");
        }
        public void lunch() {
            System.out.println("Serving " + NumberServings + " helpings. Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!");
        }
    I would like something like this- though this does not work:
    Java Code:
    @Override
        public String toString() {
        return "Serving a meal for " + NumberServings + ". Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!" +
        "Serving " + NumberServings + " helpings. Pouring " + Drink + ". The main course is " + Main + ". Bon appetite!";
        }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: java class help

    You are not expected to do that for the different instances.
    The Driver code is creating three different objects, 'breakfast', 'lunch', 'romanticDinner'.
    Your class needs to take the inputs for those and have the toString() convert them into the expected output.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 03-01-2012, 04:07 PM
  2. Replies: 3
    Last Post: 01-27-2012, 12:20 AM
  3. Replies: 16
    Last Post: 05-28-2009, 04:41 PM
  4. Replies: 14
    Last Post: 05-28-2009, 06:44 AM
  5. class.java to class.exe
    By f_the_cook in forum New To Java
    Replies: 11
    Last Post: 07-16-2008, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •