Results 1 to 2 of 2

Thread: Subclass help

  1. #1
    amystauff is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Subclass help

    I have an assignment for school and cannot seem to get past one part. I cannot seem to call from one class to my subclass. Can you help?

    I have three different classes. Main, Product, and director.

    Java Code:
    public class Main
    {
    
    
        public static void main(String args[] ) // main method begins program execution
        {          // create Scanner to obtain input from command window
    
    
    
            System.out.println( "Welcome to Inventory Program Part 3!" ); // display a welcome message
    
    
            Product[] Product = new Product[50]; // an array of 50 products
    
            Product[0] = new Product( "Resevoir Dogs", 1, 90, 12.99);
            Product[1] = new Product( "Pulp Fiction", 2, 80, 12.99 );
            Product[2] = new Product( "Four Rooms", 3, 40, 10.99 );
            Product[3] = new Product( "Kill Bill 1", 4, 50, 15.99 );
            Product[4] = new Product( "Kill Bill 2", 5, 50, 17.99 );
    
    
            Product[0].showInventory(); // display the inventories one at a time
            Product[1].showInventory();
            Product[2].showInventory();
            Product[3].showInventory();
            Product[4].showInventory();
    
    
            for ( int i = 0; i < args.length; i++ ) // sort products by name
                System.out.println( args[i] + ", " );
    
            double array[] = { 1169.10, 1039.20, 439.60, 799.50, 899.50 };
            double total = 0;
    
    
            for ( int counter = 0; counter < array.length; counter++) // add each element's value to total
                total += array[ counter ];
            System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
    
            System.out.println( "\nThank you for using Inventory Program Part 3!\n" );
    
        } // end method main
    
    } // end class Inventory3
    
    class Product
    {
        static int length;
         public String productName;
         public int productNumber;
         public int productUnits;
         public double productPrice;
    
         //default constructor
         public Product()
         {
             productName = "";
             productNumber = 0;
             productUnits = 0;
             productPrice = 0.00;
         }
    
       //parametized constructor
        public Product(String name, int number, int units, double price)
        {
            this.productName = name;
            this.productNumber = number;
            this.productUnits = units;
            this.productPrice = price;
        }
    
         public void setProductName( String name ) // set products name
    {
        this.productName = name;
    } // end method set products name
    
    
         public String getProductName() // return products name
         {
             return productName;
             } // end method get products name
    
         public void setProductNumber( int number ) // set products number
         {
             this.productNumber = number;
         }    // end method set products number
    
    
         public int getProductNumber() // return products number
    {
        return productNumber;
    } // end method get products number
    
    
         public void setProductUnits( int units ) // set products in stock
         {
             this.productUnits = units;
            } // end method set products units
    
    
         public int getProductUnits() // return products units
         {
             return productUnits;
           } // end method get products units
    
    
         public void setProductPrice( double price ) // set products price
         {
             this.productPrice = price;
           } // end method set products price
    
    
         public double getProductPrice() // return products price
         {
             return productPrice;
          } // end method get products price
    
    
         public double getValue() // calculate products inventory value
         {
             return productUnits * productPrice;
          } // end method products inventory value
    
    
    
         public void showInventory() // display inventory
         {
             System.out.println();// outputs blank line
             System.out.println( "Product Name:  " + productName );
             System.out.println( "Product Number:  " + productNumber );
             System.out.println( "Units in Stock:  " + productUnits );
             System.out.printf( "Unit Price:  $" + productPrice );
    
             director Product = new director( "Resevoir Dogs", 1, 90, 12.99, "Quentin Tarentino" );
    
             System.out.println( "\nDirector:  " + Product.getDirector() );
    
             System.out.printf( "\nInventory value of "+  productName + " is = $%.2f\n", getValue() ); // display the value
    
         } // end display inventory
    
    } // end class products
    
    class director extends Product
    {
    
         private String productDirector; // holds the products director
    
    
         public director( String productName, int productNumber, int productUnits,
              double productPrice, String director ) // five-argument constructor
         {
            Product ( productName, productNumber, productUnits, productPrice );
              productDirector = director;
         } // end five-argument constructor
    
    
         public void setDirector( String director ) // set products director
         {
              this.productDirector = director;
         } // end method set products director
    
    
         public String getDirector() // return products director
         {
             return productDirector;
         } // end method get products director
    
    
      
        @Override
         public double getValue() // add 5% restocking fee
         {
              return getValue()  * .05;
         } // end method return products director
    
    
         public double getRestockingFee() // calculate restocking fee
         {
              return getValue() * .05;
         } //end method calculate restocking fee
    
    
       @Override
         public String toString() //return String representation of productsDirector
         {
              String formatString = "Director:  %s";
              formatString += "Restocking Fee:  $%.2f";
              formatString = String.format( formatString, productDirector,
                   getValue() * 0.05 );
              return( formatString + super.toString() );
         } // end toString()
    
    
       @Override
         public void showInventory() // display inventory
         {
              super.showInventory();
              System.out.println( toString() );
    
    
              System.out.printf( "\nInventory value of " + productName + " is = $%.2f\n",
                   getRestockingFee() ); // Display value plus restocking fee
    
         } // end method display inventory
    
    
    } // end class director
    This is the error that I get.

    Welcome to Inventory Program Part 3!

    Product Name: Resevoir Dogs
    Product Number: 1
    Units in Stock: 90
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
    at director.<init>(director.java:12)
    at Product.showInventory(Product.java:89)
    at Main.main(Main.java:24)
    Unit Price: $12.99Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You shouldn't try to run this until you get it to compile first. So the key is in the error that the compiler is telling you, not this. Hint: you call a super constructor with the keyword "super" not with the name of the super class.

Similar Threads

  1. Object is a Subclass of
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:42 AM
  2. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 04:51 PM
  3. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:03 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM
  5. SubClass problems
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-19-2007, 06:54 PM

Posting Permissions

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