Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Accessing objects within a GCompound not possible?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing objects within a GCompound not possible?

    Novice question. Given the class MyExample below:

    public class MyExample extends GCompound {

    //instance variables
    public GRect R1 = new GRect(0, 0, 20, 20);
    public GRect R2 = new GRect(0, 0, 5, 5); //R2 is in front of R1, but its coordinates are all "inside" of R1's coordinates

    //constructor
    public MyExample() {
    add(R1);
    add(R2);
    }

    }

    1) Suppose I'm in a GraphicsProgram and declare:

    MyExample myex = new MyExample();

    Suppose I have coordinates (x1, y1), and want to find out whether this is the myex object defined in the previous line. I would use:

    obj = getElementAt(x, y);
    if (obj == myex) (...and so on)

    Now suppose I want to test whether the object is the GRect object R1 within myex. Why doesn't the following work?
    if(obj ==myex.R1) (...and so on);

    Here is the full code that shows my question; it outputs "myex", none of the other outputs come out...
    public void run() {

    GObject obj1, obj2;

    MyExample myex = new MyExample();

    add(myex);

    obj1 = getElementAt(1, 1);
    obj2 = getElementAt(19, 19);

    if (obj1 == myex) System.out.println("myex");

    if(obj1 == myex.R1) System.out.println("R1");

    if(obj1 == myex.R2) System.out.println("R2");

    if(obj2 == myex.R1) System.out.println("R11");

    if(obj2 == myex.R2) System.out.println("R22");

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,326
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Accessing objects within a GCompound not possible?

    Try doing some debugging. Add some println() statements to print out the values of the variables used in the code to see what the computer sees and help you understand what the computer is doing when it executes the code.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    NOTE: The posted code doesn't show what packages and classes are being used. Many don't look to be part of Java SE.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing objects within a GCompound not possible?

    Wrapping my code with code tags and adding the packages used:

     
    import acm.program.*;
    import acm.graphics.*;
     
    public class test extends GraphicsProgram {
     
    	public class MyExample extends GCompound {
     
    	    //instance variables
    	    public GRect R1 = new GRect(0, 0, 20, 20);
    	    public GRect R2 = new GRect(0, 0, 5, 5);    //R2 is in front of R1, but its coordinates are all "inside" of R1's coordinates 
     
    	    //constructor
    	    public MyExample() {
    	    add(R1);
    	    add(R2);
    	    }
    	}
     
    	public void run() {
     
    		GObject obj1, obj2;
     
    		MyExample myex = new MyExample();
     
    		add(myex);
     
    		obj1 = getElementAt(1,1);
    		obj2 = getElementAt(19,19);
     
    		if (obj1 == myex) System.out.println("myex");
     
    		if(obj1 == myex.R1) System.out.println("R1");
     
    		if(obj1 == myex.R2) System.out.println("R2");
     
    		if(obj2 == myex.R1) System.out.println("R11");
     
    		if(obj2 == myex.R2) System.out.println("R22");
    	}
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Accessing objects within a GCompound not possible?

    Unfortunately, there aren't many here who can help with the ACM packages which grew from a fundamentally different way of teaching Java. It's not a bad way, it's just different, and most here do not know it, don't want to know it, can't translate it into what they do know, so can't offer you much help. You might try looking for ACM-specific forums, support groups, or ask your instructors that have put you on this path.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing objects within a GCompound not possible?

    Thanks for letting me know.

    What is this alternative route used in learning Java? Which standard textbook takes this other approach?

    Thanks

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Accessing objects within a GCompound not possible?

    They are legion. Look for (search the Internet, if you'd like) Java textbooks, tutorials, or videos. I would be surprised if a generic search of the Internet for those resources would return any results that include ACM.

Similar Threads

  1. GCompound Object Help
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2013, 01:16 PM
  2. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  3. [SOLVED] Accessing && Editing properties of objects in an array. Plus a few more questions.
    By CameronFaust in forum Collections and Generics
    Replies: 31
    Last Post: August 10th, 2011, 07:35 PM
  4. accessing
    By gcsekhar in forum Java Native Interface
    Replies: 1
    Last Post: December 14th, 2010, 09:04 PM
  5. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM

Tags for this Thread