File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes need help with assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help with assignment " Watch "need help with assignment " New topic
Author

need help with assignment

Tammy Sadler
Greenhorn

Joined: Oct 13, 2008
Posts: 6
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

Joined: Oct 13, 2005
Posts: 38016
    
  22
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

Joined: Oct 13, 2008
Posts: 6
ok will try that thanks
Tammy Sadler
Greenhorn

Joined: Oct 13, 2008
Posts: 6
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
And see if this part of the Java Tutorials helps. (If it doesn't we'll have to try elsewhere!)
Tammy Sadler
Greenhorn

Joined: Oct 13, 2008
Posts: 6
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

Joined: Nov 08, 2007
Posts: 457
Originally posted by Campbell Ritchie:
set up a 10" pizza with pepperoni/chillis/choritzo..


chorizo pizza!
that's what i'm talking about


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Tammy Sadler
Greenhorn

Joined: Oct 13, 2008
Posts: 6
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

Joined: Oct 13, 2005
Posts: 38016
    
  22
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

Joined: Oct 13, 2008
Posts: 6
ok im lost lol
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
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

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with assignment
 
Similar Threads
problem on OverLoading
Constructor overloading problem
Is there away I can have two or more classes in same file?
Why different output ???
How to invoke Child 's println using cast?