3

Before I get into detail, YES this is a HOMEWORK ASSIGNMENT. NO I DON'T WANT ANSWERS, JUST TIPS. The one of the direction of the assignment states this

"Modify your SolarModel to allow for two Planets instead of one."

Things to keep in mind:

  • SolarModel is a class that creates a solar system model with a Planet object (it's own separate class with a Position, Direction, and Mass parameters [Not too important for this case]), a Sun object (also, with it's own Position, Direction, and Mass parameters), and a double Tick (a tick is an amount of time in which planets rotate that changes position - again, don't think it's important for the problem I am having).

  • The whole package of codes is based on a hierarchy, "why?" (you might ask). I don't know why, it's just the topic I am learning in class.

Here is what I have in my SolarModel class:

public class SolarModel {
    protected Sun sun;
    protected Planet planet;
    protected Planet planetb;
    protected double tick;

    public SolarModel (double tick, Sun sun, Planet planet) {
        this.tick = tick;
        this.sun = sun;
        this.planet = planet;
        }

There are more code to this, but are not important to this problem because they are just getters, setters, and a method that uses the tick to update position of the planet object...blah, blah, blah. Again, there's more to this class, just not important.

My problem (restated), is how do I use the same constructor for my SolarModel to occupy two planets? Thanks in advance.

4
  • Change the constructor to accept a second Planet argument. Commented Mar 9, 2014 at 4:31
  • I can't, the Simulation class (something I forgot to mention), uses this object with only three parameters, and it's premade to accommodate two planets, although I saw that it uses a .getOtherPlanet() calling, I just don't know how it ties in the SolarModel class. Commented Mar 9, 2014 at 4:34
  • 1
    Then create a setter and use it. Commented Mar 9, 2014 at 4:35
  • Okay, I'll try that and hope for the best that the Simulation works. Thanks for the advice! I really appreciate it. Commented Mar 9, 2014 at 4:39

2 Answers 2

3

For two planets, you can create a constructor that accepts a second Planet, as suggested by @SotriosDelimanolis:

public SolarModel (double tick, Sun sun, Planet planet1, Planet planet2) {

If your feeling saucy, you could also implement a vararg, which would then allow you to accept zero or more planets:

public SolarModel (double tick, Sun sun, Planet... planets) {

You could call it like this:

SolarModel smodel = new SolarModel(3.2, aSunObj);  //No planets
SolarModel smodel2 = new SolarModel(3.2, aSunObj, earth);
SolarModel smodel3 = new SolarModel(3.2, aSunObj, earth, venus);
SolarModel smodel3 = new SolarModel(3.2, aSunObj, earth, venus, mars);

To enforce at least two planets, but optionally allow more, use:

public SolarModel (double tick, Sun sun, Planet planet1, Planet planet2, Planet... planets3AndUp) {

Another alternative is to accept an array of planets in place of the vararg:

public SolarModel (double tick, Sun sun, Planet planet1, Planet[] planets) {
public SolarModel (double tick, Sun sun, Planet planet1, Planet planet2, Planet[] planets3AndUp) {

And the first of these are called with:

SolarModel smodel = new SolarModel(3.2, aSunObj, null);  //No planets
SolarModel smodel = new SolarModel(3.2, aSunObj, new Planet[]{});  //No planets
SolarModel smodel2 = new SolarModel(3.2, aSunObj, new Planet[]{earth});
SolarModel smodel3 = new SolarModel(3.2, aSunObj, new Planet[]{earth, venus});
SolarModel smodel3 = new SolarModel(3.2, aSunObj, new Planet[]{earth, mars});
Sign up to request clarification or add additional context in comments.

Comments

1

You can approach this in following way too:

  • instead of having one planet object, you can have an array of planets as a part of class definition.
  • add additional attribute Numberofplanets to the class.
  • modify constructor to have both parameters.
  • in constructor validate that planets is having same length as mentioned in Numberofplanets parameter.

Since you are learning class design, it is important to design a generic structure.

Above approach will allow any number of planets in solar system. Moreover, you can also try to develop a class design which can represent structure of any Solar System with all panetory and astronomical objects.

2 Comments

There's nothing stopping you from enforcing that a certain number of objects are in a parameter-array either. Hiding that array as a private internal object doesn't necessarily make it a better design, or a more "generic stricture". All arrays can be declared with any number of elements, regardless of their visibility, and all arrays can have their length externally validated. If there has to be exactly two objects, then it should be two objects. My answer makes it clear that array and vararg parameters are for "a certain number or more", not just "any number".
@aliteralmind your answer is perfect and gives alternatives to perform task in Java. All I wanted suggest that while designing a class structure, one must look at problem statement and emphasize on a generic and reusable design. My answer is merely pointing out a way to design solution. OP is in learning mode and should get some design tips as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.