Hello, I am having a rough time getting this the following project to work witho
ID: 3671357 • Letter: H
Question
Hello,
I am having a rough time getting this the following project to work without errors. Any help would be greatly appreciated.
Instructions: Create a simple command line Java program to model our solar system (use
Wikipedia for any data you do not need to cite, also Pluto’s inclusion/exclusion is up to you)
and another fictional solar system of your choice/creation (the inclusion of Imperial planet
destroying artificial satellites is again your choice). To complete this project you will need to
create at least 3 separate java files:
A Planet Class: this file should be a class that contains at least 2 private attributes or properties.
The first attribute should be for the name of the planet and the second a number for the number
of moons that the planet has. The class should have a parameterized constructor, “setters and
getters” for the attributes, and finally a “toString” method that returns the name and the number
of moons.
A Solar System Class: this file should be a class with two private attributes. The first attribute is
again a name for the solar system. The second attribute should be an array of planet objects
(instances of the above class). The class should have a parameterized constructor that accepts
an int for the size of the solar system (the number of planets it can hold) and a String for the
name. There should be setters and getters for name attribute, however the accessor methods
for the array of planets should be limited to inserting a planet at a certain index (place) in the
array and retrieving a planet by index (position). Additionally include methods that return the
number of planets in the solar system and the total number of moons within the solar system.
Finally, the Solar System should have a method that prints out all of the planets it contains and
the number of their moons (the toString method for each planet should be called).
A Driver Class: this file should contain a main method that:
1. Instantiates 2 solar systems
2. Creates and inserts planets into the solar systems
3. Prints the name of each solar system, the number of its planets, the total number of its
moons, and finally each of its planets (along with their moon count)
Note: Only the Driver class should have a main method. Additionally there is no need for user
interaction with this program (Scanner or argc). All of the planet data should be hardcoded
into
the driver class.
Explanation / Answer
package com.he.capillary.chegg1;
public class Planet {
private String planetName;
private Integer numOfMoons;
public String getPlanetName() {
return planetName;
}
public void setPlanetName(String planetName) {
this.planetName = planetName;
}
public Integer getNumOfMoons() {
return numOfMoons;
}
public void setNumOfMoons(Integer numOfMoons) {
this.numOfMoons = numOfMoons;
}
@Override
public String toString() {
return "Planet [planetName=" + planetName + ", numOfMoons=" + numOfMoons + "]";
}
public Planet(String planetName, Integer numOfMoons) {
super();
this.planetName = planetName;
this.numOfMoons = numOfMoons;
}
}
package com.he.capillary.chegg1;
import java.util.Arrays;
public class SolarSystem {
private String solrSystemName;
private Planet[] planets;
public String getSolrSystemName() {
return solrSystemName;
}
public void setSolrSystemName(String solrSystemName) {
this.solrSystemName = solrSystemName;
}
public Planet getPlanets(int index) {
return planets[index];
}
public void setPlanets(int index, Planet planet) {
this.planets[index] = planet;
}
public SolarSystem(String solrSystemName, int planets) {
super();
this.solrSystemName = solrSystemName;
this.planets = new Planet[planets];
}
public int getSolarSystemPlanetCount() {
return this.planets.length;
}
public int getSolarSystemMoonCount() {
int count = 0;
for (Planet planet : this.planets) {
count += planet.getNumOfMoons();
}
return count;
}
@Override
public String toString() {
return "SolarSystem [solrSystemName=" + solrSystemName + ", planets=" + Arrays.toString(planets) + "]";
}
}
package com.he.capillary.chegg1;
public class Driver {
public static void main(String[] args) {
SolarSystem s1 = new SolarSystem("S1", 5);
s1.setPlanets(0, new Planet("A", 2));
s1.setPlanets(1, new Planet("B", 3));
s1.setPlanets(2, new Planet("C", 4));
s1.setPlanets(3, new Planet("D", 5));
s1.setPlanets(4, new Planet("E", 6));
SolarSystem s2 = new SolarSystem("S2", 4);
s2.setPlanets(0, new Planet("P", 2));
s2.setPlanets(1, new Planet("Q", 3));
s2.setPlanets(2, new Planet("R", 4));
s2.setPlanets(3, new Planet("S", 5));
System.out.println(s1.getSolrSystemName()+" "+s1.getSolarSystemPlanetCount()+" "+s1.getSolarSystemMoonCount()+" "+s1.toString());
System.out.println(s2.getSolrSystemName()+" "+s2.getSolarSystemPlanetCount()+" "+s2.getSolarSystemMoonCount()+" "+s2.toString());
}
}
Let me know if you need any modification.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.