I am new to Java, I am six weeks through a nine week class. I am having problems extending a class in my program. My assignment is as follows:

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
 Modify the output to display this additional feature you have chosen and the restocking fee.

My code compiles but does not print the contents of the ExtendDVD class. I have the three classes saved as seperate files. Can someone please help me find determine what is wrong. Thank you.

import java.util.Arrays; 
public class Inventory2 // declares public class inventory 

{ 	

public static void main(String args[]) // starts the program	

  { 
    		
    DVD[] dvd = new DVD[3]; 		
    

		dvd[0] = new DVD(1, "Knocked Up", 2, 17.99, "min100");// declares a new item number, dvd title, quantity, and price

		dvd[1] = new DVD(2, "Napolean Dynamite", 4, 14.99, "100 Min");

		dvd[2] = new DVD(3, "Happy Gilmore", 6, 9.99, "100 Min");

               
              
             

		Inventory2 x = new Inventory2();// declare second instance of inventory to sort
		x.SortDVD(dvd);

                for(int i = 0; i < 3; i++)// loop to call array list and set dvd = i 
        
                
               {
               System.out.println(dvd[i]);// print array i
               System.out.println();  
              
               System.out.println("The item number is " + dvd[i].getItemNumber()); // print item number              
               System.out.println("Product Title is " + dvd[i].getDvdTitle()); //print dvd title
               System.out.println("The number of units in stock is " + dvd[i].getOnHand()); //print on hand quantity
               System.out.println("The price of each DVD is $" + dvd[i].getDvdCost()); //print cost
               System.out.println("The value of this Item is $" + dvd[i].value());  // print value of all on hand

               }              
              
 
               double total = 0;// initiate total inventory as double
             
               for( int counter = 0; counter < dvd.length; counter++ )// calculate total inventory 
               total += dvd[ counter ].value();
               System.out.println(); 
               System.out.println( "The Value of All On Hand Inventory is $" + total );//print the total value of the inventory
             
             
             
              
                
             
 
	}// end main

	public void SortDVD(DVD[] theDVD)// method to sort array dvd 

               {
		for (int index = 0; index < theDVD.length - 1; index++) 
                       
                    {
	             String s1 = theDVD[index].getDvdTitle(); // set title to sort 
		     String s2 = theDVD[index + 1].getDvdTitle();// set title to compare in sort
			if (comparewords(s1, s2)) // compares titles to sort
                       
                    {
		     DVD temp = theDVD[index];// creates temporary instance of dvd
		     theDVD[index] = theDVD[index + 1];
		     theDVD[index + 1] = temp;
		     index = -1;
		    }
		}
	}

	private boolean comparewords(String s1, String s2)// creates boolean and compares the two strings 
            {
	     boolean islarger = false; //continues to sort until true

	     for (int index = 0; index < s1.length(); index++) //loop to determine title length
                     
                    {
		     if (index < s2.length()) 
                       
                    {
		     if (s1.toLowerCase().charAt(index) > s2.toLowerCase().charAt(index)) 
                    {
	             islarger = true;//sort by longer name
	             break;
	            }
			if (s1.toLowerCase().charAt(index) < s2.toLowerCase().charAt(index)) 
                        {
					
		        break;// stop
			}
			}
                        else 
                        { 
		        return true; 
			}
		     }

		return islarger;// return larger moves title higher in the list
	}
        
       
} // end class Inventory2
public class DVD // declares class inventory
{

       
        int itemNumber;// declares item number as in
	String dvdTitle;// declares dvd title as string
	int onHand; // declares on hand as int
	double dvdCost;// declares cost as double
       
	public DVD(int stockNumber, String title, int inStock, double price, String studio) // constructor
	{
		itemNumber = stockNumber;// intitiates stock number, title, instock, and
		// price
		dvdTitle = title;
		onHand = inStock;
		dvdCost = price;

	} 

	// set DVD StockNumber //sets stock number
	public void setItemNumber(int stockNumber)

	{
		itemNumber = stockNumber;

	}

	public int getItemNumber() // class item number

	{
		return itemNumber; // returns item number
	} // end method get Dvd Item

	public void setDvdTitle(String title) // set DVD Title

	{
		dvdTitle = title;
	}

	public String getDvdTitle() // gets and returns DVD Title

	{

		return dvdTitle;
	}

	public void setOnHand(int inStock) // Set on hand

	{
		onHand = inStock;
	}

	public int getOnHand() // gets and returns on hand

	{
		return onHand;
	} //

	public void setDvdCost(double price) // sets dvd cost

	{
		dvdCost = price;
	}

	public double getDvdCost() // gets and returns DVD cost

	{
		return dvdCost;
	}

	public double value() // calculates the value of stock

	{
		return dvdCost * onHand;
	}


        


} // end class DVD
public class ExtendDVD extends DVD

{

    private String time = "";

    public ExtendDVD( int item, String name, int units, double price, String studio )
    
     {
       super( item, name, units, price, studio);
       this.time = studio;
     }

     public String getTime()
       {
        return time;
       }
     
     public void setTime( String time )
       {
        this.time = time;
       }

     public double getInventoryValue()
       {
        return 1.05 * getOnHand() * getDvdCost();
       }
      
     public double fee()
       {
        return 0.05 * getOnHand() * getDvdCost();
       }

     public String tostring()
       {
        return String.format("\nInventory Information\n%s %s\n%s\t %s\n%s\t%d\n%s\t $%, .2f \n%s\t$%, .2f",
                             "Item Number ", getItemNumber(), 
                             "Title ", getDvdTitle(), 
                             "Quantity On Hand ", getOnHand(),
                             "Cost ", getDvdCost(),
                             "Running Time ", getTime(),
                             "Fee ", fee(),
                             "Total Value ", getInventoryValue() );
       }
       
       
                     
}

My code compiles but does not print the contents of the ExtendDVD class. I have the three classes saved as seperate files.

Well just a quick Search of your code revealed that you have never created an Object of ExtendDVD so I guess its logical that nothing from it would be printed.

commented: Beat me to it! +15

I don't see any calls to an ExtendDVD constructor, so perhaps there are no ExtendDVD obejcts to display since none are ever created? I see these objects of type DVD created here:

DVD[] dvd = new DVD[3]; 		
    

		dvd[0] = new DVD(1, "Knocked Up", 2, 17.99, "min100");// declares a new item number, dvd title, quantity, and price

		dvd[1] = new DVD(2, "Napolean Dynamite", 4, 14.99, "100 Min");

		dvd[2] = new DVD(3, "Happy Gilmore", 6, 9.99, "100 Min");

Quick test. Delete the ExtendDVD class and see if the program compiles. If it compiles, that means ExtendDVD isn't necessary.

[edit]
To clarify, if it compiles without the ExtendDVD class, that means that no ExtendDVD objects are created.
[/edit]

Hello Again. Your main never creates any instances of the extended class. Even though the ExtendedDVD class extends DVD they are not the same object. You would have to make a new instance of the ExtendedDVD class:

ExtendedDVD (variablename) = new ExtendedDVD();

When a class Inherits another class it has the following nifty features:

1.) can use functions from the "super" or "parent" class
2.) can "override" functions from the "super" or "parent" class
3.) is considered, as far as java goes, to be the same as its "parent" class.

The third one is pretty useful. Let's say that you have a "person" class. This person class has a method called "walk". You could have a "boy" class and a "girl" class both extend the "person" class, and they could be treated as the same data type by using "person" as the identifier. You could make a Person array (like you made the dvd array) that could hold both boy and girl objects!

This only goes one way though. Java sees this about your inherited classes:

ExtendedDVD = DVD
DVD != ExtendedDVD

This is only from a comparison standpoint though. You still have to make an ExtendedDVD object to use the ExtendedDVD functions that differ from the regular DVD functions. On a side note, a method that is named the same as a method from the parent class but performs a different action is called polymorphism. The tostring() method you created is an example of polymorphism.

I got the Program running as intended. It seems that I need to pay closer attention to details. Thank you again for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.