Results 1 to 8 of 8
  1. #1
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Unhappy Trouble creating object that includes array

    Hello. I'm new in the world of Java and in this forum, so please excuse me if I'm posting in the wrong section or explaining my problem in a false manner and please dont refrain from noting it in your reply.

    Well, here is the thing. I'm trying to code a small spaceship shooter game and this is my first time dealing with objects.

    I have this class at my disposal

    Java Code:
    public Polygon(Point[] inShape, Point inPosition, double inRotation)
    The Polygon class gets a series of point coordinates (inShape) that form a shape, the coordinates of this shape's current position (inPosition) and inRotation has nothing to do with my problem :P
    The way Polygon works is, also, irrelevant (I think.)

    I use the class Point as a means of storing coordinates. Here is the code:
    Java Code:
    class Point {
    	double x;
    	double y;
    
    	public Point(double inX, double inY) { 
    		x = inX; 
    		y = inY; 
    	}
    
    	public Point clone() {
    		return new Point(x, y);
    	}
    }

    And here is where the trouble is. I also have a class named Ship which represents the spaceship and is a subclass of Polygon.
    Java Code:
    public class Ship extends Polygon {
    In there i'm trying to define the shape of the spaceship by creating Point type objects, that I will then pass over to Polygon to process the shape and it's position in space. I cant create a Point[] Object successfully, i guess because am using the wrong syntax when trying to fill an array with coordinate pairs.
    These are some of my numerus failed experimental attempts to do it. I got frustrated so most of them are just random typing. They might not make any sense at all...

    Java Code:
    Point[] Alpha = {(0,0), (2,1), (4,0), (2,4)};
    Point[] Alpha = new Point[] {(0,0), (2,1), (4,0), (2,4)};
    Point[] Alpha = new Point[4] {(0,0), (2,1), (4,0), (2,4)};
    Point[] Alpha = new Point[] [(0,0), (2,1), (4,0), (2,4)];
    Point[] Alpha = new Point[] (0,0), (2,1), (4,0), (2,4);
    Point[] Alpha = new Point[] {(0.0), (2.1), (4.0), (2.4)};
    The basic error that occurs is : Syntax error on token ",", invalid AssignmentOperator. and it refers to the commas inside the coordinates' brackets. Here I should note that I work on Eclipse.

    Can anyone point out to me which is the ridiculus syntax mistake that tortures me? Thank you very much :D

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You were pretty close. Here's the correct syntax:
    Java Code:
    Point[] array = {new Point(1,2), new Point(3,4)}
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Default

    oh gosh im gonna try this out right now! Thank you an awful lot :)

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Java Code:
    Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
    edit: too dang slowwww!

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by curmudgeon View Post
    Java Code:
    Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
    edit: too dang slowwww!
    Neener neener! :D
    To the OP, no probs, you're a very rare first-time poster, that actually has a real question, not a do my homework assignment type post. Glad to have been of assistance.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Desdenova View Post
    Can anyone point out to me which is the ridiculus syntax mistake that tortures me?

    try this

    Java Code:
    public class PointExample {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
    		
    		int pointCount = 1;
    		for (Point p : Alpha) {
    			System.out.println("Point " + pointCount++ + " coordinates are: " + p.x + "," + p.y);
    		}
    	}
    }

    and the output will be

    Point 1 coordinates are: 0.0,0.0
    Point 2 coordinates are: 2.0,1.0
    Point 3 coordinates are: 4.0,0.0
    Point 4 coordinates are: 2.0,4.0
    have fun.

  7. #7
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Default

    Okay, now how do I call super polygon from the Ship Class?

    Java Code:
    public class Ship extends Polygon {
    	
    	Point[] Alpha = {new Point (0,0),new Point (2,1),new Point (4,0),new Point (2,4)};
    	
        public Ship(){
        	super (Alpha, new Point (3,5), 45);
        	
        }
        
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Desdenova View Post
    Okay, now how do I call super polygon from the Ship Class?

    Java Code:
    public class Ship extends Polygon {
    	
    	Point[] Alpha = {new Point (0,0),new Point (2,1),new Point (4,0),new Point (2,4)};
    	
        public Ship(){
        	super (Alpha, new Point (3,5), 45);
        	
        }
        
    }
    The first thing a constructor does is either call another constructor in its class or call a superclass constructor. Either implicitly, i.e. the compiler inserts a "super()" call in your constructor or explicitly as you have done. The current object doesn't exist yet, i.e. it needs to be build up from the inside out: first the superclass constructor then comes your constructor afterwards.

    Because the object of your class doesn't exist yet you can't refer to any member of it yet, as your Alpha array. Either make that Alpha array static or pass it in to your constructor as a parameter.

    kind regards,

    Jos

Similar Threads

  1. Trouble with creating TreeSet
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 12-27-2009, 02:22 AM
  2. Replies: 2
    Last Post: 02-23-2009, 02:18 AM
  3. Tomcat5: changing url (includes port and etc)
    By happyknappy in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 01:33 PM
  4. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •