I have 3 classes..but i need to print the outcome from the printArray.
Right now it is printing from the GetTax Compute class. I am so confused.

Class 1...
public class PosCalProgram {
public static void main(String[] args) {
 
double x = 0;
item[] items = new item[3];
items[0]=new item("521412", "Shoes", 200);
items[1]=new item("823153", "Shirt", 100);
items[2]=new item("638212", "Pants", 400);
printArray(items);
GetTax c = new GetTax(0.07,0.08);
double z = c.compute(items);
 
}
public static void printArray(item[] y){
int size = y.length;
for (int i=0; i < size; i++){
item x =y[i];
 
}}
}
 
 
 
Class 2....
public class GetTax {
private double Gst;
private double Pst;
private double GstTaxes;
 
public GetTax(double GetGst, double GetPst){
Gst = GetGst;
Pst = GetPst;
}
 
public double compute(item[] items){
double total = 0;
//compute the price
PosCalProgram x = new PosCalProgram();
int size = items.length;
for (int i=0; i < size; i++){
item z=items[i];
 
double PstTax = Pst * z.price;
double GstTax = Gst * z.price;
double aftertax = GstTax + PstTax + z.price;
 
System.out.println(z.barCode + " " + z.name + " " + z.price + " " + GstTax + " " + PstTax + " " + aftertax);
 
total = total + aftertax;
 
}
System.out.println("Total Amount is : " + total);
return total;
}
}
 
 
Class 3....
public class item {
public String barCode = null;
public String name = null;
public double price = 0;
public item(){
}
public item(String s1, String s2, double d1){
barCode = s1;
name = s2;
price = d1;
}
 
}

any help would be appreciated

I would rather not be confused. Could you describe your problem?

Member Avatar for DaSogo

I'm guessing this is a class assignment and your instructor wants you to modify it. The program has been done for you. All you have to do is retrieve the values associated with the items that are stored in the items array which are already given in the code. First things first, forget about the output that is being done in GetTax.compute. It has nothing to do with what your instructor wants you to do. Focus only on Class1 and Class3.

The items array is holding 3 items. Each item is an object that stores the barcode, name and price. These values have been hardcoded into the main method of class PosCalProgram.

When the program runs, the main method is the first to be initialized(made active). In this process, 3 item objects are created (initialized). I will walk you through the first initialization of the first item. items[0]=new item("521412", "Shoes", 200) 1. During initialization of the first item (we are in class1), the item class(result will be an object on the heap after its initialization) is passed 3 parameters. The fist parameter is a String object, the second a String object and the third is a primitive double value (control temporarily passes to class3).
Since an item is being invoked by the main method, the item constructor begins to process all that is within it.
Its variables barcode is assigned the String object "521412", name is assigned the String object "Shoes", and price is assigned the primitive double value 200 (control passes back to class1 and the next two items are go through the same initialization process).

Control is now back in class1. The method printArray(items); is invoked (called to be used, so control is given to this method which is passed an object reference to the items array (remember, this array holds a reference to the 3 item objects stored in memory. The reference is a bit pattern pointing to the item objects and not the actual objects themselves). It is within this method where you would print the values of each item object to the output screen. It is here where your instructor wants you to learn something valueable, so I won't give you the full details. Try to figure this part out and then report back with your findings.

Ok..well i know in the printArray after item x=y; there should be
System.out.println(x.barcode + " " + x. name + " " + x.price);

but then I also want the gst, pst and total to print out in this method.

i tried to use
compute b = new Compute();
then use b.GstTax but that does not seem to work. I need to get the values from the compute method to print from the print array.

Member Avatar for DaSogo

i tried to use
compute b = new Compute(); this is a method correct? Well my friend, you CANNOT instantiate a method. You CAN however call it from another class by referencing the object to which it belongs.

Take a good look within Class 1. You'll hit yourself in the head, AGAIN!!!

Hey, what IDE are you using? I would suggest using Bluejay. It was developed by a university as a learning aide. It gives clues when you do something wrong like trying to instantiate a method;)

www.bluej.org/

One more thing? Please be sure to include error messages. I hate having to go back to read through code to find what's wrong, thanks.
then use b.GstTax but that does not seem to work. I need to get the values from the compute method to print from the print array.

Let us know how things go

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.