Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: an Array of objects

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default an Array of objects

    Hi,

    When creating a multidimensional array of objects, I don't know how to refer to a specific object.

    I think that instead of changing the data of a specific object, i'm changing only the data fields of the class.

    You can see the methods i've built in the class.

    It must be stupid and basic, because I'm really new to OOP : \

    Java Code:
    class Worker{
    	
    	private static int id; 
    	private static String fullName;
    	private static int salary;
    	
    	private Worker(){
    		
    	}
    	
    	public static Worker fillName(Worker worker, String givenFullName, int givenId){//fillName method starts- fills name and id into a Worker object
    		id= givenId;
    		fullName= givenFullName;
    		salary= id*1000;
    		
    		
    		
    		return (worker);
    		
    	}
    	
    	public static void printWorker(Worker worker){//printWorker method starts
    		
    		System.out.print(id+fullName+salary);
    		
    		
    		
    		
    		
    	}
    	
    	
    }
    Thanks!

    oh, and these are 2 methods that use the Worker class methods:

    Java Code:
    public static void printMatrix(Worker[][] mat){	
    		int counter=0;
    		for(int r=0; r<mat.length; r++){
    			for(int c=0; c<mat[r].length; c++){
    				if (counter%4==0&&counter>0) System.out.println("\n-------------------------------------------------------------------");
    				Worker.printWorker(mat[r][c]);
    				
    				
    				counter++;
    			}//second for ends
    			
    			
    		}//first for ends
    		if (counter%4==0&&counter>0) System.out.println("\n-------------------------------------------------------------------");
    		
    		
    		
    	}
    	
    	
    	
    	public static void getMatrix(Worker[][] mat ,String[] fullNames){//getMatrix method begins
    		int id=1;
    		for(int r=0; r<mat.length; r++){
    			for(int c=0; c<mat[r].length; c++){
    				Worker.fillName(mat[r][c],fullNames[id-1],id);
    				
    				
    				
    				id++;	
    			}//second for ends
    			
    			
    		}//first for ends
    		
    		
    		
    		
    	}
    Last edited by Mapisto; 01-07-2012 at 07:46 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Why would you give your Worker class static fields of any kind? Doesn't that completely defeat its purpose? How would an individual Worker object be able to hold its own data?

    I think that you want to re-think this class first and foremost and give it only instance variables (the only exception would be if you needed a single variable or constant shared by the whole class).
    Last edited by Fubarable; 01-07-2012 at 08:41 PM.

  3. #3
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    I had a feeling something is wrong with the static decleration, but when I tried to change it, it gave me an error :|

    I'll try again, and also read about the static decleration, because I think i haven't fully understood its purpse.....

    thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    You're fixing the wrong thing by making the variables all static. You need to fix it the other way around -- only refer to those variables in a non-static way.

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    The problem is I'm calling the first methods from the main method, which is static.
    then those methods must be also static, otherwise i won't be able to refer to them from the main.
    then the same goes for the Worker class methods, they also need to be static, but if they're static, their variables must be also static :(

    What am i missing..?

    Here are the changes I've made so far:

    Java Code:
    	public static void printMatrix(Worker[][] mat){	
    		int counter=0;
    		for(int r=0; r<mat.length; r++){
    			for(int c=0; c<mat[r].length; c++){
    				if (counter%4==0&&counter>0) System.out.println("\n-------------------------------------------------------------------");
    				Worker.printWorker(mat[r][c]);
    				
    				
    				counter++;
    			}//second for ends
    			
    			
    		}//first for ends
    		if (counter%4==0&&counter>0) System.out.println("\n-------------------------------------------------------------------");
    		
    		
    		
    	}
    	
    	
    	
    	public static void getMatrix(Worker[][] mat ,String[] fullNames){//getMatrix method begins
    		int id=1;
    		for(int r=0; r<mat.length; r++){
    			for(int c=0; c<mat[r].length; c++){
    				Worker.fillName(mat[r][c],fullNames[id-1],id);
    				
    				
    				
    				id++;	
    			}//second for ends
    			
    			
    		}//first for ends
    		
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    
    }
    
    
    
    class Worker{
    	
    	private int id; 
    	private String fullName;
    	private int salary;
    	
    	Worker(){
    		
    	}
    	
    	Worker fillName(Worker worker, String givenFullName, int givenId){//fillName method starts- fills name and id into a Worker object
    		id= givenId;
    		fullName= givenFullName;
    		salary= id*1000;
    		
    		
    		return (worker);
    		
    	}
    	
    	void printWorker(Worker worker){//printWorker method starts
    		
    		System.out.print(id+fullName+salary);
    		
    		
    		
    		
    		
    	}
    	
    	
    }
    lines 6 and 26 are underlined red : \

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Then don't call the method in a static way. Only call instance methods in an instance way -- on an object of the class, not on the class itself. But more importantly, what are you trying to do in the getMatrix method -- what is its function? Because it doesn't really make sense.

    I think that you should spell out in much greater detail the goal of your program and the goal of the code you've posted, because that's the point where you need to start. Next of all, all fields and methods of the Worker class should be non-static (unless you post a very good reason for an exception -- which you've yet to do so far).
    Last edited by Fubarable; 01-07-2012 at 09:24 PM.

  7. #7
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    I'm sorry, I haven't posted the whole thing.
    What u're missing is the main method, which is given to me and cannot be changed, I must work with it as it is. that's part of the excersize.. and it's static.. (public static void main(String[] args))

    getMatrix method accepts an array of worker objects and an array of names (for the workers).
    Its function is to go to each object in the array and send it to the fillName method (in the worker class).
    The fillName method then change its deafult "full name" to one of the names in the string array, change its deafult sallary to something else, and also give him an ID.

    The problem is when i print the matrix of objects, it just prints all the objects with the same data, which is the data of the last object.
    (well, that was before I've changed the static, now it won't do anything at all coz of the errors : \ )

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Quote Originally Posted by Mapisto View Post
    What u're missing is the main method, which is given to me and cannot be changed, I must work with it as it is. that's part of the excersize.. and it's static.. (public static void main(String[] args))
    Yes, this is always static. Can you post it so we can see it?

    getMatrix method accepts an array of worker objects and an array of names (for the workers).
    Its function is to go to each object in the array and send it to the fillName method (in the worker class).
    The fillName method then change its deafult "full name" to one of the names in the string array, change its deafult sallary to something else, and also give him an ID.
    I think that this is where you're making a mistake. You're calling fillName on the Worker class, not on each Worker object in the array. The fillName method should accept only two parameters -- the String name and the int for the id, that's it. It should then use these parameters and set the fields of the Worker object. Again, you must call this method on the item held by the array. The method should return nothing -- void.

    The problem is when i print the matrix of objects, it just prints all the objects with the same data, which is the data of the last object.
    Which is why you should never use static fields in this situation. When your Worker class uses a static String name field, then all Worker objects share this field. If you change it in one Worker object, you change it for all Worker objects, which was the point of my very first post in this thread.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    As an aside, getMatrix may need to be a static method depending on your assignment rules and the code given to you. Are you 100% sure that the first parameter to this method is a two dimensional array of Worker and not a one dimensional array of Worker?

    Again, if you could post the main method that you've been given and your actual assignment requirements, we'd probably be better able to help you.

  10. #10
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    here's the main method:

    Java Code:
    public static void main(String[] args) {
    		
    		// Number rows and columns of the matrix
    		final int rows = 2, columns = 4;
    		// Matrix of workers
    		Worker[][] mat = new Worker[rows][columns];
    		// Array of names
    		String[] fullNames = {"DAVID1", "DAVID2", "DAVID3",
    		"DAVID4", "DAVID5", "DAVID6",
    		"DAVID7", "DAVID8"};
    		// Fills the matrix with workers
    		getMatrix(mat, fullNames);
    		System.out.println("matrix:\n");
    		printMatrix(mat);
    I've got the part with the static and why it ruined the purpse, but I still can't figure how exactly can I approach each object and not the data fields.
    I refer to the objects through their places in the matrix, and not through their ID.
    I've given the "fillName" method the specific object i wanted to deal with in every loop.
    What should i write so it refers only to a specific object?

  11. #11
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    Quote Originally Posted by Fubarable View Post
    As an aside, getMatrix may need to be a static method depending on your assignment rules and the code given to you. Are you 100% sure that the first parameter to this method is a two dimensional array of Worker and not a one dimensional array of Worker?

    Again, if you could post the main method that you've been given and your actual assignment requirements, we'd probably be better able to help you.
    100% positive, u can see it now
    The purpose of this excercise is to make our life harder :| and i accept and deal with it.. but it's not a logical problem, I just don't really "speak the language" yet : \
    That's why i wanted to know how am i supposed to refer to a specific object.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    OK, I'm understanding it better, but yet still, can you post the actual instructions?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Hello? you still there?

    Again, you will need to post your instructions, but I think it all can be solved simply and easily by giving Worker a constructor that accepts a name String and an id int.

  14. #14
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    Sry, I took care of something.

    I'll try and translate that part of the instruction for you, I'll post it soon.

    About the constructor, I know it would be much simpler.. I'd have done the same, but I'm not allowed to changed the given lines :( That's the point of the excersize..

    I think they're doing it for 2 reasons.. First, they plant a lot of problems so we'll have to think and in order to solve.. which is good at the end..
    second, when I will program for a company.. sometimes I'll have to look for bugs or just change an existing program without rewriting it all over again..

    So I guess I'll have to stick to what I've already done, just fixing what I've written before.. of course I'll keep thinking of a simpler solution..

    and hey, thanks a lot for your time and patience :)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Quote Originally Posted by Mapisto View Post
    Sry, I took care of something.

    I'll try and translate that part of the instruction for you, I'll post it soon.

    About the constructor, I know it would be much simpler.. I'd have done the same, but I'm not allowed to changed the given lines :( That's the point of the excersize..
    Change what given lines? How would making the Worker constructor accept a String and an int change the main method? Answer: it wouldn't.

    I think they're doing it for 2 reasons.. First, they plant a lot of problems so we'll have to think and in order to solve.. which is good at the end..
    second, when I will program for a company.. sometimes I'll have to look for bugs or just change an existing program without rewriting it all over again..

    So I guess I'll have to stick to what I've already done, just fixing what I've written before.. of course I'll keep thinking of a simpler solution..
    Suit yourself.

  16. #16
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    The translation: (of the part I've asked about)


    You're to write the class "Worker" which represents a worker (obviously : \ ).

    You must work with the lines given in the main method exactly without changing it.

    In the Worker class you'll save the ID of every worker, his full name, and his salary.

    You're to work with the given fields:

    private int id;
    private String fullName;
    private int salary;

    The main method creates a matrix in this order:
    In [0][0] will be worker with ID 1, name 1 (from the string array), salary 1000.
    In [0][1] will be worker with ID 2, name 2 (from the string array), salary 2000.
    and so goes on.

    after all that, the matrix needs to be printed in this way exactly:

    1 David1 1000 | 2 David2 2000 | 3 David3 3000 | 4 David4 4000 |
    -------------------------------------------------------------------------------------------
    5 David5 5000 | 6 David6 6000 | 7 David7 7000 | 8 David8 8000 |

  17. #17
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    Quote Originally Posted by Fubarable View Post
    Change what given lines? How would making the Worker constructor accept a String and an int change the main method? Answer: it wouldn't.

    But the main method already constructs those objects for me.
    Should i change the deafult constructor to make something else?

    In the fillName method I've changed the existing objects. they've had deafult when I've created them.

    I can't refer to them with id as they are, only with the [row][column]. At least that's what I understand :|

    Please explain

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    Quote Originally Posted by Mapisto View Post
    But the main method already constructs those objects for me.
    It does??? Where? All the main method does now is to just create an array of Workers but does not initialize any of the Worker objects held in the array. That you must do by calling the constructor inside the for loops in your fillName method. Again, you should have the constructor accept a String or an int.


    Should i change the deafult constructor to make something else?
    See above.


    In the fillName method I've changed the existing objects. they've had deafult when I've created them.
    There are no "existing object", and again you yourself have to create them there. Consider that creating an array of objects is like constructing a parking lot. All you do when you make the array is create the lot itself. Later you have to fill it with cars (with objects).
    Mapisto likes this.

  19. #19
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: an Array of objects

    Oh!
    Now I get it!
    So the array just makes places for the objects, not the objects themselves.
    I thought it made the objects also with the deafult values.

    Thanks man! now it will be simpler (I hope )

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: an Array of objects

    This is the part of the process that I just love -- when the light goes off. Good luck; I think you've got it licked now.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Array of objects
    By Saletra in forum New To Java
    Replies: 2
    Last Post: 07-31-2010, 12:16 PM
  3. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 PM
  4. Array of objects
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 10-31-2008, 04:03 AM
  5. Array with objects
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:50 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
  •