Java help concerning arrays

WiLLiSTER

2[H]4U
Joined
Mar 24, 2008
Messages
2,702
Long story short, my mom is in this java class and doesn't understand it at all. She's been having me help her, but I haven't done java in about 4-5 years now.

Right now, we have to create a program that uses an array and basically it needs to look like this:
Code:
ID    Product    Units     Price     Value
0     Stapler      2         2         4
ID will just be 0-n, however many products there are.
Products will be text.
Units is how many of said product is in stock.
Price is the price per unit.
Value is (price)(units).

I'm having trouble understanding how to do this. I know string arrays and int arrays but not a way to combine them into one and still be able to multiply for the Value column. Any thoughts guys?
 
So it's just a program that generates basically an inventory list.

Think about the problem, you want to have a list that represents items in an inventory. This should give you a hint that one approach you can take is that you're not just storing simple data types like ints and Strings in a list, perhaps something slightly more complex.

You or your mom know how to make a Java class, right?

If not...read this:
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

And the output is just formatting.
 
You could just make the array of strings and store everything as a string unless you have to do arithmetic on the stored numbers. But a class/struct would probably be the proper approach.
 
So it's just a program that generates basically an inventory list.

Think about the problem, you want to have a list that represents items in an inventory. This should give you a hint that one approach you can take is that you're not just storing simple data types like ints and Strings in a list, perhaps something slightly more complex.

You or your mom know how to make a Java class, right?

If not...read this:
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

And the output is just formatting.

Yeah, it's pretty much a simple inventory array. Looking at the link, would you do something like
Code:
public class inventory

public string product
public int units
public double(?) price
public double(?) value

Does that seem like a step in the right direction?
 
Last edited:
Well you need the brackets...

Code:
public class Item
{
    public String productName;
    public int units;
    public double price;
    public double value;
}

I don't know anything about the assignment, so perhaps 'id' is also part of the Item (or maybe id is just the position in the array? I don't know)

You could also override the 'toString()' method in the class so that the formatting of how the item's information is done can be handled by its instances.

And then there is also the topic of constructors. Right now if you create an instance of Item, productName will be a null string, and units/price/value will all default to the default values of their types (in these cases...0, 0.0, and 0.0 respectively). But perhaps you'd want to create the objects by passing them a name, units, price, and value all at the same time.

Hopefully this gives you and your mom enough information to pursue this, in conjunction with the assignment directions, class notes, the textbook, and Sun's Java tutorials.
 
Back
Top