Link to home
Start Free TrialLog in
Avatar of kpu8
kpu8Flag for United States of America

asked on

Handful of simple errors in Inventory and DVD code

I'm getting a handful of errors in my simple Java program and don't know why - what am I doing wrong?

import java.util.Scanner;          (error: Incorrect Package, unused import)
import java.util.ArrayList;        (error: unused import)

package Inventory;                    (error: class, interface, or enum expected)

/**
 *
 * @author
 */
public class InventoryMain
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
         Dvd[] aDvdArray;    // declares an array of Dvd objects
         aDvdArray = new Dvd[5];        // allocates memory for 5 Dvds

         // initialize array elements
         aDvdArray[0] = new Dvd("Quantum of Solace", 10, 24.99, 1);
         aDvdArray[1] = new Dvd("Slumdog Millionaire", 5, 25.99, 2);
         aDvdArray[2] = new Dvd("Iron Man", 25, 20.99, 3);
         aDvdArray[3] = new Dvd("The Dark Knight", 50, 23.99, 4);
        aDvdArray[4] = new Dvd("Gran Torino", 15, 19.99, 5);


         aDvdArray = SortByProductName(aDvdArray);  (error: non-static method SortByProductName(Dvd[]) cannot be referenced from static context)
         // Print DVD inventory One Product at a time
         System.out.println("---------------------------------------");
         for(int i = 0; i < aDvdArray.length; i++)
         {
             System.out.println("Item Number: " + aDvdArray[i].getDvdItem());
             System.out.println("Product Name: " + aDvdArray[i].getdvdTitle());
        System.out.println("Units In Stock: " + aDvdArray[i].getDvdStock());
        System.out.println("Unit Price: " + aDvdArray[i].getDvdPrice());
        System.out.println("Inventory Value: " + aDvdArray[i].value() );
        System.out.println("---------------------------------------");
         }

         System.out.println("Entire Inventory Value: " + CalculateInventoryValue(aDvdArray));   (error: non-static method  CalculateInventoryValue(Dvd[]) cannot be referenced from static context)
    }

    // method to calculate value of entire inventory
    public double CalculateInventoryValue(Dvd[] dvds)
    {
      double inventoryValue = 0;
      for(int i = 0; i < dvds.length; i++)
      {
        inventoryValue = inventoryValue + dvds[i].getDvdPrice();
      }
      return inventoryValue;
    }

    // method to sort dvd array by product name
    public Dvd[] SortByProductName(Dvd[] dvds)
    {
     Dvd aDvd;
     if (dvds.length == 1)
     {
         return dvds;
     }
     for (int i = 0; i < dvds.length; i++)
     {
      for (int j = i + 1; j < dvds.length; j++)
      {
        if(dvds[i].getdvdTitle().compareTo(dvds[j].getdvdTitle()) > 0)
        {
         aDvd = dvds[i];
         dvds[i] = dvds[j];
         dvds[j] = aDvd;
        }
      }
     }
     return dvds;
    }
}

public class Dvd extends Object  (error: class Dvd is public, should be declared in a file named Dvd.java)

{

            private String dvdTitle;
            private double dvdStock;
            private double dvdPrice;
            private double dvdItem;
            public Dvd( String title, double stock, double price, double item )

                        {

                                    dvdTitle = title;
                                    dvdStock = stock;
                                    dvdPrice = price;
                                    dvdItem = item;
                        } //end constructor

            //set DVD name
            public void setdvdTitle( String title )
            {
                        dvdTitle = title;
            } //end method setdvdTitle

            //return dvd title
            public String getdvdTitle()
            {
                        return dvdTitle;
            } //end method getdvdTitle

            //set Dvd stock
            public void setDvdStock( double stock)
            {
                        dvdStock = ( stock < 0.0 ) ? 0.0 : stock;
            } //end method setDvdStock

            //return dvd stock
            public double getDvdStock()
            {
                        return dvdStock;
            } //end method getDvdStock

            public void setDvdPrice( double price )
            {
                        dvdPrice = ( price < 0.0 ) ? 0.0 : price;
            } //end method SetDvdPrice

            //return dvd price
            public double getDvdPrice()
            {
                        return dvdPrice;
            } //end method get Dvd Price

            public void setDvdItem( double item )
            {
                        dvdItem = ( item < 0.0) ? 0.0 : item;
            } //end method set dvd Item

            //return dvd item

            public double getDvdItem()
            {
                        return dvdItem;
            } //end method getDvdItem


            // calculate inventory value
            public double value()
            {
                        return dvdPrice * dvdStock;

            } //end method value


  } //end class Dvd


This is the result when i run it:

run:
Exception in thread "main" java.lang.ExceptionInInitializerError
        at InventoryMain.main(InventoryMain.java:22)
Caused by: java.lang.RuntimeException: Uncompilable source code - class Dvd is public, should be declared in a file named Dvd.java
        at Dvd.<clinit>(InventoryMain.java:80)
        ... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

import java.util.Scanner;          (error: Incorrect Package, unused import)
import java.util.ArrayList;        (error: unused import)
 
package Inventory;                    (error: class, interface, or enum expected)
 
/**
 *
 * @author 
 */
public class InventoryMain
{
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
         Dvd[] aDvdArray;    // declares an array of Dvd objects
         aDvdArray = new Dvd[5];        // allocates memory for 5 Dvds
 
         // initialize array elements
         aDvdArray[0] = new Dvd("Quantum of Solace", 10, 24.99, 1);
         aDvdArray[1] = new Dvd("Slumdog Millionaire", 5, 25.99, 2);
         aDvdArray[2] = new Dvd("Iron Man", 25, 20.99, 3);
         aDvdArray[3] = new Dvd("The Dark Knight", 50, 23.99, 4);
        aDvdArray[4] = new Dvd("Gran Torino", 15, 19.99, 5);
 
 
         aDvdArray = SortByProductName(aDvdArray);  (error: non-static method SortByProductName(Dvd[]) cannot be referenced from static context)
         // Print DVD inventory One Product at a time
         System.out.println("---------------------------------------");
         for(int i = 0; i < aDvdArray.length; i++)
         {
             System.out.println("Item Number: " + aDvdArray[i].getDvdItem());
             System.out.println("Product Name: " + aDvdArray[i].getdvdTitle());
        System.out.println("Units In Stock: " + aDvdArray[i].getDvdStock());
        System.out.println("Unit Price: " + aDvdArray[i].getDvdPrice());
        System.out.println("Inventory Value: " + aDvdArray[i].value() );
        System.out.println("---------------------------------------");
         }
 
         System.out.println("Entire Inventory Value: " + CalculateInventoryValue(aDvdArray));   (error: non-static method  CalculateInventoryValue(Dvd[]) cannot be referenced from static context)
    }
 
    // method to calculate value of entire inventory
    public double CalculateInventoryValue(Dvd[] dvds)
    {
      double inventoryValue = 0;
      for(int i = 0; i < dvds.length; i++)
      {
        inventoryValue = inventoryValue + dvds[i].getDvdPrice();
      }
      return inventoryValue;
    }
 
    // method to sort dvd array by product name
    public Dvd[] SortByProductName(Dvd[] dvds)
    {
     Dvd aDvd;
     if (dvds.length == 1)
     {
         return dvds;
     }
     for (int i = 0; i < dvds.length; i++)
     {
      for (int j = i + 1; j < dvds.length; j++)
      {
        if(dvds[i].getdvdTitle().compareTo(dvds[j].getdvdTitle()) > 0)
        {
         aDvd = dvds[i];
         dvds[i] = dvds[j];
         dvds[j] = aDvd;
        }
      }
     }
     return dvds;
    }
}
 
public class Dvd extends Object  (error: class Dvd is public, should be declared in a file named Dvd.java)
 
{
 
            private String dvdTitle;
            private double dvdStock;
            private double dvdPrice;
            private double dvdItem;
            public Dvd( String title, double stock, double price, double item )
 
                        {
 
                                    dvdTitle = title;
                                    dvdStock = stock;
                                    dvdPrice = price;
                                    dvdItem = item;
                        } //end constructor
 
            //set DVD name
            public void setdvdTitle( String title )
            {
                        dvdTitle = title;
            } //end method setdvdTitle
 
            //return dvd title
            public String getdvdTitle()
            {
                        return dvdTitle;
            } //end method getdvdTitle
 
            //set Dvd stock
            public void setDvdStock( double stock)
            {
                        dvdStock = ( stock < 0.0 ) ? 0.0 : stock;
            } //end method setDvdStock
 
            //return dvd stock
            public double getDvdStock()
            {
                        return dvdStock;
            } //end method getDvdStock
 
            public void setDvdPrice( double price )
            {
                        dvdPrice = ( price < 0.0 ) ? 0.0 : price;
            } //end method SetDvdPrice
 
            //return dvd price
            public double getDvdPrice()
            {
                        return dvdPrice;
            } //end method get Dvd Price
 
            public void setDvdItem( double item )
            {
                        dvdItem = ( item < 0.0) ? 0.0 : item;
            } //end method set dvd Item
 
            //return dvd item
 
            public double getDvdItem()
            {
                        return dvdItem;
            } //end method getDvdItem
 
 
            // calculate inventory value
            public double value()
            {
                        return dvdPrice * dvdStock;
 
            } //end method value
 
 
  } //end class Dvd
 
 
This is the result when i run it:
 
run:
Exception in thread "main" java.lang.ExceptionInInitializerError
        at InventoryMain.main(InventoryMain.java:22)
Caused by: java.lang.RuntimeException: Uncompilable source code - class Dvd is public, should be declared in a file named Dvd.java
        at Dvd.<clinit>(InventoryMain.java:80)
        ... 1 more
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

Open in new window

Avatar of basav_com
basav_com
Flag of India image

Here is the corrected code:
Check my NEXT post for explanation.

Output:
---------------------------------------
Item Number: 5.0
Product Name: Gran Torino
Units In Stock: 15.0
Unit Price: 19.99
Inventory Value: 299.84999999999997
---------------------------------------
Item Number: 3.0
Product Name: Iron Man
Units In Stock: 25.0
Unit Price: 20.99
Inventory Value: 524.75
---------------------------------------
Item Number: 1.0
Product Name: Quantum of Solace
Units In Stock: 10.0
Unit Price: 24.99
Inventory Value: 249.89999999999998
---------------------------------------
Item Number: 2.0
Product Name: Slumdog Millionaire
Units In Stock: 5.0
Unit Price: 25.99
Inventory Value: 129.95
---------------------------------------
Item Number: 4.0
Product Name: The Dark Knight
Units In Stock: 50.0
Unit Price: 23.99
Inventory Value: 1199.5
---------------------------------------
Entire Inventory Value: 115.94999999999999


package Inventory; 
 
import java.util.Scanner;          
import java.util.ArrayList;       
 
                  
 
/**
 *
 * @author 
 */
public class InventoryMain
{
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
         Dvd[] aDvdArray;    // declares an array of Dvd objects
         aDvdArray = new Dvd[5];        // allocates memory for 5 Dvds
 
         // initialize array elements
         aDvdArray[0] = new Dvd("Quantum of Solace", 10, 24.99, 1);
         aDvdArray[1] = new Dvd("Slumdog Millionaire", 5, 25.99, 2);
         aDvdArray[2] = new Dvd("Iron Man", 25, 20.99, 3);
         aDvdArray[3] = new Dvd("The Dark Knight", 50, 23.99, 4);
        aDvdArray[4] = new Dvd("Gran Torino", 15, 19.99, 5);
 
 
         aDvdArray = SortByProductName(aDvdArray);  
         // Print DVD inventory One Product at a time
         System.out.println("---------------------------------------");
         for(int i = 0; i < aDvdArray.length; i++)
         {
             System.out.println("Item Number: " + aDvdArray[i].getDvdItem());
             System.out.println("Product Name: " + aDvdArray[i].getdvdTitle());
        System.out.println("Units In Stock: " + aDvdArray[i].getDvdStock());
        System.out.println("Unit Price: " + aDvdArray[i].getDvdPrice());
        System.out.println("Inventory Value: " + aDvdArray[i].value() );
        System.out.println("---------------------------------------");
         }
 
         System.out.println("Entire Inventory Value: " + CalculateInventoryValue(aDvdArray));   
    }
 
    // method to calculate value of entire inventory
    public static double CalculateInventoryValue(Dvd[] dvds)
    {
      double inventoryValue = 0;
      for(int i = 0; i < dvds.length; i++)
      {
        inventoryValue = inventoryValue + dvds[i].getDvdPrice();
      }
      return inventoryValue;
    }
 
    // method to sort dvd array by product name
    public static Dvd[] SortByProductName(Dvd[] dvds)
    {
     Dvd aDvd;
     if (dvds.length == 1)
     {
         return dvds;
     }
     for (int i = 0; i < dvds.length; i++)
     {
      for (int j = i + 1; j < dvds.length; j++)
      {
        if(dvds[i].getdvdTitle().compareTo(dvds[j].getdvdTitle()) > 0)
        {
         aDvd = dvds[i];
         dvds[i] = dvds[j];
         dvds[j] = aDvd;
        }
      }
     }
     return dvds;
    }
}
 
class Dvd extends Object 
 
{
 
            private String dvdTitle;
            private double dvdStock;
            private double dvdPrice;
            private double dvdItem;
            public Dvd( String title, double stock, double price, double item )
 
                        {
 
                                    dvdTitle = title;
                                    dvdStock = stock;
                                    dvdPrice = price;
                                    dvdItem = item;
                        } //end constructor
 
            //set DVD name
            public void setdvdTitle( String title )
            {
                        dvdTitle = title;
            } //end method setdvdTitle
 
            //return dvd title
            public String getdvdTitle()
            {
                        return dvdTitle;
            } //end method getdvdTitle
 
            //set Dvd stock
            public void setDvdStock( double stock)
            {
                        dvdStock = ( stock < 0.0 ) ? 0.0 : stock;
            } //end method setDvdStock
 
            //return dvd stock
            public double getDvdStock()
            {
                        return dvdStock;
            } //end method getDvdStock
 
            public void setDvdPrice( double price )
            {
                        dvdPrice = ( price < 0.0 ) ? 0.0 : price;
            } //end method SetDvdPrice
 
            //return dvd price
            public double getDvdPrice()
            {
                        return dvdPrice;
            } //end method get Dvd Price
 
            public void setDvdItem( double item )
            {
                        dvdItem = ( item < 0.0) ? 0.0 : item;
            } //end method set dvd Item
 
            //return dvd item
 
            public double getDvdItem()
            {
                        return dvdItem;
            } //end method getDvdItem
 
 
            // calculate inventory value
            public double value()
            {
                        return dvdPrice * dvdStock;
 
            } //end method value
 
 
  } //end class Dvd
 
 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of basav_com
basav_com
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial