Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3243522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:26:36+00:00 2026-05-17T18:26:36+00:00

I have the following class: class Position { private double x,y; private int id;

  • 0

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);
         //
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:26:37+00:00Added an answer on May 17, 2026 at 6:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first things first. I have the following classes: class Employee { private int employeeID;
I have the following class: public abstract class AbstractParent { static String method() {
I have the following class objects: public class VacancyCategory { public int ID {
Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
I have the following class public class Car { public Name {get; set;} }
I have the following class which uses BinaryReader internally and implements IDisposable. class DisposableClass
I have the following class [XmlRoot(ElementName= webSites)] //No capital w at the beginning public
I have the following class structure FlowerDAO with the fields (mapped using Hibernate): id
I have the following class in my C# .NET 3.5 win forms app: class
Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.