1 Replies - 322 Views - Last Post: 08 October 2011 - 09:59 PM Rate Topic: -----

#1 ITtams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-October 11

Need help with override methods in Java....my code doesn't work

Posted 08 October 2011 - 05:50 PM

Hello All,

I have written a 3 class inventory program for a Java course assignment, my code is included below, and I can't figure out why my calculation methods are not working. This is actually the third version of the methods that I've written, and I have no clue how to fix this short of just scrapping all of the calculation methods and starting over. Can, anyone take a look at my code and point me in the right direction? As a total newbie to Java and OOP in general, I'd appreciate any help I can get VERY MUCH!

Thank you for taking the time to look!

Here is my HighDefDVD class code:

Line 55 of this code is throwing the exception "inventoryValue has private access in DVD class" and I can't figure that out either?

 /**Program: Inventory Program Part 3
 *File: HighDefDVD.java
 *Summary: inherits DVD class, retaining all its attributes and methods,
 *and contains a method that sets, retrieves the high-def dvd's format, a method to calculate a restocking fee, and two methods that override           * methods in the superclass; one that adds the restocking fee while calculating the inventory value, and one to display the dvd information
**/
 

public class HighDefDVD extends DVD
{//begin class HighDefDVD, subclass to superclass DVD
    
    //declare instance variables
    private String discFormat;//format of high-def dvd
    private final double fivePercent = .05;//used to compute restocking fee
    private double restockingFee;//restocking fee for high-def dvd
    private double newInventoryValue;//inventory value including restocking fee
    
    public HighDefDVD(){}//declare empty constructor
    
    public HighDefDVD(String dvdTitleIn, int itemNumberIn, double unitPriceIn, int unitsInStockIn, String discFormatIn)
    {//constructor to initialize class instance variable
        
        super(dvdTitleIn, itemNumberIn, unitPriceIn, unitsInStockIn);//pass to superclass constructor
        this.discFormat = discFormatIn;//set format of HighDefDVD
        
    }//end constructor
    
    public void setDiscFormat(String discFormatIn)
    {//begin method to set discFormat
        this.discFormat = discFormatIn;
    }//end set discFormat method
    
    public String getDiscFormat()
    {//begin method to retrieve discFormat value
        return discFormat;
    }//end get discFormat method
    
    public void setRestockingFee(double restockingFeeIn)
    {//begin setRestockingFee method
        this.restockingFee = restockingFeeIn;
    }//end set method  
    
    public double getRestockingFee()
    {//begin getRestockingFee method
        return restockingFee;    
    }//end getRestockingFee        
    
    public double calculateRestockingFee(double unitPriceIn)
    {//begin method to calculate restocking fee
        return (fivePercent * unitPriceIn);//calculates fee    
    }//end calculate restocking fee method
    
    @Override
        public void calculateInventoryValue (double unitPrice, int unitsInStock)
    {//method to calculate inventoryValue
        this.inventoryValue = super.getInventoryValue() + (restockingFee * unitsInStock);
    }// end calculate inventory value method
    
    public void setNewInventoryValue(double getUnitPriceIn, int getUnitsInStockIn, double fivePercentIn)
    {//begin setNewInventoryValue method
        this.newInventoryValue = super.getInventoryValue() + (restockingFee * super.getUnitsInStock());
    }//end setNewInventoryValue method       
    
    public double getNewInventoryValue()
    {//begin getNewInventoryValue method
        return this.newInventoryValue;
    }//end getNewInventoryValue method 
    
    @Override
        public void displayDVDInformation()
    {//overrides displayDVDInformation method in superclass DVD
        System.out.println("Dvd Title: " + this.getDvdTitle());//displays title of dvd unit
        System.out.println("Item Number: " + this.getItemNumber());//displays item number of dvd unit
        System.out.println("Unit Price: " + nf.format(this.getUnitPrice()));//displays price per DVD unit
        System.out.println("Number of units in stock: " + this.getUnitsInStock());//displays total number of DVD units in stock
        System.out.println("Product Inventory value: " + nf.format(newInventoryValue));//displays the inventory value of units in stock
        System.out.println("High Definition Format: " + this.getDiscFormat());//displays HighDefDVD format
        System.out.println("Restocking fee: " + nf.format(this.calculateRestockingFee(this.getUnitPrice())));//retrieves and passes unitPrice and unitsInStock to calculateRestockingFee method and displays restocking fee
    }//end displayDVDInformation override
    
}//end class HighDefDVD

  


This is the code for my DVD class:
 /**Program: Inventory Program Part 3
 *File: DVD.java
 * Summary: Sets, retrieves DVD product information including, name, item number, unit price, units in stock, inventory value 
 */

import java.text.NumberFormat;// import Java package used to format currency

public class DVD 
{//begin class DVD
    //declare instance variables
    private String dvdTitle;//title of dvd
    private int itemNumber;//item number of dvd
    private double unitPrice;//price per unit
    private int unitsInStock;//number of units on hand
    private double inventoryValue;//value of units on hand
    private static double totalInventoryValue;//total value of all inventory items
   
    NumberFormat nf = NumberFormat.getCurrencyInstance();//instantiate currency format object 
   
    public DVD(){ }//declare empty constructor
    
    public DVD(String dvdTitleIn, int itemNumberIn, double unitPriceIn, int unitsInStockIn)
    {//declare DVD constructor
        this.dvdTitle = dvdTitleIn;
        this.itemNumber = itemNumberIn;
        this.unitPrice = unitPriceIn;
        this.unitsInStock = unitsInStockIn;                
    }//end DVD constructor
    
    public void setDvdTitle(String dvdTitleIn)
    {//set dvdTitle method
        this.dvdTitle = dvdTitleIn;    
    }// end set dvdTitle method
    
    public String getDvdTitle()
    {//method to return dvdTitle
        return this.dvdTitle;
    }//end return dvdTitle method
    
    public void setItemNumber(int itemNumberIn)
    {//set itemNumber method
        this.itemNumber = itemNumberIn;    
    }//end set itemNumber method
    
    public int getItemNumber()
    {//method to return movieTitle value
        return this.itemNumber;
    }//end return itemNumber method
    
    public void setUnitPrice(double unitPriceIn)
    {//set unitPrice method
        this.unitPrice = unitPriceIn;    
    }//end set itemNumber method
    
    public double getUnitPrice()
    {//method to return unitPrice value
        return this.unitPrice;
    }//end return unitPrice method
    
    public void setUnitsInStock (int unitsInStockIn)
    {//set unitsInStock method
        this.unitsInStock = unitsInStockIn;    
    }//end set inStock method
    
    public int getUnitsInStock()
    {//method to return inStock value
        return this.unitsInStock;
    }// end return inStock method
    
    public void calculateInventoryValue (double unitPrice, int unitsInStock)
    {//method to calculate inventoryValue
        this.inventoryValue = unitPrice*unitsInStock;
    }// end calculate inventory value method
    
    public double getInventoryValue()
    {//method to return inventory value
        return inventoryValue;
    }//end return inventoryValue method
                  
    public void calculateTotalInventoryValue(double inInventoryValue)
    {//method to calculate value of all inventory
        totalInventoryValue = totalInventoryValue + inInventoryValue;
    }//end calculate total inventory value method
    
    public double getTotalInventoryValue()
    {//method to return totalInventoryValue
        return totalInventoryValue;    
    }//end return totalInventoryValue method
        
    public void displayDVDInformation()
    {//method to display DVD information
        System.out.println("Dvd Title: " + dvdTitle);//displays title of dvd unit
        System.out.println("Item Number: " + itemNumber);//displays item number of dvd unit
        System.out.println("Unit Price: " + nf.format(unitPrice));//displays price per DVD unit
        System.out.println("Number of units in stock: " + unitsInStock);//displays total number of DVD units in stock
        System.out.println("Product Inventory value: " + nf.format(inventoryValue));//displays the inventory value of units in stock
        
    }//end displayDVDInformation method
    
    public static void sortByName(DVD dvd[])
      {    // sort the person by name
            DVD temp;
            for(int j = dvd.length - 2; j >= 0; j--)
                  for(int i = 0 ; i <= j ; i++)
                if(dvd[ i ].getDvdTitle().compareTo(dvd[ i + 1 ].getDvdTitle())>0)
                {
                     temp=dvd[ i ];
                     dvd[ i ] = dvd[ i + 1 ];
                     dvd[ i + 1 ] = temp;
                 }
      }  // end sortByName method
   
   
}//end class DVD
 


And finally, here is the code for my inventory program class:
 /**Program: Inventory Program Part 3
 *File: InventoryProgram.java
 *Summary: Creates an array of highDefDVD objects for inventory purposes
 */

public class InventoryProgram 
{//begin InventoryProgram class
    
    public static void main(String[] args) 
    {//begin main method
        
        final int Array_Length = 5;//declare constant to set array length
        HighDefDVD highDefDVD[] = new HighDefDVD[Array_Length];//create an array of 5 HighDefDVD objects
        
        HighDefDVD highDefDVD1 = new HighDefDVD("How To Train Your Dragon",1101, 24.99, 10, "BluRay");//creates class instance How To Train Your Dragon & initializes instance variables
              highDefDVD1.calculateInventoryValue(highDefDVD1.getUnitPrice(), highDefDVD1.getUnitsInStock());//method call to calculate inventory value for object How To Train Your Dragon
              highDefDVD1.calculateTotalInventoryValue(highDefDVD1.getInventoryValue());//method to calculate total inventory value
              highDefDVD1.calculateRestockingFee(highDefDVD1.getUnitPrice());//method call to calculate restocking fee
        HighDefDVD highDefDVD2 = new HighDefDVD("Bambi", 1102, 21.99, 15, "BluRay");//creates class instance Bambi & initializes instance variables
              highDefDVD2.calculateInventoryValue(highDefDVD2.getUnitPrice(), highDefDVD2.getUnitsInStock());//method call to pass inventory value for object Bambi
              highDefDVD2.calculateTotalInventoryValue(highDefDVD2.getInventoryValue());//method to calculate total inventory value
              highDefDVD2.calculateRestockingFee(highDefDVD2.getUnitPrice());//method call to calculate restocking fee
        HighDefDVD highDefDVD3 = new HighDefDVD("Rango", 1103, 19.99, 50, "BluRay");//creates class instance Rango & initializes instance variables
              highDefDVD3.calculateInventoryValue(highDefDVD3.getUnitPrice(), highDefDVD3.getUnitsInStock());//method call to pass inventory value for object Rango
              highDefDVD3.calculateTotalInventoryValue(highDefDVD3.getInventoryValue());//method to calculate total inventory value
              highDefDVD3.calculateRestockingFee(highDefDVD3.getUnitPrice());//method call to calculate restocking fee
        HighDefDVD highDefDVD4 = new HighDefDVD("Tangled", 1104, 24.99, 25, "BluRay");//creates class instance Tangled & initializes instance variables
              highDefDVD4.calculateInventoryValue(highDefDVD4.getUnitPrice(), highDefDVD4.getUnitsInStock());//method call to pass inventory value for object Tangled 
              highDefDVD4.calculateTotalInventoryValue(highDefDVD4.getInventoryValue());//method to calcultate total inventory value
              highDefDVD4.calculateRestockingFee(highDefDVD4.getUnitPrice());//method call to calculate restocking fee
        HighDefDVD highDefDVD5 = new HighDefDVD("UP", 1105, 19.99, 20, "BluRay");//creates class instance UP & initializes instance variables
              highDefDVD5.calculateInventoryValue(highDefDVD5.getUnitPrice(), highDefDVD5.getUnitsInStock());//method call to pass inventory value for object UP
              highDefDVD5.calculateTotalInventoryValue(highDefDVD5.getInventoryValue());//method to calculate total inventory value
              highDefDVD5.calculateRestockingFee(highDefDVD5.getUnitPrice());//method call to calculate restocking fee
        
        highDefDVD[0] = highDefDVD1;//adds How To Train Your Dragon object to the array
        highDefDVD[1] = highDefDVD2;//adds Bambi object to the array
        highDefDVD[2] = highDefDVD3;//adds Rango object to the array
        highDefDVD[3] = highDefDVD4;//adds Tangled object to the array
        highDefDVD[4] = highDefDVD5;//adds UP object to the array
        
        HighDefDVD.sortByName(highDefDVD);//sorts the array by name
        System.out.println();//prints a blank line for readability
           
            for (int i = 0; i < highDefDVD.length; i++)
            {//loop to display inventory of HighDefDVD objects
                  highDefDVD[ i ].displayDVDInformation();//method call to display output of HighDefDVD object inventory
                  System.out.println();//prints a blank line for readability
                  
                  if (i == 4)
                  {//loop to output total inventory value    
                      System.out.println("Total Inventory Value: " + highDefDVD1.nf.format(highDefDVD[i].getTotalInventoryValue()));//outputs total inventory value to display
                  }//end if loop        
                  System.out.println();//prints a blank line for readability
            }//end for loop
        
    }//end main method    
    
}//end class InventoryProgram class
 


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with override methods in Java....my code doesn't work

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Need help with override methods in Java....my code doesn't work

Posted 08 October 2011 - 09:59 PM

Moved to Java. Please do not post help questions in the tutorials section.

You cannot access private elements outside of a class, including in its child classes. You may consider adding a setter method for this instance field in the DVD class, and you can invoke the setter method in the DVD subclass.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1