Question
public class Main { public static void main(String[] args) { Star sun = new Star(Sun, 1.0, 1.0, 5777); PlanetSystem solarSystem = new PlanetSystem(Solar System, sun);
public class Main {
public static void main(String[] args) { Star sun = new Star("Sun", 1.0, 1.0, 5777);
PlanetSystem solarSystem = new PlanetSystem("Solar System", sun);
Planet mercury = new Planet("Mercury", 0.03412549655905556, 1.7297154899894627E-4); solarSystem.addPlanet(mercury); solarSystem.addPlanet(new Planet("Venus", 0.08465003077267387, 0.002564278187565859)); solarSystem.addPlanet(new Planet("Earth", 0.08911486599899289, 0.003146469968387777)); solarSystem.addPlanet(new Planet("Mars", 0.04741089912158004, 3.3667017913593256E-4)); solarSystem.addPlanet(new Planet("Jupiter",1.0, 1.0)); solarSystem.addPlanet(new Planet("Saturn", 0.8145247020645666, 0.2994204425711275)); solarSystem.addPlanet(new Planet("Uranus", 0.35475297935433336, 0.04573761854583773)); solarSystem.addPlanet(new Planet("Neptune",0.34440217087226543, 0.05395152792413066));
System.out.println(solarSystem.toString());
Planet venus = solarSystem.getPlanets().get(1); Planet earth = solarSystem.getPlanets().get(2); Planet mars = solarSystem.getPlanets().get(3); Planet jupiter = solarSystem.getPlanets().get(4); Planet saturn = solarSystem.getPlanets().get(5); Planet uranus = solarSystem.getPlanets().get(5); Planet neptune = solarSystem.getPlanets().get(7);
System.out.println(sun.toString()); System.out.println(venus); System.out.printf("%s has a radius of %skm and a mass of %skg %n", venus.getName(), venus.getRadiusInKm(), venus.getMassInKg()); System.out.printf("%s has a radius of %skm and a mass of %skg %n", saturn.getName(), saturn.getRadiusInKm(), saturn.getMassInKg());
System.out.printf("%s has a radius of %skm and a mass of %skg %n", sun.getName(), sun.getRadiusInKm(), sun.getMassInKg());
System.out.printf("%s has a radius of %s Rearth and a mass of %s Mearth %n", mars.getName(), mars.getRadiusInRearth(), mars.getMassInMearth());
System.out.println("Staurn gravity " + saturn.getSurfaceGravity() + " m/s"); System.out.println("Neptune gravity " + neptune.getSurfaceGravity() + " m/s");
System.out.println("Smallest: " + solarSystem.getSmallestPlanet()); System.out.println("Largest: " + solarSystem.getLargestPlanet()); } }
public class Planet { private String name; private double radius; private double mass;
public Planet(String name, double radius, double mass) { this.name = name; this.radius = radius; this.mass = mass; }
public double getSurfaceGravity() { // g = GM / R return (6.67408E-11 * getMassInKg()) / Math.pow(getRadiusInMeter(), 2); }
public double getRadiusInKm() { return radius * 71492; }
private double getRadiusInMeter() { return getRadiusInKm() * 1000; }
public double getMassInKg() { return mass * 1.898E27; }
public double getMassInMearth() { return getMassInKg() / 5.972E24; }
public double getRadiusInRearth() { return getRadiusInKm() / 6371; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getRadius() { return radius; }
public void setRadius(double radius) { this.radius = radius; }
public double getMass() { return mass; }
public void setMass(double mass) { this.mass = mass; }
@Override public String toString() { return String.format("%s has a radius of %s Rjup and a mass of %s Mjup", name, radius, mass); } }
public class Star { private String name; private double radius, mass, effectiveTemp;
public Star(String name, double radius, double mass, double effectiveTemp) { this.name = name; this.radius = radius; this.mass = mass; this.effectiveTemp = effectiveTemp; }
public double getRadiusInKm() { return radius * 695700; }
public double getMassInKg() { return mass * 1.98892E30; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getRadius() { return radius; }
public void setRadius(double radius) { this.radius = radius; }
public double getMass() { return mass; }
public void setMass(double mass) { this.mass = mass; }
public double getEffectiveTemp() { return effectiveTemp; }
public void setEffectiveTemp(double effectiveTemp) { this.effectiveTemp = effectiveTemp; }
@Override public String toString() { return String.format("%s has a radius of %s Rsun, a mass of %s Msun and a effective temperature of %.0f K", name, radius, mass, effectiveTemp); } }
import java.util.ArrayList;
public class PlanetSystem { private String name; private Star centerStar; private ArrayList
public PlanetSystem(String name, Star centerStar) { this.name = name; this.centerStar = centerStar; }
public Planet getSmallestPlanet() { if (planets.size() == 0) return null;
Planet smallestPlanet = planets.get(0);
for (Planet currentPlanet : planets) { if (currentPlanet.getRadius() < smallestPlanet.getRadius()) { smallestPlanet = currentPlanet; } else if (currentPlanet.getRadius() == smallestPlanet.getRadius() && currentPlanet.getMass() > smallestPlanet.getMass()) { smallestPlanet = currentPlanet; } }
return smallestPlanet; }
public Planet getLargestPlanet() { if (planets.size() == 0) return null;
Planet largestPlanet = planets.get(0);
for (Planet currentPlanet : planets) { if (currentPlanet.getRadius() > largestPlanet.getRadius()) { largestPlanet = currentPlanet; } else if (currentPlanet.getRadius() == largestPlanet.getRadius() && currentPlanet.getMass() > largestPlanet.getMass()) { largestPlanet = currentPlanet; } }
return largestPlanet; }
public void addPlanet(Planet aPlanet) { planets.add(aPlanet); }
public ArrayList
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Star getCenterStar() { return centerStar; }
public void setCenterStar(Star centerStar) { this.centerStar = centerStar; }
// Object.java @Override public String toString() { return name + " has " + planets.size() + " planets that revolve around the star " + centerStar.getName(); } } Question 1
we would like to be able to retrieve a planet from a PlanetSystem based on name. Create a method that does this.
question 2
If we look more closely at Planet and Star, we see that these have several instance variables that are identical. Both of these classes can be seen as "Celestial Body". A Planet is a Celestial Body, and a Star is a Celestial Body.
We therefore want to abstract the instance variables that are common to a new class: Celestial Body.
Abstract (move) the instance variables that are common from Star and Planet to a new class Celestial Body Move methods you think belong in Celestial Body Create a constructor in Celestial Body Set Planet and Star to Extend Celestial Body Be sure to call the super-constructor in Planet and Star Verify that all the code we have in Main.java is still running properly Can you see any potential problems with any of the instance variables / methods it is natural to move?
question 3
In astronomy, we have a concept that objects move in orbit around other objects. These are called satellites. A natural satellite is e.g. a moon or planet orbiting a planet or star. For example. the earth is a satellite because it orbits the sun. In the same way, the moon is a satellite because it orbits the earth.
We want to be able to make simple calculations of these orbits, and therefore want to introduce the concept of satellites. We must therefore create the class NaturalSatellite.
a) - Natural satellite data
The vast majority of these orbits go in a kind of ellipse. In order to calculate the orbit and speed of a natural satellite, we need some data points. These are:
semi-major axis - this gives the longest distance from the focal point of an ellipse to the outside, ie from the planet to the star it orbits (this is not entirely correct, but we can relate to it this way) eccentricity - A number between 0 and 1 that says something about how the elliptical shape is orbital period - How long does it take to circle a round (in the days of the earth) centralCelestialBody - The CelestialBody this natural satellite orbits
Create the NaturalSatellite class with the mentioned data points, as well as get and set methods.
B. Natural satellite heritage
If we say that a NaturalSatellite "is a" CelestialBody, and Planet "is a" NaturalSatellite. How does it now become natural to set up the inheritance hierarchy?
Make the natural changes to make this new hereditary hierarchy work as intended. It will be necessary to change designers.
Make changes to Main.java so that the planets created in the planetary system get the new data we have specified.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started