Results 1 to 5 of 5
Thread: object name confusion
- 01-20-2011, 01:55 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
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
- 01-20-2011, 02:03 AM #2
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.
- 01-20-2011, 02:22 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
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.
and the methodJava 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();
Im not sure about this do i have to use the assigned name when i call the method or can i use the variable.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); }
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.
- 01-20-2011, 02:25 AM #4
The variable name is a String but the display method is in the Square class. Hmmmm!Java Code:name.display(); // correct spelling
- 01-20-2011, 02:45 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 23
- Rep Power
- 0
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
-
Confusion here @@' Help!
By pleasurelyours in forum New To JavaReplies: 7Last Post: 06-09-2010, 03:42 PM -
Confusion in line
By JavaJunkie in forum New To JavaReplies: 1Last Post: 06-13-2009, 10:46 PM -
confusion in paragraph
By JavaJunkie in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-19-2009, 03:02 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM -
bracket confusion...?
By gallimaufry in forum New To JavaReplies: 4Last Post: 10-28-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks