Results 1 to 2 of 2
Thread: Inheritance problem
- 08-18-2012, 08:26 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 1
- Rep Power
- 0
Inheritance problem
I have literally been stuck for almost 24 hours with no progress, so that is why I am here. I have 3 Java programs that I have written. They are Order.java, ShippedOrder.Java and UseOrder.Java. The UseOrder has the main method and it uses the other 2. It asks for input about a purchase order, which I get the data back correctly, but then it asks about adding shipping and handling, and when I do that, it returns all fields back as 0 or null except for the shipping cost field. I know that it is an inheritance problem, but I cannot figure out what I need to change to make it work properly.
Here is the parent class
Java Code:import java.util.Scanner; public class Order { protected String customerName; protected int customerNum, quantity; protected double unitPrice, totalPrice; Scanner input = new Scanner(System.in); public String getCustomerName() { return customerName; } public void setCustomerName() { System.out.println("Please enter customer name."); customerName = input.nextLine(); } public int getCustomerNum() { return customerNum; } public void setCustomerNum() { System.out.println("Please enter customer number."); customerNum = input.nextInt(); } public int getQuantity() { return quantity; } public void setQuantity() { System.out.println("Please enter quantity ordered."); quantity = input.nextInt(); } public double getUnitPrice() { return unitPrice; } public void setUnitPrice() { System.out.println("Please enter unit price."); unitPrice = input.nextDouble(); } public void computePrice() { totalPrice = quantity * unitPrice; } public void displayInfo() { System.out.println("Customer Name : " + customerName + "\nCustomer Number : " + customerNum + "\nQuantity Ordered : " + quantity + "\nUnit Price : $" + unitPrice + "\nTotal Price : $" + totalPrice); } }
The child class
the main classJava Code:import java.util.Scanner; public class ShippedOrder extends Order { private double extraCharge = 4; private double totalPrice1; private String customerName1 = super.getCustomerName(); private int customerNum1 = super.getCustomerNum(); private int quantity1 = super.getQuantity(); private double unitPrice1 = super.getUnitPrice(); public void computePrice() { totalPrice1 = quantity1 * unitPrice1 + extraCharge; } public void displayInfo() { System.out.println("Customer Name : " + customerName1 + "\nCustomer Number : " + customerNum1 + "\nQuantity Ordered : " + quantity1 + "\nUnit Price : $" + unitPrice1 + "\nShipped Price : $" + extraCharge + "\nTotal Price : $" + totalPrice1); } }
When I run it, this is what the second output comes back asJava Code:import java.util.Scanner; public class UseOrder { public static void main(String[] args) { int num, q; String name, response; double p; char responseChar; Scanner keyboard = new Scanner(System.in); Order order1 = new Order(); order1.setCustomerName(); order1.setCustomerNum(); order1.setQuantity(); order1.setUnitPrice(); order1.computePrice(); order1.displayInfo(); keyboard.nextLine(); System.out.println("Do you need the shipping and handling service?"); System.out.println("Enter y or n :"); response = keyboard.nextLine(); responseChar = response.charAt(0); if(responseChar == 'y') { ShippedOrder shippedorder1 = new ShippedOrder(); shippedorder1.getCustomerName(); shippedorder1.getCustomerNum(); shippedorder1.getQuantity(); shippedorder1.getUnitPrice(); shippedorder1.computePrice(); shippedorder1.displayInfo(); } } }
Java Code:customer name : null customer number: 0 Quantity Ordered: 0 Unit Price:0 Shipped Price: $4.0 Total Price: $4.0
- 08-18-2012, 09:12 PM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Inheritance problem
You seem to be under the impression that the values of order1's fields are automatically transferred to shippedorder1, but this isn't true.
order1 and shippedorder1 are 2 different objects. You didn't call any of the "setter-methods" for shippedorder1, so all of its fields are going to be the default value of 0 (or 4 in the case of extra charge). Therefore, calling the "getter-methods" for shippedorder1 is going to return those default values. Calling the "getter-methods" of shippedorder1 will not return the values of order1's fields."Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
Similar Threads
-
Inheritance Problem
By kazumahits in forum New To JavaReplies: 5Last Post: 01-11-2011, 03:46 PM -
Inheritance problem
By ZuperZombie in forum Advanced JavaReplies: 0Last Post: 04-02-2010, 03:55 PM -
Inheritance Problem
By g2beastie in forum New To JavaReplies: 4Last Post: 03-25-2010, 08:23 PM -
inheritance problem
By er1c550n20 in forum New To JavaReplies: 2Last Post: 03-10-2010, 06:01 PM -
Problem with Inheritance
By KronikAlkoholik in forum New To JavaReplies: 4Last Post: 08-25-2009, 12:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks