tags:

views:

95

answers:

6

I have the following class:

class Position {

        private double x,y;
        private int id;

        private static int count=0; //counts number of times a Position object has been created

        public Position (double initX, double initY) {
            x=initX;
            y=initY;
            id=count;
            count++;
        }

        public Position (Position a) {
            id=count;
            count++;
            x=a.x;
            y=a.y;
        }

I want to now create a Position object in another of my .java files. How would I do so? Wouldnt I just use Position x=new Position; ? Thats not working. Do I have to import the position class into the files? I tried that too, didnt work. Wouldnt let me import. My files are in the default folder.

Here's where I want to use it. Im not even sure Im reading the instructions correctly. Is this what they want from me? To initialize every element of the array to a new position object?

/**
     * Returns an array of num positions. Each position is initialized to a random
     * (x,y) position.
     * if num is less than zero, just return an empty array of length 0.
     * 
     * @param num
     *            number of positions to create
     * @return array of newly minted Points
     */
 public int[] randomPos(int[] a) {
     int numPositions=Position.getNumPositionsCreated();
     int[] posArr=new int[numPositions];
     int x,y;
     for (int i=0; i<numPositions;i++)
         Position rand = new Position(x,y);
         //
+4  A: 

You would need to invoke the constructor,

Position x = new Position(2.0,2.0);

Since one of your constructor takes two double's as arguments, I used those as an example.

Or, you could create a new Position object by passing in another Position object,

Position otherX = new Position(new Position(2.0,2.0)); 
// or combining our above example assuming that x is already instantiated
Position otherX = new Position(x);

Also, just in case you are unsure, there is a difference between instantiation and declaration!

Instantiation:

Position posX = new Position(1.0, 4.0);

Now, posX is an instance of a Position object, because we construct our object by invoking the constructor.

Declaration:

Position posX;

Note that the posX variable is declared to be a Position object, but has not yet been instantiated so posX would have a null reference.

Update:

Without actually do the homework for you, because you will not learn that way. I can tell you that what you have so far, and what is listed in the javadoc above do not agree. Also, given by the way the javadoc is written, it is tough to follow, therefore let me try to clean it up for you and leave you to do the rest,

/* Returns an array of n Positions. Each Position is initialized to a random
 * (x,y) position.
 * if n is less than zero, just return an empty array of length 0.
 * 
 * @param n
 *     number of Positions to create
 * @return array of newly created Positions
 */

Now we can break down that javadoc, so lets pinpoint what we know.

  • We are passing an argument, n which indicates how big the Positions array should be.
  • We need to check to see if n is equal to 0, if so we return an empty Position array.
  • Each Position object would be instantiated with random x and y values.
  • We know that we need to return an Position array.

This should get you started, I am sure you can figure out the rest.

Anthony Forloney
Ok but now I get 'Position cannot be resolved to a variable'
fprime
Can you post the code that you are using that is causing the error?
Anthony Forloney
What does your code look like?
Tyler
make Position a public class and make sure whatever is invoking it is in the same package or imports the Position class.
Steven
see edits please
fprime
Well we have a `public static int getNumPositionsCreated(){` method, so thats what we should be using. So when he says 'returns an array of num positions', does he mean returns an array with size numPositions?
fprime
I do not know your homework assignment, but I read that as "return an array of *n* (the argument being passed in) `Positions`". When reading, "num positions" it sounds like "number of positions", which throws me off.
Anthony Forloney
+1  A: 
Position x = new Position(1.0, 2.0);

For example. The way you have it now is missing method arguments.

Tyler
+1  A: 
Position x = new Position(1d,2d);

You need to pass your constructor arguments. Since you defined 2 constructors on your Position class, you must use one of them. Usually not a bad idea to put a do nothing constructor on your class. like

Postion () {}

or in your case

Position () {
   Position.static++;
}

since you are counting. Note you should reference static members statically.

hvgotcodes
+1  A: 

Don't forget the parentheses and arguments to pass into the constructor when you call new, eg:

Position p = new Position(x, y);

Depending upon where the other class is, you may also need to make Position a public class (it appears you are using the default visibility of package-private).

Also note that x and y must be initialized first,

Michael McGowan
+1  A: 

Your constructors take arguments, so you have to provide some.

Position p = new Position(1.0, -2.0);

If you want to create one without arguments, you need to a add a zero-argument constructor.

public Position() {
}

which would then be invoked by doing Position p = new Position() (the parenthesis are required when not passing in arguments)

If you don't define any constructors, java automatically creates an implied zero-arg constructor, but once you define a different constructor, you have to explicitly define a zero-arg constructor to have one.

If both of your classes are in the default package you shouldn't need to import anything.

bemace
+1  A: 

Java already has a Point class. It may be overkill for you but though I'd mention it.

http://download-llnw.oracle.com/javase/6/docs/api/java/awt/geom/Point2D.html

jms