Hello, I am having trouble with this intro level program. I need the first solar
ID: 3671444 • Letter: H
Question
Hello, I am having trouble with this intro level program. I need the first solar system to reflect the real solar system and the real amount of moons each planet has. The second solar system has made up planets and a made up number of moons. This is very entry level and doesn't need to have complex code. I seem to be having issues with the number of moons being too large for the integer specified, but when I try to change it to double, the program fries. Thank you in advance and I really appreciate your help!
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)
Explanation / Answer
public class Planet {
String name; // attribute for name of planet
int moons; // attribute ofr number of moons
public Planet(String name, int moons) // getters and setters for planet and moons
{
this.moons = moons;
this.name = name;
}
public String toString() { // printing the planet name and number of moons in that planet
return "The Planet " + name + " Has " + moons + " Moon(s) ";
}
}
public class SolarSystem {
private String solarsystem; // name of solar system
private Planet[]planets; // array containing planets
private int position = 0; // assigining initial position to be 0
Planet[]moons; // array for moons
public SolarSystem(int size) { // the number of planets in solar system
planets = new Planet[size];
}
public void add(Planet planet) {
planets[position] = planet; // finiding the positions of planets
position++;
}
public int sum(Planet[] planets) {
int sum = 0;
for(Planet planet : planets){
sum += planet.moons; // number of moons in planets
}
return sum;
}
public String toString(){
String result = "";
for(int i = 0; i < planets.length; i++){
result += planets[i].toString();
// total number of moons in all planets is stored in result and in order to control overflow of integer as asked in question, converting it to string and then printing the result after the println statement
}
if(position ==9) System.out.println("You Have " + position + " Planets In Your REAL Solar System and " + this.sum(this.planets) + " Moons");
// real solar system has 9 planets including pluto
else System.out.println("You Have " + position +" Planets In Your VIRTUAL Solar System and " + this.sum(this.planets) + " Moons");
return result; // total number of moons
}
}
public class Driver {
public static void main(String[]args) {
Planet mercury = new Planet ("Mercury", 0); // planets in real solar system
Planet venus = new Planet ("Venus", 0);
Planet earth = new Planet ("Earth", 1);
Planet mars = new Planet ("Mars", 2);
Planet jupiter = new Planet ("Jupiter", 67);
Planet saturn = new Planet ("Saturn", 62);
Planet uranus = new Planet ("Uranus", 27);
Planet neptune = new Planet ("Neptune", 14);
Planet pluto = new Planet ("Pluto", 5);
Planet M = new Planet ("m", 4444311); // planets in virtual solar system
Planet V = new Planet ("V", 13342321);
Planet E = new Planet ("e", 13344411);
Planet R = new Planet ("r", 134555111);
Planet J = new Planet ("j", 6704994);
Planet S = new Planet ("s", 62272772);
Planet U = new Planet ("u", 27717272);
Planet N = new Planet ("n", 141662);
SolarSystem RealSolarsystem = new SolarSystem(9); // object of realsolarsystem
SolarSystem FictionalSolarsystem = new SolarSystem(8); // object of virtaul solar system
RealSolarsystem.add(mercury); // adding planets in real solar system
RealSolarsystem.add(venus);
RealSolarsystem.add(earth);
RealSolarsystem.add(mars);
RealSolarsystem.add(jupiter);
RealSolarsystem.add(saturn);
RealSolarsystem.add(uranus);
RealSolarsystem.add(neptune);
RealSolarsystem.add(pluto);
System.out.println(RealSolarsystem);
FictionalSolarsystem.add(M); // adding planets in virtual solar system
FictionalSolarsystem.add(V);
FictionalSolarsystem.add(R);
FictionalSolarsystem.add(R);
FictionalSolarsystem.add(J);
FictionalSolarsystem.add(S);
FictionalSolarsystem.add(U);
FictionalSolarsystem.add(N);
System.out.println(FictionalSolarsystem);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.