Inheritence and problems with static methods.
I'm having some trouble getting this program to work, my assignment deals with inheritance, and I'm basically trying to call my methods in main in order to run them.... so here's what I've gotten so far.
Code java:
import javax.swing.*;
public class Order
{
private static String customer, numb, quant, unitPrice;
private static int custNumber;
private static double orderQuant, unitCost;
public static double total;
public static void setCustomer()
{
customer = JOptionPane.showInputDialog(null,"Enter customer name:");
}
public static String getCustomer()
{
return customer;
}
public static void setCustNumber()
{
numb = JOptionPane.showInputDialog(null, "Enter costumer number:");
custNumber = Integer.parseInt(numb);
}
public static int getCustNumber()
{
return custNumber;
}
public static void setOrderQuant()
{
quant = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
orderQuant = Double.parseDouble(quant);
}
public static double getOrderQuant()
{
return orderQuant;
}
public static void setUnitCost()
{
unitPrice = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
unitCost = Double.parseDouble(unitPrice);
}
public static double getUnitCost()
{
return unitCost;
}
public static void computePrice()
{
total = (orderQuant * unitCost);
}
public static void display()
{
JOptionPane.showMessageDialog(null, "Customer Number: " + custNumber +
"\nCustomer Name: " + customer + "\nQuantity Ordered: " + Math.round(orderQuant) +
"\n Price Each: $" + unitCost + "\nThe cost of your order is: $" + total);
}
}
Code java:
public class ShippedOrder extends Order
{
private final double shipHandle = 4.00;
public void computePrice()
{
super.total = ((getOrderQuant() * getUnitCost()) + shipHandle);
}
public double getTotal()
{
return total;
}
}
Code java:
import javax.swing.*;
public class UseOrder
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "First, enter data for a pick up order.");
Order.setCustNumber();
Order.setCustomer();
Order.setOrderQuant();
Order.setUnitCost();
Order.computePrice();
Order.display();
JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
Order.setCustNumber();
Order.setCustomer();
Order.setOrderQuant();
Order.setUnitCost();
ShippedOrder.computePrice();
Order.display();
}
}
basically the problem is with the computePrice() methods in both the Order, and ShippedOrder classes. computePrice() method in Order, is supposed to be overridden by the computePrice() in ShippedOrder, basically adding $4.00 to the total. However if I make the computePrice() method in Order non-static, the UseOrder class can't use it but ShippedOrder class can, and if I make it static UseOrder class can use it, but ShippedOrder can't.
Quite a conundrum for me. I did read something about creating a new instance of the method perhaps? But dealing with inheritance I'm not quite sure how to go about doing such a thing. Thank you in advance for your help.
Re: Inheritence and problems with static methods.
If you're learning OOP principles, one of the first rules to learn is that you should avoid static anything except in very specific situations. Here your use of static methods are preventing inheritance from working at all. I suggest that *all* of your methods be instance or non-static methods with the only exception being the main method. To call non-static methods from the main method (or any static method), you need to first create an instance of your class, and then call the methods on that instance. In your code above, you're trying to call methods directly on the class, which again you shouldn't be doing. Create an Order object or a ShippedOrder object and call the *non-static* methods from the object.
Re: Inheritence and problems with static methods.
You mean to instead of creating three different classes I should just have one class with an Order and ShippedOrder method in the one class? The assignment is to create three classes I can't really get around that part.
Unless I totally missed what you meant, which I probably did.
Re: Inheritence and problems with static methods.
Quote:
Originally Posted by
Sylis
You mean to instead of creating three different classes I should just have one class with an Order and ShippedOrder method in the one class? The assignment is to create three classes I can't really get around that part.
Unless I totally missed what you meant, which I probably did.
Yep, you missed what I meant. Let me try again. You should have one class, Order, that has no static methods, no static fields, no static *anything*. You should have another class, ShippedOrder, that extends Order, that likewise contains *nothing* static. You should have a third class, UseOrder, that has a static main method, that creates an instance of ShippedOrder in main, and that calls methods on this instance.
Re: Inheritence and problems with static methods.
Thank you, I figured it out by using:
Code java:
import javax.swing.*;
public class UseOrder
{
public static void main(String[] args)
{
Order anOrder = new Order();
JOptionPane.showMessageDialog(null, "First, enter data for a pick up order.");
anOrder.setCustNumber();
anOrder.setCustomer();
anOrder.setOrderQuant();
anOrder.setUnitCost();
anOrder.computePrice();
anOrder.display();
ShippedOrder aShippedOrder = new ShippedOrder();
JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
anOrder.setCustNumber();
anOrder.setCustomer();
anOrder.setOrderQuant();
anOrder.setUnitCost();
aShippedOrder.computePrice();
anOrder.display();
}
}
my only problem now is the overriding of the computePrice(); method, the second compute price should be calculating an extra $4.00 on top for shipping and handling, but the computePrice() method in aShippedOrder isn't overriding the total from Order.
Re: Inheritence and problems with static methods.
Again, have you changed the code in Order and ShippedOrder as I've recommended?
Re: Inheritence and problems with static methods.
Yes I have.
Code java:
import javax.swing.*;
public class Order
{
private String customer, numb, quant, unitPrice;
private int custNumber;
private double orderQuant, unitCost;
public double total;
public void setCustomer()
{
customer = JOptionPane.showInputDialog(null,"Enter customer name:");
}
public String getCustomer()
{
return customer;
}
public void setCustNumber()
{
numb = JOptionPane.showInputDialog(null, "Enter costumer number:");
custNumber = Integer.parseInt(numb);
}
public int getCustNumber()
{
return custNumber;
}
public void setOrderQuant()
{
quant = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
orderQuant = Double.parseDouble(quant);
}
public double getOrderQuant()
{
return orderQuant;
}
public void setUnitCost()
{
unitPrice = JOptionPane.showInputDialog(null, "Enter quantity ordered:");
unitCost = Double.parseDouble(unitPrice);
}
public double getUnitCost()
{
return unitCost;
}
public void computePrice()
{
total = (orderQuant * unitCost);
}
public void display()
{
JOptionPane.showMessageDialog(null, "Customer Number: " + custNumber + "\nCustomer Name: " + customer + "\nQuantity Ordered: " + Math.round(orderQuant) +
"\n Price Each: $" + unitCost + "\nThe cost of your order is: $" + total);
}
}
Code java:
public class ShippedOrder extends Order
{
private final double shipHandle = 4.00;
public void computePrice()
{
total = ((getOrderQuant() * getUnitCost()) + shipHandle);
}
public double getTotal()
{
return total;
}
}
I took out every static out of the program, except main.
Re: Inheritence and problems with static methods.
Check your UserOrder's main method again. Which object are you using in the second half of this method, the Order object or the ShippedOrder object. Check again.
Re: Inheritence and problems with static methods.
Shouldn't it be the ShippedOrder method? The first set of dialog boxes set up the information, and then the computeOrder() in Order computes only quantity * price, and the computeOrder uses quantity * price + 4.00 for shipping and handling.
Re: Inheritence and problems with static methods.
Quote:
Shouldn't it be the ShippedOrder method?
Yes it should be, but what objects are you using mostly here?:
Code java:
// second half of main method
ShippedOrder aShippedOrder = new ShippedOrder();
JOptionPane.showMessageDialog(null, "Now, enter data for a shipped order.");
anOrder.setCustNumber(); // ** anOrder??? **
anOrder.setCustomer(); // ** anOrder??? **
anOrder.setOrderQuant(); // ** anOrder??? **
anOrder.setUnitCost(); // ** anOrder??? **
aShippedOrder.computePrice();
anOrder.display(); // ** anOrder??? **
Also, note that your setXXX(...) methods are non-standard. Usually they accept a parameter with which you set the class field, and usually they don't themselves do any user interaction (i.e., they don't contain Scanner or JOptionPane code). Perhaps this is what your instructor wants, but again typically the setXXX(...) methods don't do this.
Re: Inheritence and problems with static methods.
Well, I use the methods to set the customer number, customer name, order quantity, and unit cost from Order, and then computePrice from a shipped order class in order to calculate the four dollars into it total. should, ShippedOrder have it's own set methods for each variable?
Re: Inheritence and problems with static methods.
Quote:
Originally Posted by
Sylis
Well, I use the methods to set the customer number, customer name, order quantity, and unit cost from Order, and then computePrice from a shipped order class in order to calculate the four dollars into it total. should, ShippedOrder have it's own set methods for each variable?
ShippedOrder already has all of the non-static methods of Order. That's what inheritance is all about. Again please make the indicated method calls on the ShippedOrder object, not the Order object.
Re: Inheritence and problems with static methods.
You sir are a gentleman and a scholar.
I like that you gave me the answers I was looking for without just giving it to me. Made me think, and using my head, I figured it out, and instantly inheritance makes a world more sense to me than it originally did. Thank you very much.
Re: Inheritence and problems with static methods.
Re: Inheritence and problems with static methods.
Wow you guys are over-shooting the mark. The solution is far more simple:
1. Static fields and methods are not associated with objects. Thus - using them is out of the question, if you want to have sub-classes.
2. To use a method from another class, even non-static, you create a new instance of it. You don't have to make the methods static.
There, problem solved.
Re: Inheritence and problems with static methods.
Quote:
Originally Posted by
Programming_Hobbyist
Wow you guys are over-shooting the mark. The solution is far more simple:
1. Static fields and methods are not associated with objects. Thus - using them is out of the question, if you want to have sub-classes.
2. To use a method from another class, even non-static, you create a new instance of it. You don't have to make the methods static.
There, problem solved.
Aren't you being a bit presumptuous here? Please explain how my advice was "over-shooting the mark". I told him basically the same thing in my first post if you'd take the time to read the whole thread.