0

I'm having problems trying to come up with a Boolean method in my Planet class to remove an array by name and shift values to close any null gaps. Could any one assist with that? Below are my classes for the Moon and Planet...

MOON CLASS:-

public class Moon
{
  private float angle=0.01;
  // add class member variables here
  private String name;
  private float radius;
  private float distance;
  private float speed;
  private int orbitalPeriod;


  // add constructor here
  public Moon(String name, float radius, float distance, float speed, int orbitalPeriod)
  {
    this.name=name;
    this.radius=radius;
    this.distance=distance;
    this.speed=speed;
    this.orbitalPeriod=orbitalPeriod;
  }


  // add other methods here
  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public float getRadius()
  {
    return radius;
  }

  public float getDistance()
  {
    return distance;
  }

  public float getSpeed()
  {
    return speed;
  }

  public float getAngle()
  {
    return angle;
  }

  public int getOrbitalPeriod()
  {
    return orbitalPeriod;
  }

  public void setOrbitalPeriod(int orbitalPeriod)
  {
    this.orbitalPeriod = orbitalPeriod;
  }

  public String toString()
  {
    return "Moon:"+name+" - (orbit= "+orbitalPeriod+")";
  }

  // This will display the moon when other code is completed.
  public void display()
  {
    angle=angle+(0.01*speed);
    pushMatrix();
    rotate(angle);
    translate(distance, 0);
    fill(149, 149, 149);
    ellipse(0, 0, radius*2, radius*2);
    popMatrix();
  }
}

PLANET CLASS:-

public class Planet
{
  private float angle=0.01;
  // add class member variables here
  private float radius, distance, speed;
  private String name;
  private Moon [] moons = new Moon [5];
  private int numOfMoons = 0;


  // add constructor here
  public Planet(String name, float radius, float distance, float speed)
  {
    this.name=name;
    this.radius=radius;
    this.distance=distance;
    this.speed=speed;
  }

  // add other methods here
  public String getName()
  {
    return name;
  }

  public float getRadius()
  {
    return radius;
  }

  public void setRadius(float radius)
  {
    this.radius = radius;
  }

  public float getDistance()
  {
    return distance;
  }

  public float getSpeed()
  {
    return speed;
  }

  public Moon[] getMoons()
  {
    return moons;
  }

  public String toString()
  {
    return "Planet:"+name+" (r= "+radius+ "d= "+distance+") has "+moons.length+" moon(s)";
  }

public void printMoons()
{
  for (Moon moon : getMoons())
  println(moon);
}

public void addMoon(Moon moon)
{
  moons[numOfMoons] = moon;
  ++numOfMoons;
}

public boolean removeMoonByName(String moonName)
{
  for (Moon moon : getMoons())
    if(moon!=null)
      moon.removeMoonByName();

}


// This will display the moon when other code is completed.  You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);   

for (Moon moon : getMoons())
  if(moon!=null)
    moon.display();

popMatrix();
 }
}
  • What were you trying to do when calling moon.removeMoonByName();? You didn't give your Moon class a method of that name. Also: Do you have to use Arrays as a requirement? Using an ArrayList would make your work much simpler and you wouldn't have to worry about that whole "closing gap" thing. – OH GOD SPIDERS Oct 14 '16 at 12:21
  • have a look here -> https://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java#644719 – Ammar Oct 14 '16 at 12:29
  • @911DidBush It was just my attempt of trying to make that method work. I want to search the Array for the name of the moon then remove that particular moon, closing any null gaps in the process. And yes I'm required to use an Array. – Troy Sesen Oct 14 '16 at 12:31
  • @unixer I doubt he can use this method without first implementing equals and hashcode. – OH GOD SPIDERS Oct 14 '16 at 12:34
  • "And yes I'm required to use an Array." - as in "The assignment explicitly states ' use an Array'" or in "I don't know any other data structures like maps or lists". – Fildor Oct 14 '16 at 12:35
  • @Fildor The assignment states that an Array should be used. I agree that an ArrayList would be the best choice but I'm limited to the use of an Array – Troy Sesen Oct 14 '16 at 12:38
  • You accepted the answer suggesting an ArrayList. An ArrayList is still a list not a plain array. Will that be ok for the assignment? – Fildor Oct 14 '16 at 12:42
  • @Fildor sorry that was an accident, I'm new to this. Still looking for a solution – Troy Sesen Oct 14 '16 at 12:45

2 Answers2

1

Your code looks like the number of moons can change very often, so an array may not be the best choice for what you want to do.

Consider using an ArrayList, which is flexible in size. Here is what you want to do:

private ArrayList<Moon> moons = new ArrayList<>();

public void addMoon(Moon moon){
  moons.add(moon);
}

public boolean removeMoonByName(String moonName){
  Moon remove;
  for(Moon m : moons){
    if(m.getName().equals(moonName)){
      remove = m;
    }
  }
  if(remove != null) return moons.remove(remove);
  return false;
} 

Note that it is more complicate if you remove your moon by name since you have to iterate through your list, get the moon you want, and delete it afterwards.

Zyndoras
  • 33
  • 1
  • 10
1

I suspect the assignment explicitly wants you to use arrays and "do the math" yourself. So suggesting anything with lists etc is futile. Therefore:

Divide and conquer:

  1. Write a function, that traverses your Moon[] array and closes null-gaps by moving non-null entries to the front.

    Of course, there are plenty ways to do this. I think I'd do a simple copy: Take another array of same size, then copy all non-null entries from first array. Then set the reference to the copy and abandon the original to GC.

  2. Write a function that takes a moon's name, and

    a. traverses the moon array looking up each moon's name.
    b. if the name matches, clear the reference (= set to null) at the respective index in the array and exit loop.
    c. call function from 1.

Additional hint: Look out for out-of-bounds! I'd at least mention in comments that those could cause exceptions if the assignment doesn't explicitly state they have to be handled and how.

To give you a starter:

Moon[] removeGaps( Moon[] original ){
    Moon[] clean = new Moon[original.length];
    int i = 0;
    for ( int j = 0; j < original.length; j++ ){
        if( original[j] != null ) clean[i++]=original[j];
    }
    return clean;
}

Remove Moon by name:

void removeMoon( String name )
{
     // moons is the instance variable Moon[] moons ...
     for( int i = 0 ; i < moons.length; i++ ){
     // TODO for you: check moon[i] for null (or getName will throw NPE)
        if ( moons[i].getName().equals(name) ){
            moons[i] = null; // clears the refernce in the array, leaving the object for GC to be collected.
            moons = removeGaps(moons);
            break;
        }
     }
}

It's not the most efficient solution, but pretty simple.

Of course you can do all this with fancy new Java 8 features possibly in a 1-liner ... but this is for learning.

Fildor
  • 14,510
  • 4
  • 35
  • 67