Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-26-2008, 06:57 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2008, 07:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2008, 07:57 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-26-2008, 08:33 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-26-2008, 08:47 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-26-2008, 09:25 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-26-2008, 09:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-26-2008, 08:36 PM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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() );
}
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-26-2008, 09:04 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 690
Norm is on a distinguished road
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!!!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-26-2008, 09:07 PM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-27-2008, 03:04 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-27-2008, 04:10 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 690
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-27-2008, 04:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-27-2008, 05:06 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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 );
}
}
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-27-2008, 05:46 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-27-2008, 06:15 AM
Member
 
Join Date: Jul 2008
Posts: 22
bri1547 is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-27-2008, 06:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,864
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
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.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
cannot run the program amiey New To Java 1 11-20-2007 05:13 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 08:22 AM


All times are GMT +3. The time now is 12:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org