Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    You are confusing java, and javascript. Two completly different animals. Though the syntax is similar.

    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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);
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •