Results 1 to 20 of 20
Thread: Accessor method
- 06-12-2009, 05:40 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Accessor method
I'm having trouble accessing an object. Here's some of my code...
The accessor method in my Student class works fine when used on an object that has a name. For example...Java Code:public class Student{ public Student(int h, int a, int i, String n) { height=h; age=a; iq=i; name=n; } public String getName() { return name; } }
However when I create an object that doesn't have a name, how am I supposed to use the accessor method on it?Java Code:Student studentA = new Student(172,22,110,"gary"); System.out.println(studentA.getName());
The reason I'm creating objects without name is because I've written a program that creates student objects based on input from a scanner class. I don't know if it's possible to name objects using input from a scanner.Java Code:new Student(182,18,122,"gary"); System.out.println(???.getName());
Last edited by DC200; 06-12-2009 at 05:43 AM.
- 06-12-2009, 06:10 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
Well, Technically your name is just an attribute.
Your objects reference point is studentA
You can either have another constructor in Student that accepts the same parameters with out a name or just have
Student studentB = new Student(182,18,122,"");
then use your normal studentB.getName();
your second section of code makes no sense as its not creating anything and it also has the name "gary" being assigned.
Using this will allow you to create a student object with out a name (however assuming you havn't initialised it it will return null if you try and call it or nothing).Java Code:public class Student{ public Student(int h, int a, int i, String n) { height=h; age=a; iq=i; name=n; } public Student(int h, int a, int i) { height=h; age=a; iq=i; } public String getName() { return name; } }
Student studentC = new Student(182,18,122);
studentC.getName() = null or "" can't rememberLast edited by JohnnyR; 06-12-2009 at 06:12 AM.
- 06-12-2009, 06:21 AM #3
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Yes, but how do I use the getName() method on an object whose reference point is unknown to me? Take the following object, for example.
What is its reference point?Java Code:new Student(182,18,122,"gary");
Last edited by DC200; 06-12-2009 at 07:05 AM.
- 06-12-2009, 06:33 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Objects withod assigned variables cannot be accessed
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-12-2009, 06:49 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Ah, I see.
How else can I create an object during runtime? Obviously I can't hardcode the reference points because the number of objects is indefinite.
Object objectname = new Object(parameters);
In this case, objectname would be some sort of a variable that allows me to assign new reference points to each new objects.Last edited by DC200; 06-12-2009 at 06:53 AM.
- 06-12-2009, 06:55 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
Every object has to have a reference point.
You just want to create some with names and some with out which is where my above stuff comes in.
You could store them in an ArrayList or predefined Object[].
Then you can loop through that to pull them out, you can't just create an object over and over with the same variable reference, they need to be stored somewhere, cause otherwise it overrides the previous pointer to the previous object.
make sense?
e.g. you loop over
int i = 0;
while (i != 5) {
Object objectname = new Object();
i++;
}
This creates an object of reference of objectname 5 times, but only the 5th object is actually still available as its the only one left with a reference point of object name.
You want to add an ArrayList in there to list.add(objectname) once its completed and then you will see 5 objects with in that list all with pointers to each object.
- 06-12-2009, 06:57 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
No need for posts like that...
Just thought of a quick correction... objects created without assigning a variable to them cannot be accessed in the constructor/method they are created by. However, if you create them as constructor/method parameters they can be accessed by the constructor/method they are parameters for.
e.g
The method callEqualityMethod will always return false. TestEquality probably would always return false as well, because I don't believe JLabel overrides .equals. But that is irrelevant. The point is that objects created using new MyObject() without assigning a variable are only accessible if used as parameters, and even then can only be accessed by the constructor/method they are parameters for.Java Code:public boolean testEquality(JLabel labs){//Allows unlimited JLabel parameters JLabel firstLab = labs[0];//gets a single JLabel from the array to test against for(JLabel lab : labs){ if(!lab.equals(firstLab)){ return false; } } return true; } public boolean callEqualityMethod(){ return testEquality(new JLabel("text"), new JLabel("anotherText"), new JLabel()); }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-12-2009, 07:14 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Thank you both for offering alternatives, but I still don't think either of you understand what I'm trying to do. I'm probably not being concise enough, and I apologize for that.
What I'm trying to do is allow the user to create as many objects as they want during runtime. Leaving out the object's reference point was one way I thought this could be done, though I now understand that this was the wrong approach since it makes it difficult to access the objects after they have been created.
I'm hoping that there is another simple way I could go about doing this.Last edited by DC200; 06-12-2009 at 07:17 AM.
- 06-12-2009, 07:45 AM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Hmm... What you want is a dynamic array... Try using an ArrayList or Vector. They both allow you to add, remove, and get objects. The only real difference is that ArrayList is unsynchronized, while Vector is. I would suggest that you use an ArrayList, because I doubt that you will be doing multi-threaded programming with multiple threads accessing the ArrayList, and a Vector takes far more memory than an ArrayList.
Take this method...
The code above allows the user to enter an undefined number of Strings, adds them to the ArrayList, and if they press enter, it returns the ArrayList as a String array by calling toArray.Java Code:public String[] getAnArray() throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> strings = new ArrayList<String>(); String input; while(true){ System.out.println("Enter a string or press enter to indicate completion."); input = reader.readLine(); if(input.equals("")) break; strings.add(input); } return strings.toArray(new String[0]); }
Hope this is what you were looking for,
Singing BoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-12-2009, 08:27 AM #10
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Yes, I'm already using an ArrayList. This is what I've got so far...
It uses the same Student class from my first post in this thread. Now suppose I run the program and loop twice to create two Student objects, this is what System.out.println(itr.next()); will display:Java Code:import java.util.*; public class StudentArray{ public static void main (String[] args){ //create arraylist ArrayList<Object> studentArray = new ArrayList<Object>(); //input loop while(1==1) { Scanner input = new Scanner(System.in); System.out.println("continue? y/n"); String cont = input.nextLine(); if (cont.equals("n")) { break; } else { System.out.print("\nstudent name: "); String name = input.nextLine(); System.out.print("height: "); int height = input.nextInt(); System.out.print("age: "); int age = input.nextInt(); System.out.print("iq: "); int iq = input.nextInt(); studentArray.add(new Student(height,age,iq,name)); } } //iterator interface Iterator itr = studentArray.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); //System.out.println(itr.next().getName()); //does not work (symbol not found error thrown by compiler) } } }
In the end, I can create as many objects as I want and store them into the ArrayList, but the problem is that I cannot use my getName() method on said objects since they do not have reference points.Java Code:Student@14318bb Student@ca0b6
Last edited by DC200; 06-12-2009 at 08:37 AM.
-
but they do. You can access them through the arraylist of course.
The reason for the funny results in your println is that you haven't given the objects a toString() method override, and so Java just ouputs the default toString() result. I'd give student a String name field (I didn't see one declared in your code above), and then create a method like so:
Also, please don't cripple your ArrayList by making it an array list of object: ArrayList<Object>. it's not. It's an ArrayList<Student>Java Code:public String toString() { return name; }
- 06-12-2009, 11:43 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
As Fubarable says this:
should be this:Java Code:ArrayList<Object> studentArray = new ArrayList<Object>();
because that's what it is. Which then allows you to turn your Iterator into an Iterator<Student>. Which means the line you commented out:Java Code:ArrayList<Student> studentArray = new ArrayList<Student>();
will now work.Java Code:System.out.println(itr.next().getName());
- 06-12-2009, 08:11 PM #13
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
I did what Fubarable told me to do, and it seems to be working perfectly now. It turns out that all I needed was a toString() method override to replace my getName() method.
Now, itr.next() can display the names of each Student object.
Thanks. :)Last edited by DC200; 06-12-2009 at 08:21 PM.
- 06-15-2009, 09:51 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Well, I hate to say it, but that's not a proper solution. I'm assuming you're still using an ArrayList<Object>, rather than an ArrayList<Student>, which is bad practice. In addition, writing a toString() whose job is, in fact, a getName(), is also bad. Someone using your code (and I realise this is not something likely to see the light of day past your course) will not expect toString() to only supply the name. I would expect it to provide a full description of the object (ie all the data in Student).
A proper solution is the one I proposed above (and pointed out by Fubarable originally). Anything else is merely putting sticky tape over a problem,which is never a good thing.
- 06-15-2009, 06:37 PM #15
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
I understand what you're trying to say. I have to admit that I didn't understand what was causing this problem, and so I didn't realize that I was merely covering up the flaws in my program. I did follow your advice as well and changed the ArrayList<Object> to an ArrayList<Student>, but even then I was having trouble getting the commented line to compile.
- 06-16-2009, 09:14 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're learning, so there's no problem there. Did you change the Iterator to an Iterator<Student>? If it's still a straight Iterator then that's the equivalent to Iterator<Object>, which means that you will still have that problem.
If you have changed it to <Student> then if you post the code (and the compilation error) we might be able to spot the problem.
- 06-16-2009, 05:42 PM #17
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Oh, I didn't know I was supposed to change the iterator to <Student> as well. I did that, and now it's giving me a compilation error. Below is the iterator section of my code. I posted the rest earlier (see post #10).
Java Code://iterator interface System.out.println("\niterating through array..."); Iterator<Student> itr = studentArray.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); }Perhaps I'm not instantiating the iterator properly.Java Code:StudentArray.java: incompatible types found: java.util.Iterator<java.lang.Object> required: java.util.Iterator<Student> Iterator<Student> itr = studentArray.iterator();Last edited by DC200; 06-16-2009 at 05:46 PM.
- 06-16-2009, 05:52 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
If you're still using the code in post 10 then this:
is your problem. Your Iterator<Student> is fine, however studentArray is not an ArrayList<Student>. You have to have both...the type if the iterator has to match the type of the list.Java Code:ArrayList<Object> studentArray = new ArrayList<Object>();
Edit: Also, you really ought to be doing itr.next().getName() in the println.Last edited by Tolls; 06-16-2009 at 05:55 PM.
- 06-17-2009, 02:33 AM #19
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
I was just about to post the code again when I noticed that my ArrayList hadn't been changed to <Student>. I could have sworn that I had already done that earlier...
Anyway, it seems to be working perfectly now. I also noticed that after changing the ArrayList and Iterator to <Student>, I can use my getName() method on the objects without the need for a toString() override. Spot on.
Thank you for taking the time to point this out. :)Last edited by DC200; 06-17-2009 at 02:39 AM.
- 06-17-2009, 09:11 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
The getName() works because the compiler now knows that, firstly everything in your ArrayList is a Student, and secondly everything returned by your Iterator is also a Student. This means that itr.next() actually returns a Student and not simply an Object as it was doing before.
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
Accessor help needed (I think...- little confussed)
By thomase in forum New To JavaReplies: 7Last Post: 03-11-2009, 10:29 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:15 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks