-1

I'm new at java, not doing well, need some help. I have a class called "RestaurantObject" and I am trying to create an object from that class in my main. I'm having a problem because one of the variables in that object is an array. I'm just really not sure what to put there.

Here is my main:

public class MyRestaurantObject {
     public static void main(String[] args) {

     RestaurantObject mcDonalds = new RestaurantObject("McDonalds", "McDonalds Menu", **WHAT GOES HERE?**);

Here is the code from my class "RestaurantObjects"

public class RestaurantObject {

private static final Object[] String = null;
private String restaurantName;
private String menu;
private String [] employees;


public RestaurantObject(String name, String menu, String [] employees){
setRestaurantName(name);
setMenu(menu);
setEmployees(employees);
}


//restaurantName
public String getRestaurantName(){
return restaurantName;
}

private void setRestaurantName(String name){
restaurantName = name;
}

//menu
public String getMenu(){
return menu;
}

private void setMenu(String menu){
this.menu = menu;
}

//employees
public String[] getEmployees(){
return employees;

}

private void setEmployees(String [] employees){
this.employees = employees;
}

}

I know I'm not doing this right. Please help!

Thank you!

Also, please correct me if any of my terminology is wrong.

cat hodges
  • 29
  • 5

4 Answers4

2

Just pass a new string array in the 3rd argument.

RestaurantObject mcDonalds = new RestaurantObject("McDonalds", "McDonalds Menu", new String[] {"Value1", "Value2"});
Eddie
  • 427
  • 3
  • 8
1

I'm guessing that you haven't learned Java varargs yet, but this is a perfect case where to use them. Then you could go

public RestaurantObject(String name, String menu, String...employees) {
   setRestaurantName(name);
   setMenu(menu);
   setEmployees(employees);
}

and later,

RestaurantObject mcDonalds = new RestaurantObject(
        "McDonalds", "McDonalds Menu", "John Doe", "Jane Doe");

If you can't use or haven't learned varargs yet, @fvu version 2 is my preference.

user949300
  • 15,364
  • 7
  • 35
  • 66
0

Well there are two options:

1 Create the array before your create your RestaurantObject (The class should really just be called Restaurant

String[] emplArr = new String[]{ "Bob", "Alice", "Fred"};
RestaurantObject mcDonalds;
mcDonalds = new RestaurantObject("McDonalds", "McDonalds Menu", emplArr);

2 Intialize an array with a static intializer in the constructor call for RestaurantObject

RestaurantObject mcDonalds;
mcDonalds = new RestaurantObject("McDonalds", "McDonalds Menu", new String[]{ "Bob", "Alice", "Fred"});
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • My teacher keeps calling them "objects" and he was specific about what this class should be called. It's bothering me as well. How can i initialize the array in the constructor call? Thank you very much. – cat hodges Feb 21 '13 at 01:23
  • Well there is a difference between a `Class` and an `Object`. You can think of a `Class` as a blueprint for an `Object`, it documents all the properties that `Objects` of that class possess. An `Object` on the other hand is a specific instance of a `Class`. So in your class `mcDonalds` is the instance of your `Restaurant` class that is specific to the McDonald's restaurant. – Hunter McMillen Feb 21 '13 at 01:25
0

You can do

RestaurantObject mcDonalds = new RestaurantObject(
    "McDonalds", "McDonalds Menu", new String[] {"John Doe", "Jane Doe"});

or

String[] employees = new String[] {"John Doe", "Jane Doe"};
RestaurantObject mcDonalds = new RestaurantObject(
    "McDonalds", "McDonalds Menu", employees);

or

String[] employees = new String[2];
employees[0] = "John Doe";
employees[1] = "Jane Doe";
RestaurantObject mcDonalds = new RestaurantObject(
    "McDonalds", "McDonalds Menu", employees);

A note regarding the latter: when you create an array like this, you have to know its size, which can be quite annoying sometimes. That's why many people will prefer to use ArrayLists which behaves more or less like an elastic array, ie it autoexpands when n item is added.

fvu
  • 32,488
  • 6
  • 61
  • 79