Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Handful of simple errors in Inventory and DVD code

Posted on 2009-04-02
2
Medium Priority
?
830 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:kpu8
  • 2
2 Comments
 
LVL 12

Expert Comment

by:basav_com
ID: 24056390
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

0
 
LVL 12

Accepted Solution

by:
basav_com earned 500 total points
ID: 24056408
1. package Inventory;                    (error: class, interface, or enum expected)   ----> [basav_com]: You need to place the package statement in the first line of the program i.e., before the import statements.

2.  aDvdArray = SortByProductName(aDvdArray);  (error: non-static method SortByProductName(Dvd[]) cannot be referenced from static context)   --->  [basav_com]: main method is the static method. You cann't refer the non-static method(SortByProductName) from static code. Hence I declared the SortByProductName as static.

OR you can create the object of the InventoryMain and call this method.

3. ("Entire Inventory Value: " + CalculateInventoryValue(aDvdArray));   (error: non-static method  CalculateInventoryValue(Dvd[]) cannot be referenced from static context)   ----->  [basav_com]: Same explanation as point 2.

4. public class Dvd extends Object  (error: class Dvd is public, should be declared in a file named Dvd.java)  -----> [basav_com]: In a java file you can have only 1 public class whose name is equivalent to the name of the file. Remaining classes shouldn't be public. Hence I changed the access specifier of Dvd class.

OR you can create a separate file with filename Dvd and you can move this class into that.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question