[code]
// Victoria Steele
// class for storing and displaying inventory item
public class Product
{
public int number; // number in inventory
public String title; // name of game in inventory
public int stock; // number of units in stock
public double price; // price of unit in inventory
// constructor for Product class
public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
{
int number = idNumber;
String title = gameTitle;
int stock = unitStock;
double price = unitPrice;
} // end of constructor
// create objects for inventory
public static void main( String args[] )
{
// create object for first item
Product myProduct1 = new Product( 01, " Madden ", 3, 39.99);
myProduct1.displayInventory();
Product myProduct2 = new Product( 02, " Tiger Woods ", 3, 49.99 );
myProduct2.displayInventory();
Product myProduct3 = new Product( 03, " Grand Theft Auto ", 1, 39.99 );
myProduct3.displayInventory();
Product myProduct4 = new Product( 04, " Destroy All Humans ", 1, 24.99 );
myProduct4.displayInventory();
Product myProduct5 = new Product( 05, " Mafia ", 1, 29.99 );
myProduct5.displayInventory();
} // end main
// method for value anddisplaying members of inventory
public void displayInventory()
{
System.out.print( idNumber + "\t" + gameTitle + "\t" + unitStock + "\t\t" );
System.out.printf("$%.2f\t\t", unitPrice);
} // end display
} // end class Product
print statement errorsI am getting cannot find symbol errors in my print statements.
Page 1 of 1
3 Replies - 921 Views - Last Post: 17 August 2007 - 10:59 AM
Replies To: print statement errors
#2
Re: print statement errors
Posted 17 August 2007 - 09:57 AM
There is no function for System.out.printf
I think the printf function is in the PrintStream class (or somewhere else, go to the java api for a looksky)
I think the printf function is in the PrintStream class (or somewhere else, go to the java api for a looksky)
#3
Re: print statement errors
Posted 17 August 2007 - 10:42 AM
Hi there!
I will post two possible solutions to your problem.
If you redefine the displayInventory() method like this:
it will compile. Your version of this method will not compile because the variables you refer to (idNumber, gameTitle, unitStock and unitPrice) are the formal parameters of the constructor; you need to use the instance variables (i.e. number, title, stock and price) in your method.
Another solution to your problem might be to rename the instance variables and redefine the constructor as follows:
If you go for this solution, there will be no need to alter the displayInventory() method.
I hope that I didn't miss out anything important and I also hope that all of this was of any value to you.
I will post two possible solutions to your problem.
If you redefine the displayInventory() method like this:
public void displayInventory()
{
System.out.print(number + "\t" + title + "\t" + stock + "\t\t" );
System.out.printf("$%.2f\t\t", price);
}
it will compile. Your version of this method will not compile because the variables you refer to (idNumber, gameTitle, unitStock and unitPrice) are the formal parameters of the constructor; you need to use the instance variables (i.e. number, title, stock and price) in your method.
Another solution to your problem might be to rename the instance variables and redefine the constructor as follows:
//Note, only displays the first part of the Product class
public class Product
{
public int idNumber; // number in inventory
public String gameTitle; // name of game in inventory
public int unitStock; // number of units in stock
public double unitPrice; // price of unit in inventory
// constructor for Product class
public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
{
this.idNumber = idNumber;
this.gameTitle = gameTitle;
this.unitStock = unitStock;
this.unitPrice = unitPrice;
}
...
If you go for this solution, there will be no need to alter the displayInventory() method.
I hope that I didn't miss out anything important and I also hope that all of this was of any value to you.
#4
Re: print statement errors
Posted 17 August 2007 - 10:59 AM
Hi,
You had a few things wrong here but you were not too far off. First of all, printf is fine because System.out is a type of printstream. Your main problems were related to scope of your variables and accessing the right variable names.
The main changes to this code are in Product's constructor as well as the displayInventory() method. Remember that the variable names you gave for the incoming variables are only used to set the product's member variables. All your methods should then access the Product member variable names because they are of higher scope and related to the Product.
This code above has been tested and compiles, the output could use some better organizing, but it is putting out what you designed it to do.
Look at the other member's examples on this board. Right now there are about 3 - 4 other people doing this same project.
Good luck.
You had a few things wrong here but you were not too far off. First of all, printf is fine because System.out is a type of printstream. Your main problems were related to scope of your variables and accessing the right variable names.
public class Product
{
// These variables belong to the class Product.
public int number; // number in inventory
public String title; // name of game in inventory
public int stock; // number of units in stock
public double price; // price of unit in inventory
// constructor for Product class
public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
{
// Here we use the passed parameters to set Product's variables
// Notice how I don't put datatypes on them, I am referencing the ones above with the bigger scope.
number = idNumber;
title = gameTitle;
stock = unitStock;
price = unitPrice;
} // end of constructor
// create objects for inventory
public static void main( String args[] )
{
// create object for first item
Product myProduct1 = new Product( 01, " Madden ", 3, 39.99);
myProduct1.displayInventory();
Product myProduct2 = new Product( 02, " Tiger Woods ", 3, 49.99 );
myProduct2.displayInventory();
Product myProduct3 = new Product( 03, " Grand Theft Auto ", 1, 39.99 );
myProduct3.displayInventory();
Product myProduct4 = new Product( 04, " Destroy All Humans ", 1, 24.99 );
myProduct4.displayInventory();
Product myProduct5 = new Product( 05, " Mafia ", 1, 29.99 );
myProduct5.displayInventory();
} // end main
// method for value anddisplaying members of inventory
public void displayInventory()
{
// Notice here that I am accessing the Product's variables, not the ones passed to product.
// They were only used to set the products variables which should then be referenced from now on.
System.out.print( number + "\t" + title + "\t" + stock + "\t\t" );
System.out.printf("$%.2f\t\t", price);
} // end display
} // end class Product
The main changes to this code are in Product's constructor as well as the displayInventory() method. Remember that the variable names you gave for the incoming variables are only used to set the product's member variables. All your methods should then access the Product member variable names because they are of higher scope and related to the Product.
This code above has been tested and compiles, the output could use some better organizing, but it is putting out what you designed it to do.
Look at the other member's examples on this board. Right now there are about 3 - 4 other people doing this same project.
Good luck.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|