Results 1 to 5 of 5
  1. #1
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default object name confusion

    hi everyone

    i have been working on some object orientated work this week and trying to get the hang of how classes and object work ect. this has led me onto array lists which i only discovered a few hours ago.they seem very useful. i have been working on a problem which creates multiple instances of objects from a class. my class is called square and i would like a user to be able to create as many of these as required and then have them added to an array list.below is a section of my program which i suspect is very misguided :) to put it simply is how do i set this up so i can have the name of my object
    set to change on every loop as the user decides ? so i can call methods related to them as i need eg squareA.display ect .. any help would be appreciated (if this problem even makes sense).

    my array list is called squareX and is of the type square.and all is inside a for loop.

    Java Code:
                String name;
    	    double lenght;
    		 
    	   
    	    System.out.println("Enter the lenght of the side of a square ");
                lenght = Double.parseDouble(kbd.readLine());
                 
                 squareX.add( new square(lenght));
    Last edited by mobosecomin; 01-20-2011 at 02:01 AM. Reason: code tags

  2. #2
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Simple. Add an extra parameter to you square constructor and pass the name the same as you do with length.

    By the way, following Java conventions you should have class names begin with a capital letter.

  3. #3
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    HI Junky

    Thanks i was going that direction but i could not get it to run when i tried to run my method called display from the class.Here is the code.

    Java Code:
     
    		 String name;
    		 double lenght;
    		 
    	    	 System.out.println("Enter the name of a square ");
    	    	 name = kbd.readLine();
    	    	System.out.println("Enter the lenght of the side of a square ");
                    lenght = Double.parseDouble(kbd.readLine());
                 
                    squareX.add( new Square(name,lenght));
                   name.display();
    and the method

    Java Code:
    public class Square {
    	
            double side  = 0;
    	String name = "";
    	
    	Square(String n, double l ){
    		
    		side  = l;
    		name  = n;
    		
    	}
    	 
    	  void display(){
    		 System.out.println(name);
    	 }
    Im not sure about this do i have to use the assigned name when i call the method or can i use the variable.
    I also get the error the method display is undefined for the type string.

    Im kind of jumping ahead of where Im at in school with this iv only been programming 12 weeks so maybe these questions are a little bit silly.But i do find if i work ahead and try stuff when we come to it in class i understand it alott better.

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    name.display(); // correct spelling
    The variable name is a String but the display method is in the Square class. Hmmmm!

  5. #5
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    Thanks Junky . Thats pretty stupid what i was doing their . Square.display(); works for me but i am still unsure about a few things but it is late here so i am going to leave it for tonight. I will do some more research tomorrow as i am still unsure how this will work when i make multiple instances but this will allow me to continue and see how the program works. Thanks for your help i appreciate it.

Similar Threads

  1. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  2. Confusion in line
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 10:46 PM
  3. confusion in paragraph
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-19-2009, 03:02 PM
  4. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM
  5. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 11:17 AM

Posting Permissions

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