8 Replies - 909 Views - Last Post: 05 April 2016 - 06:49 PM Rate Topic: -----

#1 jon.smith70   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-April 16

Arrays and GObjects

Posted 04 April 2016 - 11:41 PM

I'm confused as to how I would go about defining, initializing, and populating an array that would initially contain a GStar at position 0, a GDiamond at position 1, and a GHeart at position 2. Would someone please show me how to go about doing this? Thanks.

I know code for a GStar would be:
public class DrawStar extends GraphicsProgram {

	public void run() {
		add(new GStar(100), getWidth() / 2, getHeight() / 2);
	}

}





I know code for a GHeart would be:
 public GPolygon GHeart (int a) {
        double c = Math.sqrt(2 * a * a);
        GPolygon heart = new GPolygon();
        heart.addVertex(0,a);
        heart.addEdge(a,-a);
        heart.addArc(c,c,-45,180);
        heart.addArc(c,c,45,180);
        heart.addEdge(a,a);
        return heart;
    }



I know the code for GDiamond would be:
public class DrawDiamond extends GraphicsProgram {

	public void run() {
		GPolygon diamond = new GPolygon();
		diamond.addVertex(-DIAMOND_WIDTH / 2, 0);
		diamond.addVertex(0, DIAMOND_HEIGHT / 2);
		diamond.addVertex(DIAMOND_WIDTH / 2, 0);
		diamond.addVertex(0, -DIAMOND_HEIGHT / 2);
		add(diamond, getWidth() / 2, getHeight() / 2);
	}

	private static final double DIAMOND_WIDTH = 44;
	private static final double DIAMOND_HEIGHT = 72;

}



However, I have no idea as how I'd go about defining, initializing, and populating them in array a with the GStar at position 0, the GDiamond at position 1, and the GHeart at position 2. Would someone please clarify? Thanks.


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and GObjects

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 986
  • View blog
  • Posts: 3,882
  • Joined: 13-June 14

Re: Arrays and GObjects

Posted 05 April 2016 - 12:04 AM

Do you know how to declare an array and access its elements? If not, I suggest learning about that first. One place you can do so is The Java Tutorials.
Was This Post Helpful? 0
  • +
  • -

#3 jon.smith70   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-April 16

Re: Arrays and GObjects

Posted 05 April 2016 - 12:35 AM

I understand how to declare an array; however, I simply am confused as to how to do this with GObjects.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 986
  • View blog
  • Posts: 3,882
  • Joined: 13-June 14

Re: Arrays and GObjects

Posted 05 April 2016 - 12:50 AM

What's confusing you? Why do you think there's any difference between declaring an array of GObject and any other type?
Was This Post Helpful? 0
  • +
  • -

#5 jon.smith70   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-April 16

Re: Arrays and GObjects

Posted 05 April 2016 - 12:52 AM

Wouldn't there be a difference in declaring?
Was This Post Helpful? 0
  • +
  • -

#6 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Arrays and GObjects

Posted 05 April 2016 - 12:57 AM

you could have an array of Objects to store different types, e.g.
public class ArrayOfObject {

    public static void main(String[] args) {
    Object[] t={"123", "456", "345", 67, 'X', 3.14159}; 
    t[1]=4;
    for (int i=0;i<t.length;i++)
      System.out.println("t[" + i + "] =  " + t[i] + " type " + t[i].getClass());
    }
}

if you run it
t[0] =  123 type class java.lang.String
t[1] =  4 type class java.lang.Integer
t[2] =  345 type class java.lang.String
t[3] =  67 type class java.lang.Integer
t[4] =  X type class java.lang.Character
t[5] =  3.14159 type class java.lang.Double


This post has been edited by horace: 05 April 2016 - 12:58 AM

Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 986
  • View blog
  • Posts: 3,882
  • Joined: 13-June 14

Re: Arrays and GObjects

Posted 05 April 2016 - 01:09 AM

The general syntax for declaring an array of type type is

type[] name = new type[size];


You can also use the initialiser list as horace shows above.

The point is that the syntax doesn't depend on the type - we only need to specify the type and everything else is the same.

So one can write, e.g.

int[] values = new int[10];
String[] names = new String[15];


Was This Post Helpful? 0
  • +
  • -

#8 jon.smith70   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-April 16

Re: Arrays and GObjects

Posted 05 April 2016 - 06:31 PM

What if one wanted to create something with similar GObjects using an arrayList, or even a single-linked list or a double-linked list? Even a stack or a queue too.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • Posts: 45,864
  • Joined: 27-December 08

Re: Arrays and GObjects

Posted 05 April 2016 - 06:49 PM

Let's take this one step at a time. Built-in data structures have these things called generics. Locke has an excellent tutorial introducing ArrayLists. You can see the syntax for using generic data structures there.

If you want customized data structures, you have to roll your own or find a library. If you are rolling your own, I'd advocate using generics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1