Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2004, 02:46 PM   PM User | #1
BladeZ
New to the CF scene

 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
BladeZ is an unknown quantity at this point
Beginner needs guidence with Arrays.

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!
BladeZ is offline   Reply With Quote
Old 12-12-2004, 12:03 AM   PM User | #2
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
You are confusing java, and javascript. Two completly different animals. Though the syntax is similar.

Basscyst
__________________
Helping to build a bigger box. - Adam Matthews
Basscyst is offline   Reply With Quote
Old 12-12-2004, 06:08 AM   PM User | #3
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
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.
turbowrx is offline   Reply With Quote
Old 12-13-2004, 07:59 AM   PM User | #4
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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:
Code:
         g.drawString("The item is a "+ stationary[a].getCost(), 20, 30+a*12);
Roelf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:00 PM.


Advertisement
Log in to turn off these ads.