Write a simple command line Java program to model our solar system<?xml:namespac
ID: 3538163 • Letter: W
Question
Write a simple command line Java program to model our solar system<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Mercury, 0 moons
Venus 0 moons
Earth 1 moon
Mars 2 moons
Jupiter 67 moons
Saturn 62 moons
Uranus 27 moons
Neptune 13 moons
Pluto 5 moons
To complete this project you will need to create 3 separate java class files:
A Planet Class: this file is should be a class that contains 2 private attributes or properties. The first attribute should be for the name and the second a number for the number of moons that the planet has. The class should have a parameterized constructor, %u201Csetters and getters%u201D, and finally a %u201CtoString%u201D method that returns the name and the number of moons.
A Solar System Class: this file should be a class file with a single private attribute. The attribute should be an array of planets. The class should have a parameterized constructor that accepts a number for the size of the solar system ( the number of planets ). Include accessor methods that allow the setting of planets in the array and also a method that allows us to retrieve a planet by its position within the solar system. Additionally include methods that return the number of planets in the solar system and the total number of moons.
A Driver Class: this file should contain a main method that instantiates the solar system and planets and position the planet objects within the solar system object. Then, via a simple loop, print out each of the planets and their respective moons. Finally print out the total of the moons within the entire solar system.
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 is hard-coded into the driver class.
Explanation / Answer
import java.io.*;
//PLANET CLASS
class Planet
{
private String name;
private int moons;
public Planet(String n,int num)
{
name=n;
moons=num;
}
public void setName(String n)
{ name=n; }
public void setMoons(int num)
{ moons=num; }
public String getName()
{ return name; }
public int getMoons()
{ return moons; }
public String toString()
{ return(name+" "+moons+" moons"); }
}
//SOLAR SYSTEM CLASS
class SolarSystem
{
private Planet planets[];
public SolarSystem(int size)
{ planets=new Planet[size]; }
public void setPlanet(int i,Planet p)
{ planets[i-1]=p; }
public Planet getPlanet(int i)
{ return planets[i-1]; }
public int numberOfPlanets()
{ return planets.length; }
public int totalMoons()
{ int sum=0;
for(int i=0;i<planets.length;i++)
sum=sum+planets[i].getMoons();
return sum;
}
}
//DRIVER CLASS
class Driver
{
public static void main(String args[])
{
SolarSystem S=new SolarSystem(9);
S.setPlanet(1,new Planet("Mercury",0));
S.setPlanet(2,new Planet("Venus",0));
S.setPlanet(3,new Planet("Earth",1));
S.setPlanet(4,new Planet("Mars",2));
S.setPlanet(5,new Planet("Jupiter",67));
S.setPlanet(6,new Planet("Saturn",62));
S.setPlanet(7,new Planet("Uranus",27));
S.setPlanet(8,new Planet("Neptune",13));
S.setPlanet(9,new Planet("Pluto",5));
for(int i=0;i<S.numberOfPlanets();i++)
System.out.println(S.getPlanet(i+1));
System.out.println("Total number of moons: "+S.totalMoons());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.