...

View Full Version : Beginner needs guidence with Arrays.



BladeZ
12-11-2004, 03:46 PM
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;


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


Product [] stationary = new Product[4];

and this piece


stationary[0] = new Product("Pen", 1.99f);



So far my script is this


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!

Basscyst
12-12-2004, 01:03 AM
You are confusing java, and javascript. Two completly different animals. Though the syntax is similar.

Basscyst

turbowrx
12-12-2004, 07:08 AM
The random numbers and letter you are seeing is the memory address of the object. There are a couple of solutions, if not more.

1) Use your get methods. Instead of trying to display subscription[anything], display object.getSomething(). Hope you understand that.

2) Override the toString method for your object. Create a toString method that returns a string in your product class.

Personally, I think solution 2 is cleaner. I believe you will have to do casting in #1, but #2 will only require you to write one extra method in your product class. Good luck.

Roelf
12-13-2004, 08:59 AM
given the fact that the product class may not be altered, the first option tubowrx provides is the solution.
Use the getCost method to display the cost for the product:

g.drawString("The item is a "+ stationary[a].getCost(), 20, 30+a*12);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum