Results 1 to 2 of 2
  1. #1
    paquizzle is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default 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

    Java 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);    
        }
    }
    the main class

    Java 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();
    
            }
        }
    }
    When I run it, this is what the second output comes back as
    Java Code:
    customer name : null
    customer number: 0
    Quantity Ordered: 0
    Unit Price:0
    Shipped Price: $4.0
    Total Price: $4.0

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default 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

  1. Inheritance Problem
    By kazumahits in forum New To Java
    Replies: 5
    Last Post: 01-11-2011, 04:46 PM
  2. Inheritance problem
    By ZuperZombie in forum Advanced Java
    Replies: 0
    Last Post: 04-02-2010, 04:55 PM
  3. Inheritance Problem
    By g2beastie in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 09:23 PM
  4. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 07:01 PM
  5. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 01:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •