Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By JosAH
  • 2 Post By JosAH

Thread: Learning about Objects

  1. #1
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Learning about Objects

    Hi All,

    I am trying to learn about the OO areas of Java and I believe that I have managed to create some objects but now I am trying to work out how to access information from these objects and then use System.out.print(); to display information of these on screen.
    However, I have become stuck and if I can get any advise then it will be great


    I am using the following,

    Java Code:
    public class RunProgram {
    
    	public static Object[] myObjects = new Object[10];
    	
    	public static void main(String[] args) {
    		
    		myObjects[0] = new ObjTest("Object 1", "An Object", 2, 2.2);
    		myObjects[1] = new ObjTest("Object  2", "An Object", 0, 1.3);
    		myObjects[2] = new ObjTest("Object  3", "An Object", 5, 5.57);
    		myObjects[3] = new ObjTest("Object  4", "An Object", 3, 3.31);
    		myObjects[4] = new ObjTest("Object  5", "An Object", 3, 0.34);
    		myObjects[5] = new ObjTest("Object  6", "An Object", 0, 5.20);
    		
    		// Print list of toys to screen using Object Accessor's
    		
    		
    	}
    }
    And this is supported by,

    Java Code:
    public class ObjTest {
    
    	String objID, description;
    	int aNumber;
    	double aDouble;
    
    	public ObjTest(String objID, String description, int aNumber, double aDouble) {
    		this.objID = objID;
    		this.description = description;
    		this.aNumber = aNumber;
    		this.aDouble = aDouble;
    	}
    }
    Everything compiles okay but I am stuck trying to access these objects in the array again to print data from them :(
    Last edited by GrumpyBum; 04-25-2012 at 10:03 AM. Reason: code error

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,247
    Blog Entries
    7
    Rep Power
    24

    Default Re: Learning about Objects

    You already wrote the solution in line #14: define the necessary accessor methods in the ObjTest class; here's one of them:

    Java Code:
    public String getObjID() { return objID; }
    You have to implement the others and you can use them if you have an instantiation of yourr ObjTest class.

    kind regards,

    Jos
    GrumpyBum likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Learning about Objects

    Excellent, thank you.
    I have created the following and it has given what I want :)

    Java Code:
    public String getObj() { 
    		return objID + description + aNumber;
    	}
    and,

    Java Code:
    System.out.println(((ObjTest) myObjects[0]).getObj());
    Thank you so much, you have saved me a lot of time.
    I had already spent a few hours researching this and on the bright side learnt alot about Java that I did not know.

    Now to format the output to look better, this should be the easy part :)

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Learning about Objects

    Here is a handy technique for formatting strings. You can read more about it in the Java tutorials on the Oracle site.

    Java Code:
    return String.format("ObjectID(%s): %s %d", objID, description, aNumber);
    In the project I work on, we use this a lot for logging purposes. There are some implications to doing so related to performance because these end up in a lot of mainline paths and therefore can impact performance. If you care to understand more, I can elaborate, possibly in a different thread related to Java logging. Heck, maybe it's time for my first blog!

  5. #5
    GrumpyBum is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Learning about Objects

    Thank you,
    For now I have used the following at it has worked how I want it to work, but once completed the project I will review everything to ensure I am happy.

    Java Code:
    return String.format("%-8s %-20s %d", objID, description, aNumber);
    Last edited by GrumpyBum; 04-25-2012 at 11:52 AM. Reason: code edit

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,247
    Blog Entries
    7
    Rep Power
    24

    Default Re: Learning about Objects

    Here's something else that comes in handy: if a PrintStream or a PrintWriter needs to output an object of any type it calls its toString() method and prints that. Every type has such a method, either implemented or ultimately inherited from the Object class. So if you define something like this in your ObjTest class:

    Java Code:
    public String toString() { return String.format("ObjectID(%s): %s %d", objID, description, aNumber); }
    (I copied that code from the previous reply ;-) and somewhere in your code you do this:

    Java Code:
    System.out.println(yourTestObj);
    the toString() method is called and you'll see the resulting String on stdout.

    kind regards,

    Jos
    GrumpyBum and jlczuk like this.
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,741
    Rep Power
    22

    Default Re: Learning about Objects

    If you know that your myObjects array is only going to contain ObjTest objects, then declare it as an ObjTest[].
    That'll save you having to do that casting when you;re reading things in the array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Learning SWT
    By LemonDrop in forum SWT / JFace
    Replies: 2
    Last Post: 04-01-2014, 05:16 PM
  2. Replies: 12
    Last Post: 06-14-2011, 04:55 PM
  3. Learning - Uni vs Job
    By jonytek in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 07:21 AM
  4. e-learning
    By Vipan Konnect in forum Advanced Java
    Replies: 3
    Last Post: 11-21-2009, 05:29 PM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 PM

Posting Permissions

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