Hello I have an assignment that is due and am in need of some dire help. I would appreciate all the help that I can get. Please bear with me. I am new to Java and Daniweb. I have to create a java application that displays the product number, product name, amount in stock, price per unit, and the total amount of inventory. My application must include a class that hold the product number, product name, amount in stock, and the price per unit. By the way my product is cars. So far I have created two files (Cars.java and CarsInventory.java). I am not sure if this is even right. Am I going in the right direction? Could I have incorporated all of the information into one file? Next, I am not sure how to get both of the files to work together. In addition, when I run and compile each file separately I come up with the following error messages:

ERROR:
Cars.java: 19: missing return statement
} // end four-argument constructor

public class Cars 
{
     private int prodNumber; // product number
     private String prodName; // product name
     private int unitsTotal; // total units in stock
     private double unitPrice; // price per unit 

     // initialize four-argument constructor
     public int Cars( int number, String name, int total, double price)
     {
          prodNumber = number;
          prodName = name;
          setUnitsTotal (total); // validate and store total of cars
          setUnitPrice (price); // validate and store price per car
         
      } // end four-argument constructor        
     
     public void setProdNumber( int number )
     {
          prodNumber = number;
     } 
     public int getProdNumber()
     {
          return prodNumber;
     } 
     public void setProdName( String name)
     {
          prodName = name;
     } 
     public String getProdName()
     {
          return prodName;
     } 
     public void setUnitsTotal( int total )
     {
          unitsTotal = total;    
     } 
     public int getUnitsTotal()
     {
          return unitsTotal;
     }
     public void setUnitPrice( double price )
     {
          unitPrice = price;
     }
     public double getUnitPrice()
     { 
          return unitPrice;
     } 

     // calculate value of total inventory
     public double inventory()
     {
          return unitsTotal * unitPrice;
     } // end method value
     
} // end class Cars

ERROR:
CarsInventory.java: 9: cannot find symbol
symbol: class product
location: Class CarsInventory
Cars product = new product

public class  CarsInventory
{
     public static void main( String args [ ])
     {
          // instantiate cars object
          Cars product = new Cars(
               2479, "Dodge Viper GTS", 20, 65000);

          // get car information
          System.out.println(
               "LNF New and Used Car Inventory Program");
          System.out.printf( "%s %s\n", "Product Number", 
               product.getProdNumber() );
          System.out.printf( "%s %s\n", "Product Name",
               product.getProdName() );
           System.out.printf( "%s %.2f\n", "Total Units in Stock",
               product.getUnitsTotal() );
           System.out.printf( "%s %.2f\n", "Price Per Unit",
               product.getUnitPrice() );

     } // end main method

} // end class CarsInventory

Hi,

First of all, you declared the constructor of class Cars with a return int type, and you should know from your java study that the constructor MUST NOT return any values even void. So, the proper form of the constructor will be like this one public Cars( int number, String name, int total, double price) and continue the rest of constructor definition.


The second thing is your design is good, but with one pitfall which is HOW you're going to save this data about Cars which represented by Cars class, you could use a simple database file to write these info. Or you might use serialization on Cars class.

I hope this benefits you. Happy coding :)

[EL-Prince]

Hi,

First of all, you declared the constructor of class Cars with a return int type, and you should know from your java study that the constructor MUST NOT return any values even void. So, the proper form of the constructor will be like this one public Cars( int number, String name, int total, double price) and continue the rest of constructor definition.


The second thing is your design is good, but with one pitfall which is HOW you're going to save this data about Cars which represented by Cars class, you could use a simple database file to write these info. Or you might use serialization on Cars class.

I hope this benefits you. Happy coding :)

[EL-Prince]

Hello and thanks for your help and positive feedback. That was an oversight on my part in terms of the constructor and the return type. My updated code for my Cars class is below. I have taken it out, but do not understand the second part of your response. I am new to the Java world, so please be patient with me. I have tried to find resources on serialization, but still don't quite understand how to save my Cars class. I thought that I could combine both classes into one file, but still have the same issue. I do not know how to get it to work, so that I can see the information to be displayed.

public class Cars 
{
     private int prodNumber; // product number
     private String prodName; // product name
     private int unitsTotal; // total units in stock
     private double unitPrice; // price per unit 

     // initialize four-argument constructor
     public Cars( int number, String name, int total, double price)
     {
          prodNumber = number;
          prodName = name;
          setUnitsTotal (total); // validate and store total of cars
          setUnitPrice (price); // validate and store price per car
         
      } // end four-argument constructor        
     
     public void setProdNumber( int number )
     {
          prodNumber = number;
     } 
     public int getProdNumber()
     {
          return prodNumber;
     } 
     public void setProdName( String name)
     {
          prodName = name;
     } 
     public String getProdName()
     {
          return prodName;
     } 
     public void setUnitsTotal( int total )
     {
          unitsTotal = total;    
     } 
     public int getUnitsTotal()
     {
          return unitsTotal;
     }
     public void setUnitPrice( double price )
     {
          unitPrice = price;
     }
     public double getUnitPrice()
     { 
          return unitPrice;
     } 

     // calculate value of total inventory
     public double inventory()
     {
          return unitsTotal * unitPrice;
     } // end method value
     
} // end class Cars

Hi, so I compiled your code and got everything to work. You can keep the two files separate, that's fine. I'm not sure why you're getting the constructor error. Do you have the two files in the same directory?

The only problem I found was how you were formatting the printf statements. Total units in stock is an integer, so that should be formatted with %d, and price per unit should be formatted with %g.

Those were the only errors I had, otherwise, your code works fine.

Hi, so I compiled your code and got everything to work. You can keep the two files separate, that's fine. I'm not sure why you're getting the constructor error. Do you have the two files in the same directory?

The only problem I found was how you were formatting the printf statements. Total units in stock is an integer, so that should be formatted with %d, and price per unit should be formatted with %g.

Those were the only errors I had, otherwise, your code works fine.

Hello and thanks for the feedback. The two files are in the same directory. They both are listed (in addition to some others) when I enter in the "dir" prompt from the command prompt window. I made the changes that you recommended and it worked. I was so happy until I noticed that the total inventory value did not display. It feels like I am back where I started. Here are the errors and code that I am working with. Any help would be gladly appreciated.

Errors:
Java: 55: missing method body or declare abstract
public double getValue();
Java: 57: return outside method
return unitsTotal * unitPrice;

// 
public class Cars 
{
     private int prodNumber; // product number
     private String prodName; // product name
     private int unitsTotal; // total units in stock
     private double unitPrice; // price per unit 
     private double totalInventory; // amount of total inventory
    
     // initialize four-argument constructor
     public Cars( int number, String name, int total, double price)
     {
          prodNumber = number;
          prodName = name;
          setUnitsTotal (total); // validate and store total of cars
          setUnitPrice (price); // validate and store price per car
         
      } // end four-argument constructor        
     
     public void setProdNumber( int number )
     {
          prodNumber = number;
     } 
     public int getProdNumber()
     {
          return prodNumber;
     } 
     public void setProdName( String name)
     {
          prodName = name;
     } 
     public String getProdName()
     {
          return prodName;
     } 
     public void setUnitsTotal( int total )
     {
          unitsTotal = ( total = 0 );
     } 
     public int getUnitsTotal()
     {
          return unitsTotal;
     }
     public void setUnitPrice( double price )
     {
          unitPrice = ( price = 0.0 );
     }
    
     public double getUnitPrice()
     {
           return unitPrice;
     }
     public double getValue();
     {
          return unitsTotal * unitPrice;
     }
        
} // end class Cars
public class  CarsInventory
{
     public static void main( String args [ ])
     {
          // instantiate cars object
          Cars product = new Cars(
               2479, "Dodge Viper GTS", 20, 65000);

          // get car information
          System.out.println(
               "LNF New and Used Car Inventory Program");
          System.out.printf( "%s %d\n", "Product Number:", 
               product.getProdNumber() );
          System.out.printf( "%s %s\n", "Product Name:",
               product.getProdName() );
           System.out.printf( "%s %d\n", "Total Units in Stock:",
               product.getUnitsTotal() );
           System.out.printf( "%s $%g\n", "Price Per Unit:",
               product.getUnitPrice() );

     } // end main method

} // end class CarsInventory

53. public double getValue();

That semi-colon is your problem. Remove it, and it should work.

53. public double getValue();

That semi-colon is your problem. Remove it, and it should work.

Thanks. You are my java angel :). Thanks for your time. The smallest mistake will cause an error. I have removed the semicolon and the program came right up like you said it would. The name of the program, product name, product number, total units and the price per unit displays, but the total amount of the inventory is not displaying. I do not understand why it isn't.

public class Cars 
{
     private int prodNumber; // product number
     private String prodName; // product name
     private int unitsTotal; // total units in stock
     private double unitPrice; // price per unit 
     private double totalInventory; // amount of total inventory
    
     // initialize four-argument constructor
     public Cars( int number, String name, int total, double price)
     {
          prodNumber = number;
          prodName = name;
          setUnitsTotal (total); // validate and store total of cars
          setUnitPrice (price); // validate and store price per car
         
      } // end four-argument constructor        
     
     public void setProdNumber( int number )
     {
          prodNumber = number;
     } 
     public int getProdNumber()
     {
          return prodNumber;
     } 
     public void setProdName( String name)
     {
          prodName = name;
     } 
     public String getProdName()
     {
          return prodName;
     } 
     public void setUnitsTotal( int total )
     {
          unitsTotal = ( total = 20 );
     } 
     public int getUnitsTotal()
     {
          return unitsTotal;
     }
     public void setUnitPrice( double price )
     {
          unitPrice = ( price = 65000.0 );
     }
    
     public double getUnitPrice()
     {
           return unitPrice;
     }
     public double getValue()
     {
          return unitsTotal * unitPrice;
     }
        
} // end class Cars
public class  CarsInventory
{
     public static void main( String args [ ])
     {
          // instantiate cars object
          Cars product = new Cars(
               2479, "Dodge Viper GTS", 20, 65000);

          // get car information
          System.out.println(
               "LNF New and Used Car Inventory Program");
          System.out.printf( "%s %d\n", "Product Number:", 
               product.getProdNumber() );
          System.out.printf( "%s %s\n", "Product Name:",
               product.getProdName() );
           System.out.printf( "%s %d\n", "Total Units in Stock:",
               product.getUnitsTotal() );
           System.out.printf( "%s $%g\n", "Price Per Unit:",
               product.getUnitPrice() );

     } // end main method

} // end class CarsInventory

53. public double getValue();

That semi-colon is your problem. Remove it, and it should work.

Hello again. I am sending this response to let you know that I figured it out. I had to add a System.out.printf statement for the total inventory amount to display. It now display as it should. Thanks for your help again. It was greatly appreciated.

I don't think you're printing the total amount of the inventory in your main method. I doesn't seem like you initialize that variable in your Cars class at all either.

I don't think you're printing the total amount of the inventory in your main method. I doesn't seem like you initialize that variable in your Cars class at all either.

The message displays as it should now. Thanks for your help. I appreciate it.

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.