• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help with assignment

 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently in school for programming. I need to write some code that contains; a constructor, an override and an overload. I think i have the constructor and overload, however i can not seem to get the override to work. Any help is greatly appreciated.

class Pizza { // declare pizza class
int size; //declare variable for pizza size

Pizza() {
System.out.println("Order a Pizza"); // declare pizza
size = 0;
}

Pizza(int i) {
System.out.println("Ordering a " + i + " inch pizza"); //declare pizza count
size = i;
}

void info() {
System.out.println("Pizza size is " + size + " inches"); // print pizza and size
}

void info(String s) {
System.out.println(s + ": Pizza size is " + size + " inches"); // print pizza, size and string
}
} // end pizza class

class PizzaShop extends Pizza { // declare pizza subclass
int size; //declare variable for pizza size

PizzaShop() {
System.out.println("Customer Ordered a Pizza"); // declare pizza
size = 0;
}

PizzaShop(int i) {
System.out.println("Customer Ordered a " + i + " inch pizza"); //declare pizza count
size = i;
}

void info() {
System.out.println("Customer's Pizza size is " + size + " inches"); // print pizza and size
}

void info(String s) {
System.out.println(s + ":Customer's Pizza size is " + size + " inches"); // print pizza, size and string
}

public void main(String[] args) { // declare main
for (int i = 5; i < 27; i += 4) { // set i to 10increment by 4 up to and inlcude 26
PizzaShop p = new PizzaShop(i); // set pizza constructor
p.info(); // 1 pizza declare
p.info("overloaded method"); //2 pizza declare
//PizzaShop myPizzaShop = new PizzaShop();
// Pizza myPizza = myPizzaShop;


}

// Overloaded constructor:
new PizzaShop(); // return pizza
}
}// end pizzashop subclass
public class JavaOverloading { //declare javaoverload class

public static void main(String[] args) { // declare main
for (int i = 10; i < 27; i += 4) { // set i to 10increment by 4 up to and inlcude 26
Pizza p = new Pizza(i); // set pizza constructor
p.info(); // 1 pizza declare
p.info("overloaded method"); //2 pizza declare
}
// Overloaded constructor:
new Pizza(); // return pizza
PizzaShop obj = new PizzaShop();
obj.info();

}

}

// end javaoverload class
[ October 13, 2008: Message edited by: Tammy Sadler ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please use the code button; it preserves your indentation and makes the post easier to read.
You have a constructor, and you have overloaded it, because you have two constructors. It is not usual to have print statements everywhere like that, but it will allow you to follow the flow of the program. I have my doubts about the no-argument constructor creating a 0" pizza. Since you said "school" you are probably in North America and surely a 0" pizza is anathema to all Americans. Suggest changing that to some reasonable default value, maybe 10.

Why does PizzaShop extend Pizza? You can hardly say a PizzaShop "IS-A" Pizza! And also don't have a field in a subclass which has the same name as a field in its superclass; that is a sure-fire recipe for confusion later on in life.
You seem to have most of it working, but towards the end you say new Pizza(); and never do anything with that Pizza.

Try creating a HotPizza class, which can be a subclass of Pizza, with a String ingredient field, then see how you set up a 10" pizza with pepperoni/chillis/choritzo. Remember you don't have size in the subclass, that lives in the Pizza class.
Then try overriding its info() methods, remembering you can call the superclass methods from the subclass methods.
 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok will try that thanks
 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure if i did it right, but i cant figure out the proper way to use super to pull in the constructors from pizza.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

In your original code, the info() method was overridden (even though it didn't make sense that PizzaShop extended Pizza). And it looks like you were attempting to demonstrate overriding with...

I think it's really polymorphic behavior you're trying to demonstrate. You're on the right track, because your variable name "obj" suggests that the reference type is Object instead of something more specific. But in fact, the reference type here is PizzaShop, and that's exactly the type of object it's pointing to. (Besides, if "obj" really did have a reference type of Object, then you would not be able to call the info() method on it.)

To see how to make this work, read How my dog learned polymorphism. Note that in that example, a Dog IS-AN Animal, just as your HotPizza IS-A Pizza.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And see if this part of the Java Tutorials helps. (If it doesn't we'll have to try elsewhere!)
 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i looked at what you sent me, and im still kinda confused as to where the code goes, here is what i have it compiles, but no override.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
set up a 10" pizza with pepperoni/chillis/choritzo..


chorizo pizza!
that's what i'm talking about
 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i got it to override, one last question, i would like it to run the for loop in the subclass as well, how do i do that, I assume put the code in the sub class but how do i call all the constructors from the super class ?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want constructors for the HotPizza class. Also, as I said earlier, an ingredient field, so Bill can have his Choritzo. But you will have to add the Choritzo or Pepperoni or Chillis in the constructor.
Then persuade your info() method (maybe printInfo would be a better name for it) to print out "Red-hot Chillis too-ouch!" as an addition to what the superclass method prints.
 
Tammy Sadler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok im lost lol
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have they told you about constructors in subclasses? Back to the Java Tutorial might help.

You need to find out how to write a subclass constructor, depending on how much is "new" in the subclass (the ingredient, choritzo or chillis or pepperoni or whatever) and how much is "old" and is in the superclass (the size).

Did you understand the Java Tutorial link? It is particularly the bit after "hiding fields" and before "object as a superclass" that I think is useful.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tammy Sadler:
ok im lost lol

Your Pizza class has 2 constructors defined: Pizza() and Pizza(int i).

But your HotPizza class does not have any constructors defined. Therefore, the compiler will provide a default no-args constructor (HotPizza()), but you do not have a constructor for HotPizza that will take an int argument.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic