Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Two Objects?

Tags:

java

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.

like image 867
JC626 Avatar asked Dec 11 '25 06:12

JC626


1 Answers

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});
like image 92
aliteralmind Avatar answered Dec 13 '25 21:12

aliteralmind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!