Hello all,
This year at University I am doing a beginners Java module. We are
looking at OOP Java style. This week we did Arrays.
I couldn't really get my head round it all, and one of the Tasks we have
to do, that is giving me a whole load of grief is this one.
We where given this piece of script;
Code:
class Product{
private String description;
private float cost;
public Product(String describeIn, float costIn ){
description = describeIn;
cost = costIn;
}
public String getDescription(){
return description;
}
public float getCost(){
return cost;
}
}
What we need to do is Use this class in an applet called PlayProduct. The
goal of this applet is to create and Array where 4 products are
initialised and displayed on the applet. Objects should be displayed by
printing the name and cost for each object using drawString method in a
loop. The name and cost should be accessed using the object's 'get'
methods.
Its suggested in the main class (PlayProduct) we use
Code:
Product [] stationary = new Product[4];
and this piece
Code:
stationary[0] = new Product("Pen", 1.99f);
So far my script is this
Code:
import java.awt.*;
import java.applet.*;
public class PlayProduct extends Applet {
public void paint(Graphics g) {
Product [] stationary = new Product [4];
stationary[0] = new Product("Pen", 1.99f);
stationary[1] = new Product("Book", 2.99f);
stationary[2] = new Product("Pencil", 1.50f);
stationary[3] = new Product("Folder", 2.50f);
for(int a = 0; a<4; a++)
g.drawString("The item is a "+ stationary[a], 20, 30+a*12);
}
}
class Product{
private String description;
private float cost;
public Product(String describeIn, float costIn ){
description = describeIn;
cost = costIn;
}
public String getDescription(){
return description;
}
public float getCost(){
return cost;
}
}
When this is compiled and run the applet displays;
"The item is a Product(then random numbers/letters)".
Now, to my understanding I think this is because I have not declared that
there will be floats? And also maybe one or two other things.
But I'm just not sure exactly what to add. I've been trying for about 2
hours, looking on the internet and through my text book (was not worth £35
I paid for it!!!) but I just can't make sense of it all.
So please can anybody help me?
I think I should alsomention that I am not allowed to change the class
Product. Only PlayProduct.
Thanks!