Writing a Restaurant driver class

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

50 Replies - 5244 Views - Last Post: 05 January 2011 - 01:34 PM Rate Topic: -----

#1 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Writing a Restaurant driver class

Posted 02 January 2011 - 09:22 AM

I have a homework assignment to do, that involves two classes. Unfortunately, it involves boolean expressions, strings, and integers.....and I am at a COMPLETE loss.....

Here's the class I HAVE to use:


public class AutomatedKitchen
{
  public final int DEFAULT_NUMBER_SERVINGS = 4;
  public final int DEFAULT_COOK_TEMP = 350;
  public final int DEFAULT_COOK_TIME = 30;
  public final int MAX_INGREDIENTS = 10;
  private int numberServings;
  private String[] ingredientList;
  private int cookTemp;
  private int cookTime;
  private boolean healthyFood;
  private int nextArrayIndex = 0;
  private String cookedMeal;
  private boolean mealReady;

  public AutomatedKitchen()
  {
    this.numberServings = 4;
    this.cookTemp = 350;
    this.cookTime = 30;
    this.ingredientList = new String[11];
    this.ingredientList[0] = "water";
    this.ingredientList[10] = "";
  }

  public void setNumberServings(int paramInt)
  {
    this.numberServings = paramInt;
  }

  public void setIngredient(String paramString)
  {
    if (this.nextArrayIndex < 10) {
      this.ingredientList[this.nextArrayIndex] = paramString;
      this.nextArrayIndex += 1;
    }
  }

  public int getNumberIngredients()
  {
    return this.ingredientList.length;
  }

  public void setCookTemp(int paramInt)
  {
    this.cookTemp = paramInt;
  }

  public int getCookTemp()
  {
    return this.cookTemp;
  }

  public void setCookTime(int paramInt)
  {
    this.cookTime = paramInt;
  }

  public int getCookTime()
  {
    return this.cookTime;
  }

  public void setHealthyFood(boolean paramBoolean)
  {
    this.healthyFood = paramBoolean;
  }

  public boolean getHealthyFood()
  {
    return this.healthyFood;
  }

  public void cookMeal()
  {
    this.mealReady = true;
  }

  public boolean readyToServe()
  {
    return this.mealReady;
  }

  public String serveMeal()
  {
    if (!readyToServe()) {
      return "This meal is not cooked yet!";
    }

    if (this.nextArrayIndex == 0) this.ingredientList[10] = this.ingredientList[0];
    else {
      for (int i = 0; i < this.nextArrayIndex; i++) {
        if (i == this.nextArrayIndex - 1) this.ingredientList[10] = (this.ingredientList[10] + "and " + this.ingredientList[i]); else {
          this.ingredientList[10] = (this.ingredientList[10] + this.ingredientList[i] + ", ");
        }
      }
    }
    double d = Math.random() * 10.0D;
    String str1;
    if (d < 3.0D) str1 = "pretty good";
    else if (d < 6.0D) str1 = "terrible"; else
      str1 = "five star delicous";
    String str2;
    if (getHealthyFood()) str2 = "healthy"; else str2 = "unhealthy";
    this.mealReady = false;
    return "Serving a meal for " + this.numberServings + ".  This meal is " + str2 + ".  We cooked a blend of " + this.ingredientList[10] + " for " + getCookTime() + " minutes at " + getCookTemp() + " degrees.  It's " + str1 + ".";
  }
}



And here's the main class that I've written thus far.....to include lots of errors:


public class Restaurant {

public static void main (String[] args) {

AutomatedKitchen Table1 = new AutomatedKitchen();
Table1.setNumberServings(4);
Table1.setIngredient("water")
Table1.setCookTemp(350);
Table1.setCookTime(30);
Table1.getNumberIngredients();
Table1.getCookTemp();
Table1.getCookTime();


System.out.println(Table1);

AutomatedKitchen Table2 = new AutomatedKitchen();
Table2.setNumberServings(10);
Table2.setIngredient("water, french fries, pizza, buffalo wings");
Table2.setCookTemp(400);
Table2.setCookTime(10);

System.out.println(Table2);

AutomatedKitchen Table3 = new AutomatedKitchen();
Table3.setNumberServings(1);
Table2.setIngredient(water, minestrone soup, chicken cordon bleu");
Table3.setCookTemp(375);
Table3.setCookTime(30);

System.out.println(Table3);


} // end of main() method
}



My assignment is to:

Write a driver class that simulates a restaurant using AutomaticKitchen.class. You have three tables, one with four people, one with 12 people, and one with 1 person. Everyone at any given table eats the same thing.

Use the AutomatedKitchen in your driver to make and serve a different meal to each table. You can be creative with your ingredients. You might not need to use all the available API methods.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing a Restaurant driver class

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,612
  • Joined: 27-December 08

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 09:34 AM

Post your errors.
Was This Post Helpful? 0
  • +
  • -

#3 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 09:44 AM

Errors:

C:\Users>cd java

C:\Users\java>javac Restaurant.java

Restaurant.java:27: ')' expected
Table2.setIngredient(water, minestrone soup, chicken cordon bleu");
^
Restaurant.java:27: illegal start of expression
Table2.setIngredient(water, minestrone soup, chicken cordon bleu");
^
Restaurant.java:27: ';' expected
Table2.setIngredient(water, minestrone soup, chicken cordon bleu");
^
Restaurant.java:27: unclosed string literal
Table2.setIngredient(water, minestrone soup, chicken cordon bleu");
^
4 errors



I'm not sure why that happens, even if I use a one-word string in the setIngredient, then I get the following output after it is compiled:





C:\Users\java>javac Restaurant.java

C:\Users\java>java Restaurant
AutomatedKitchen@addbf1
AutomatedKitchen@42e816
AutomatedKitchen@9304b1

C:\Users\java>

This post has been edited by decky: 02 January 2011 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 09:55 AM

The error is in this line:
Table2.setIngredient(water, minestrone soup, chicken cordon bleu"); 

setIngredient should accept a String object.
then it should be:
Table2.setIngredient("water, minestrone soup, chicken cordon bleu"); 

Was This Post Helpful? 1
  • +
  • -

#5 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 10:09 AM

OK, so it would seem that I was lacking a semicolon, and a double quotation. However, when I compile, and run it, I receive the following:

C:\Users\java>javac Restaurant.java

C:\Users\java>java Restaurant
AutomatedKitchen@addbf1
AutomatedKitchen@42e816
AutomatedKitchen@9304b1

C:\Users\java>

Here's my updated code:


public class Restaurant {

public static void main (String[] args) {

AutomatedKitchen Table1 = new AutomatedKitchen();
Table1.setNumberServings(4);
Table1.setIngredient("water");
Table1.setCookTemp(350);
Table1.setCookTime(30);
Table1.getNumberIngredients();
Table1.getCookTemp();
Table1.getCookTime();


System.out.println(Table1);

AutomatedKitchen Table2 = new AutomatedKitchen();
Table2.setNumberServings(10);
Table2.setIngredient("water, french fries, pizza, buffalo wings");
Table2.setCookTemp(400);
Table2.setCookTime(10);

System.out.println(Table2);

AutomatedKitchen Table3 = new AutomatedKitchen();
Table3.setNumberServings(1);
Table2.setIngredient("water, minestrone soup, chicken cordon bleu");
Table3.setCookTemp(375);
Table3.setCookTime(30);

System.out.println(Table3);


} // end of main() method
}

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 10:40 AM

System.out.println(Table1);
in that statement you actually invoke the toString method of Automatedkitchen class.
in order to print a "meaningfull" String of an AutomatedKitchen Object you have to override the toString method of that class.
Was This Post Helpful? 0
  • +
  • -

#7 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 10:50 AM

OK...I'm very new to programming, and I don't understand what you just told me...
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 02:20 PM

Also if you set
ingredientList[0] = "water";
then nextArrayIndex should be set to 1
and
ingredientList[10] = ""; is a useless statement that might show you missed something in the concept
Was This Post Helpful? 1
  • +
  • -

#9 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 02:56 PM

The AutomatedKitchen class is not something I'm allowed to edit. It is the class that I have to use to write the driver program, Restaurant. So what am I doing wrong in my Restaurant class?
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2858
  • View blog
  • Posts: 10,963
  • Joined: 15-July 08

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 05:27 PM

More on what japanir said. When you call System.out.println on an object, the method calls that object's toString() method, which returns a String that represents that object. By default, they print the Object name and the Hashkey for that object. Unless you override toString() in your class, how is toString supposed to change? In this method, you need to construct a String out of the fields of your object to make meaningful output.

public String toString() {
    String output = ...
    return output;
}


This post has been edited by Dogstopper: 02 January 2011 - 05:28 PM
Reason for edit:: Posting fail. :p

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Writing a Restaurant driver class

Posted 02 January 2011 - 05:43 PM

May be you can make it that way

Table2.setIngredient("water minestrone_soup chicken cordon_bleu");

void setIngredient(String ingredients) {
   String[] token = ingredients.split(" ");
   for(int i = 0; i < token.length; ++i) {
     if(arrayIndex >= ingredientList.length)
         return;   // sorry no more room
     ingredientList[arrayIndex++] = token[i];
   }
}


Was This Post Helpful? 0
  • +
  • -

#12 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 03 January 2011 - 09:57 AM

View PostDogstopper, on 02 January 2011 - 04:27 PM, said:

More on what japanir said. When you call System.out.println on an object, the method calls that object's toString() method, which returns a String that represents that object. By default, they print the Object name and the Hashkey for that object. Unless you override toString() in your class, how is toString supposed to change? In this method, you need to construct a String out of the fields of your object to make meaningful output.

public String toString() {
    String output = ...
    return output;
}




Sorry for the late reply--different timezones! So where and how would I incorporate that into my Restaurant class?
Was This Post Helpful? 0
  • +
  • -

#13 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Writing a Restaurant driver class

Posted 03 January 2011 - 10:07 AM

You put it in the Table class and you put whatever data you want to be output (table number, customers, whatever) into the output string.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#14 decky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-December 10

Re: Writing a Restaurant driver class

Posted 03 January 2011 - 10:23 AM

So now when I try to compile, I get the following error:

C:\Users\java>javac Restaurant.java
Restaurant.java:35: cannot find symbol
symbol : variable Table1
location: class Restaurant
String output = Table1;
^
1 error

C:\Users\java>

Here's my revised code:

public class Restaurant {

public static void main (String[] args) {

AutomatedKitchen Table1 = new AutomatedKitchen();
Table1.setNumberServings(4);
Table1.setIngredient("water");
Table1.setCookTemp(350);
Table1.setCookTime(30);
Table1.getNumberIngredients();
Table1.getCookTemp();
Table1.getCookTime();

System.out.println(Table1);

AutomatedKitchen Table2 = new AutomatedKitchen();
Table2.setNumberServings(10);
Table2.setIngredient("water, french fries, pizza, buffalo wings");
Table2.setCookTemp(400);
Table2.setCookTime(10);

System.out.println(Table2);

AutomatedKitchen Table3 = new AutomatedKitchen();
Table3.setNumberServings(1);
Table3.setIngredient("water, minestrone soup, chicken cordon bleu");
Table3.setCookTemp(375);
Table3.setCookTime(30);

System.out.println(Table3);


} // end of main() method
public String toString() {
    String output = Table1;
    return output;
}
}


Was This Post Helpful? 0
  • +
  • -

#15 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Writing a Restaurant driver class

Posted 03 January 2011 - 10:26 AM

No, you put toString IN your AutomatedKitchen class (sorry, I mistakenly called it by its instance name rather than its actual name), and, for example, you'd do something like this:

public String toString(){
    String returnMe = "Number of servings: "+numberServings+", Cook time: "+cookTime; //Etc...whatever else you wanna add
    return returnMe;
}


This will stop it from returning random trash when you try to print the table (you'll get actual information instead of AutomatedKitchen@addbf1)

Yours,
Shane~

This post has been edited by ShaneK: 03 January 2011 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »