PDA

View Full Version : Help with java programming problem



snowowls
05-05-2007, 07:37 PM
We have to create a Product class and then create and Inventory Test 1 to test the product class.
I am showing no errors in Product class or Inventory Test. When I put in "java InventoryTest1"; it shows name=null; number =0.0, etc.etc.
What I need to know is what I have done or need to change so that name doesn't say null , or is it supposed to?

Here is what I have:
/* File :Inventory1.java
Assignment:Inventory Program Part 1
Name : Doris Chase
Date : 05/04/07
*/

import java.util.Scanner; //required for keyboard input

public class Inventory1


{
public static void main (String args [] )
{

Scanner input = new Scanner (System.in );

Product p = new Product ( );


System.out.printf (p.toString () );

and Product class
/* File : Product.java
Assignment: Inventory Program Part 1
Name : Doris Chase
Date : 05/04/07
*/
public class Product
{
private String name; //the product name.
private double number; //the item number
private double units; //number of available units.
private double price; //the price of each unit.
private double value; //the value of total units
public Product ( ) //Default constructor
{
} //End default constructor.
public Product ( String nameIn, double numberIn, double unitsIn, double priceIn, double valueIn ) //Initialization constructor
{
setName (nameIn);
setNumber (numberIn);
setUnits (unitsIn);
setPrice (priceIn);
setValue (valueIn);
}
public void setName (String nameInput)
{
name = nameInput;
}
public void setNumber (double numberInput)
{
number = numberInput;
}
public void setUnits (double unitsInput)
{
units = unitsInput;
}
public void setPrice (double priceInput)
{
price = priceInput;
}
public void setValue (double valueInput)
{
value = valueInput;
}
public String getName ( )
{
return (name);
} //End method getName
public double getNumber ( )
{
return (numbe)r;
} //End method getNumber
public double getUnits ( )
{
return (units) ; //End method getUnits
}
public double getPrice ( )
{
return (price);
} //End method getPrice
public double getstockValue ( )
{
return (units * price);
} //End method stockValue
public String toString ( ) //Returns a formatted String for output purposes //toString () Method
{
String formatString = "itemName : %s\n";
formatString += "itemNumber : %d\n";
formatString += "totalUnits : %d\n";
formatString += "unitPrice : $%.2f\n";
formatString += "stockValue : $%.2f\n/n";

return String.format ( name(),number(), units(),price(),value() );

} //End toString()
} //End Product

Thanks for any advice.

snowowls
05-05-2007, 11:06 PM
I figured it out. Guess I just needed a break away from it to find the problem.:)

jaydobs
12-05-2007, 01:18 AM
How did you fix this? I think I'm in the same class and having pretty much the same problems....

jaydobs
12-06-2007, 12:33 AM
Well never mind. I just sucked it up and bought the solution at http://www.studentoffortune.com/question/1074/Inventory-Program-Part-2 cuz I couldn't wait any longer

Man I hate this class. I just dont get java at all haha