Can anyone solve this for me using java programming language? Exercise: A planet
ID: 3588172 • Letter: C
Question
Can anyone solve this for me using java programming language?
Exercise:
A planet has a name, an orbit, a diameter and a mass. Define a class called Planet, and provide required constructors, accessor and mutator methods. Override the following methods that are defined in the Object class
equals method that checks reference equality and returns a boolean value
clone method that creates a copy of an object and returns it
toString() method that returns a string containing object details
Develop a driver class to do the following
1.Creates an array planets using the values provided in the file input.txt. Each line of the text represent name, orbit(distance from the sun) , diameter and mass
2. Print the name of the heaviest planet
3. Print name of the farthest planet from Sun
4. Create a clone of a planet
5. Check if two planets are equal
6. Print details of all planets ( by invoking toString() method implicitly)
Sample input(planet.txt):
Mercury 57910000.0 4880.0 3.30e23
Venus 108200000.0 12103.6 4.869e24
Earth 149600000.0 12756.3 5.972e24
Mars 227940000.0 6794.0 6.4219e23
Jupiter 778330000.0 142984 1.900e27
Saturn 1429400000.0 120536 5.68e26
Uranus 2870990000.0 51118.0 8.683e25
Neptune 4504000000.0 49532.0 1.0247e26
Pluto 5913520000.0 2274.0 1.27e22
Explanation / Answer
Given below is the code for the question. To indent the code in eclipse, in the file press Ctrl +A and then Ctrl + i
Hope the answer helps. If it did, please don't forget to rate the answer. Thank you.
Planet.java
public class Planet{
private String name;
private double orbit;
private double diameter;
private double mass;
public Planet(){
}
public Planet(String name, double orb, double dia, double mass)
{
this.name = name;
this.orbit = orb;
this.diameter = dia;
this.mass = mass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getOrbit() {
return orbit;
}
public void setOrbit(double orbit) {
this.orbit = orbit;
}
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
public double getMass() {
return mass;
}
public void setMass(double mass) {
this.mass = mass;
}
//override toString
@Override
public String toString()
{
String str = "";
str += "Name: " + name +" ";
str += "Orbit: " + orbit + " ";
str += "Diameter: " + diameter + " ";
str += "Mass: " + mass + " ";
return str;
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Planet)
{
Planet p2 = (Planet)obj;
if(p2.name == name && p2.orbit == orbit
&& p2.diameter == diameter && p2.mass == mass)
return true;
else
return false;
}
else
return false;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return new Planet(name, orbit, diameter, mass);
}
}
PlanetDriver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PlanetDriver {
public static void main(String[] args) {
String filename = "planets.txt";
int count = 0;
try {
Scanner inFile = new Scanner(new File(filename));
//create an array of planets
Planet[] planets = new Planet[9]; //an array of 9 planets
while(inFile.hasNext())
{
String name = inFile.next();
double orbit = inFile.nextDouble();
double diameter = inFile.nextDouble();
double mass = inFile.nextDouble();
planets[count] = new Planet(name, orbit, diameter, mass);
count++;
}
inFile.close();
//find the heaviest and farthest planets
int indexOfHeaviest = 0;
int indexOfFarthest = 0;
for(int i = 0; i < count; i++)
{
//check if this heavier
if(planets[i].getMass() > planets[indexOfHeaviest].getMass())
indexOfHeaviest = i;
//check if this is farther
if(planets[i].getOrbit() > planets[indexOfFarthest].getOrbit())
indexOfFarthest = i;
}
System.out.println("The heaviest planet is " + " " + planets[indexOfHeaviest]);
System.out.println("The farthest planet is " + " " + planets[indexOfFarthest]);
Planet clone = (Planet)(planets[0].clone());
if(clone.equals(planets[0]))
System.out.println("clone is equal to planet 0 (" + planets[0].getName() + ")");
else
System.out.println("clone is NOT equal to planet 0 (" + planets[0].getName() + ")");
if(clone.equals(planets[1]))
System.out.println("clone is equal to planet 1 (" + planets[1].getName() + ")");
else
System.out.println("clone is NOT equal to planet 1 (" + planets[1].getName() + ")");
//displaying all planets
System.out.println(" Displaying all planets");
for(int i = 0; i < count; i++)
System.out.println(planets[i]);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (CloneNotSupportedException e) {
System.out.println(e.getMessage());
}
}
}
ouput
The heaviest planet is
Name: Jupiter
Orbit: 7.7833E8
Diameter: 142984.0
Mass: 1.9E27
The farthest planet is
Name: Pluto
Orbit: 5.91352E9
Diameter: 2274.0
Mass: 1.2700000000000001E22
clone is equal to planet 0 (Mercury)
clone is NOT equal to planet 1 (Venus)
Displaying all planets
Name: Mercury
Orbit: 5.791E7
Diameter: 4880.0
Mass: 3.3E23
Name: Venus
Orbit: 1.082E8
Diameter: 12103.6
Mass: 4.869E24
Name: Earth
Orbit: 1.496E8
Diameter: 12756.3
Mass: 5.972E24
Name: Mars
Orbit: 2.2794E8
Diameter: 6794.0
Mass: 6.4219E23
Name: Jupiter
Orbit: 7.7833E8
Diameter: 142984.0
Mass: 1.9E27
Name: Saturn
Orbit: 1.4294E9
Diameter: 120536.0
Mass: 5.68E26
Name: Uranus
Orbit: 2.87099E9
Diameter: 51118.0
Mass: 8.683E25
Name: Neptune
Orbit: 4.504E9
Diameter: 49532.0
Mass: 1.0247E26
Name: Pluto
Orbit: 5.91352E9
Diameter: 2274.0
Mass: 1.2700000000000001E22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.