
07-26-2008, 05:57 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
Help with program
I am trying to write a program that uses a four argument constructor. It is an inventory program that contains item number, item name, unit price, and units in stock. I created a class Product with get and set methods for each. There is also a method to calculate the extended price. The Inventory class must pass three sets of data through the constructor and display the results for each product on one line. Appropriate column headings must also be displayed. I am having trouble with the inventory class displaying the variables and column headings. I cannot use arrays. Can anyone help me?
|
|

07-26-2008, 06:48 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
|
Column heading should display only one time, right? So before doing all other data displaying print the headings. You can easily do that, because I think you have to use a loop to find all inventory results, and so before entering the loop you can do it.
Without looking what you have done, it's difficult to suggest a best approach at all.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-26-2008, 06:57 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
I can't use a loop to do it. I will copy and paste what I have:
public class Product
{
private int itemNumber; // declare variable for item number
private string productName; // declare variable for product name
private int stockUnits; // declare variable for number of units in stock
private double unitPrice; // declare variable for unit price of each product
// Four argument constructor
public Product( int item, String name, int units, double price )
{
itemNumber = item;
productName = name;
stockUnits = units;
unitPrice = price;
} // End four argument constructor
public void setItemNumber( int item ) // Set item number
{
itemNumber = item;
} // end set item number
public int getItemNumber() // return item number
{
return itemNumber;
} // end return item number
public void setProductName( String name ) // set product name
{
productName = name;
} // end set product name
public String getProductName() // return product name
{
return productName;
} // end return product name
public void setStockUnits( int units ) // set units in stock
{
stockUnits = units;
} // end set units in stock
public int getStockUnits() // return units in stock
{
return stockUnits;
} // end return units in stock
public void setUnitPrice( double price ) // set unit price
{
unitPrice = price;
} // end set unit price
public double getUnitPrice() // return unit price
{
return unitPrice;
} // end return unit price
public double getExtendedPrice() // calculate extended price
{
return stockUnits * unitPrice;
} // end method to calculate extended price
} // end class product
public class InventoryPart1
{
public static void main( String args[] )
{
// print column headings
system.out.println( "Item # Item Name Units in Stock Unit Price Extended Price" );
// instantiate first item
Product dreamCatcher = new Product( "001", "Dream Catcher", "25", "7.99" );
system.out.printf( dreamCatcher.getItemNumber(), dreamCatcher.getProductName(),
dreamCatcher.getStockUnits(), dreamCatcher.getUnitPrice(), dreamCatcher.getExtendedPrice() );
}
}
I just kind of guessed at the Inventory class. I am not sure how to return the results and make the table look good. Thanks for the help!
|
|

07-26-2008, 07:33 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
|
If you have copy-paste this code, it should never compile. I found an error at the very beginning of the code when I'm reading it.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-26-2008, 07:47 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
|
Firs fix those errors in two classes. You problem is about data types. In the constructor and the object instantiation, you have data type conflict.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-26-2008, 08:25 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
|
I'm not sure I undertand what you mean. The data types are specified as what I need them to be. Can you explain further?
|
|

07-26-2008, 08:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
|
Check that four argument constructor you have define in the code. There data types are int, String, int and double respectively.
Then find the Product class instantiation in the inventory class. You have declared all the argument types as String. So it wrong. Either you have to declare another constructor which can invoke with four String arguments or, change the instantiation according to your application requirement.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-26-2008, 07:36 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
Okay, I fixed the variables and changed the inventory class to set the values in the construcor. The program compiles but won't display anything. Here is the new code:
public class Product
{
private int itemNumber; // declare variable for item number
private String productName; // declare variable for product name
private int stockUnits; // declare variable for number of units in stock
private double unitPrice; // declare variable for unit price of each product
// Four argument constructor
public Product( int item, String name, int units, double price )
{
itemNumber = 0;
productName = name;
stockUnits = 0;
unitPrice = 0;
} // End four argument constructor
public void setItemNumber( int item ) // Set item number
{
itemNumber = item;
} // end set item number
public int getItemNumber() // return item number
{
return itemNumber;
} // end return item number
public void setProductName( String name ) // set product name
{
productName = name;
} // end set product name
public String getProductName() // return product name
{
return productName;
} // end return product name
public void setStockUnits( int units ) // set units in stock
{
stockUnits = units;
} // end set units in stock
public int getStockUnits() // return units in stock
{
return stockUnits;
} // end return units in stock
public void setUnitPrice( double price ) // set unit price
{
unitPrice = price;
} // end set unit price
public double getUnitPrice() // return unit price
{
return unitPrice;
} // end return unit price
public double getTotalValue() // calculate total value
{
return stockUnits * unitPrice;
} // end method to calculate total value
} // end class product
public class InventoryPart1
{
public int item;
public String name;
public int units;
public double price;
public static void main( String args[] )
{
Product product1 = new Product( 001, DreamCatcher, 25, 7.99 );
product1.setItemNumber( 001 );
product1.setItemName( DreamCatcher );
product1.setStockUnits( 25 );
product1.setUnitPrice( 7.99 );
system.out.println( "", product1.getItemNuber(), product1.getItemName(), product1.getStockUnits(),
product1.getUnitPrice(), product1.getTotalValue() );
}
}
|
|

07-26-2008, 08:04 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
|
|
If the program compiles with no errors, what shows on the console when you execute it? Add some more println() statements to show what is happening in the various methods.
Does the program shown above compile without errors?
|
Quote:
|
system.out.println( "", product1.getItemNuber(), product1.getItemName(), product1.getStockUnits(),
product1.getUnitPrice(), product1.getTotalValue() );
|
The above should give you errors!!!
|
|

07-26-2008, 08:07 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
|
The program compiled fine with what I had but when I ran it it didn't display anything. I changed the print statement to printf with format specifiers and it would not compile. I keep getting weird errors like "system package does not exist." I am sooooo confused!
|
|

07-27-2008, 02:04 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
|
For some reason, when I try to print just the headings using println the compiler says that the system package does not exist. I thought I had a fairly decent grasp on this language, but I have become very confused. Can anyone help?
|
|

07-27-2008, 03:10 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
|
|
|
Quote:
|
|
system.out.println
|
This would give a compile error because of the lowercase s in system. Which makes me think that the code you posted is not what you are exectuing.
Try System.
|
|

07-27-2008, 03:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
Yes Norm, as I said pointed very beginning on one of my replies, our thread starter found lots of case issues. Those are the basic things he should know, isn't it? Amazing thing is he doesn't found such compile errors.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-27-2008, 04:06 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
I have been playing with this code for two days and can't get it to what I want. I have made changes and got it to compile, but when I run it now it tells me I have a no such method error. Here is what I have now.
public class Product
{
private int itemNumber; // declare variable for item number
private String productName; // declare variable for product name
private int stockUnits; // declare variable for number of units in stock
private double unitPrice; // declare variable for unit price of each product
public double totalValue;
// Four argument constructor
public Product( int item, String name, int units, double price )
{
itemNumber = 0;
productName = name;
stockUnits = 0;
unitPrice = 0;
} // End four argument constructor
public void setItemNumber( int item ) // Set item number
{
itemNumber = item;
} // end set item number
public int getItemNumber() // return item number
{
return itemNumber;
} // end return item number
public void setProductName( String name ) // set product name
{
productName = name;
} // end set product name
public String getProductName() // return product name
{
return productName;
} // end return product name
public void setStockUnits( int units ) // set units in stock
{
stockUnits = units;
} // end set units in stock
public int getStockUnits() // return units in stock
{
return stockUnits;
} // end return units in stock
public void setUnitPrice( double price ) // set unit price
{
unitPrice = price;
} // end set unit price
public double getUnitPrice() // return unit price
{
return unitPrice;
} // end return unit price
public double totalValue() // calculate total value
{
totalValue = stockUnits * unitPrice;
return totalValue;
} // end method to calculate total value
public void displayInventory()
{
System.out.println( "Item # Name Stock Unit Price Total Value" );
System.out.printf( "%d", itemNumber, "%S", productName, "%d", stockUnits);
System.out.printf( "%.2f", unitPrice, "%.2f", totalValue );
}
} // end class product
public class InventoryPart1
{
public int item;
public String name;
public int units;
public double price;
public void main( String args[] )
{
Product product1 = new Product( item, name, units, price );
item = 001;
name = "Dream Catcher";
units = 25;
price = 7.99;
product1.setItemNumber( item );
product1.setProductName( name );
product1.setStockUnits( units );
product1.setUnitPrice( price );
}
}
|
|

07-27-2008, 04:46 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
|
Ok, can you run this code without any run-time error? Can you guarantee about that?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

07-27-2008, 05:15 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
Rep Power: 0
|
|
|
There is a run-time error, but no compile-time erors. I realize I should know some things and have done well to this point, but I am basiclly teaching myself how to do this. I ordered a book, but it has not arrived yet. I really, realy appreciate everyone's help. I am not looking for anyone to give me answers, just point me in the right direction. Again, thanks!
|
|

07-27-2008, 05:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 6,522
Rep Power: 9
|
|
Cool, I give the first point. In any class main method is the stating point. If any of your class doesn't have a main method, that me you can't run that class, because you don't have a starting point to directed to the Java Runtime Environment.
Since to run a class each one of it have a main method, which should be a static one. So that JVM initialize only one main method for a class, until it reaches to the end.
So your main method should like this.
|
Code:
|
public static void main(String[] args) {
// Your implementation goes here
} |
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Someone helped you? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post. Help: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Resources: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Web: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. Tips: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 06:03 AM.
|
|