I\'m having troubles with the code, please help! Program: Planet Record Sorting
ID: 3802415 • Letter: I
Question
I'm having troubles with the code, please help! Program: Planet Record Sorting Sorting is one of the most common applications of computer science. Java has built-in sorting methods that allow you to quickly and efficiently sort items in different ways. In this program, you will read in records from a file, sort them based on one of the fields of the records using Java's built-in sorting routines, and then write the sorted list to the console as a table. Things you will learn Creating simple classes Comparators and Java's sorting routines Reading input from files and formatting output. Specification In this assignment, you will implement a planet information sorter program. Your program must include the following elements: 1. A class called "Planet" to hold a planet's information. 2. Different comparators to compare the different attributes of planets. Planet records will be specified in a file, one record per line. Each line of the file will have five comma-separated fields with no spaces between them (this is called a csv file). The fields will be: planetName, yearDiscovered, mass, radius, orbitPeriod, An example file is shown below: TrES-2b, 2006, 1.197, 1.247, 2.47 HAT-P-7b, 2008, 1.781, 1.419, 2.204 Kepler-4b, 2010, 0.077, 0.357, 3.213 The program must take two pieces of input, either as command line arguments, or by prompting the user for input: 1. the name of a file containing planet records. 2. the name of a field (attribute) of the records on which to sort, which must be one of planetName, yearDiscovered, mass, radius, orbitPeriod. When run, your program must first print out your name and the program name, get input from the user (either from the command line arguments or using a scanner). Then it must print a formatted, sorted list of the records from the file, printed to the console. The fields of the formatted output must beExplanation / Answer
Please find the working code, input file data, sample output below. Use DriverClass.java to execute the program.
// Planet.java
public class Planet {
private String planetName;
private Integer yearDiscovered;
private Double mass, radius, orbitPeriod;
public Planet(String planetName, int yearDiscovered, double mass, double radius, double orbitPeriod ) {
setPlanetName(planetName);
setYearDiscovered(yearDiscovered);
setMass(mass);
setRadius(radius);
setOrbitPeriod(orbitPeriod);
}
public String getPlanetName() {
return planetName;
}
public void setPlanetName(String planetName) {
this.planetName = planetName;
}
public int getYearDiscovered() {
return yearDiscovered;
}
public void setYearDiscovered(int yearDiscovered) {
this.yearDiscovered = yearDiscovered;
}
public Double getMass() {
return mass;
}
public void setMass(double mass) {
this.mass = mass;
}
public Double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Double getOrbitPeriod() {
return orbitPeriod;
}
public void setOrbitPeriod(double orbitPeriod) {
this.orbitPeriod = orbitPeriod;
}
@Override
public String toString() {
return planetName + "," + yearDiscovered + ","+ mass + "," + radius + "," + orbitPeriod;
}
}
//PlanetNameComparator.java
import java.util.Comparator;
public class PlanetNameComparator implements Comparator<Planet> {
@Override
public int compare(Planet o1, Planet o2) {
return o1.getPlanetName().compareTo(o2.getPlanetName());
}
}
//YearDiscoveredComparator.java
import java.util.Comparator;
public class YearDiscoveredComparator implements Comparator<Planet>{
@Override
public int compare(Planet o1, Planet o2) {
if(o1.getYearDiscovered()==o2.getYearDiscovered()){
return 0;
}
if(o1.getYearDiscovered()>o2.getYearDiscovered()){
return 1;
}
if(o1.getYearDiscovered()<o2.getYearDiscovered()){
return -1;
}
return 0;
}
}
//MassComparator.java
import java.util.Comparator;
public class MassComparator implements Comparator<Planet> {
@Override
public int compare(Planet o1, Planet o2) {
return Double.compare(o1.getMass(), o2.getMass());
}
}
// RadiusComparator.java
import java.util.Comparator;
public class RadiusComparator implements Comparator<Planet> {
@Override
public int compare(Planet o1, Planet o2) {
return Double.compare(o1.getRadius(), o2.getRadius());
}
}
// OrbitPeriodComparator.java
import java.util.Comparator;
/**
*
* @author
*
*/
public class OrbitPeriodComparator implements Comparator<Planet> {
@Override
public int compare(Planet o1, Planet o2) {
return Double.compare(o1.getOrbitPeriod(), o2.getOrbitPeriod());
}
}
// DriverClass.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
*
* @author
*
*/
public class DriverClass {
public static void main(String[] args) throws FileNotFoundException, IOException {
// replace David with your name
System.out.println("My Name : " + "David");
System.out.println("Program Name : Planet Record Sorting");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the name of the file having planets data...");
String fileName = scanner.nextLine();
System.out.println("Sorting is expected on which of these parameter: planetName, yearDiscovered, mass, radius, orbitPeriod?");
String sortedOn = scanner.nextLine();
scanner.close();
// Read the file
File file = new File(fileName);
List<Planet> listOfPlanet = new ArrayList<Planet>();
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
String[] content = line.split(",");
Planet p = new Planet(content[0], Integer.parseInt(content[1]), Double.valueOf(content[2]), Double.parseDouble(content[3]), Double.parseDouble(content[4]));
listOfPlanet.add(p);
}
}
switch (sortedOn){
case "planetName":
Collections.sort(listOfPlanet, new PlanetNameComparator());
printPlanets(listOfPlanet);
break;
case "yearDiscovered":
Collections.sort(listOfPlanet, new YearDiscoveredComparator());
printPlanets(listOfPlanet);
break;
case "mass":
Collections.sort(listOfPlanet, new MassComparator());
printPlanets(listOfPlanet);
break;
case "radius":
Collections.sort(listOfPlanet, new RadiusComparator());
printPlanets(listOfPlanet);
break;
case "orbitPeriod":
Collections.sort(listOfPlanet, new OrbitPeriodComparator());
printPlanets(listOfPlanet);
break;
default:
System.out.println("Invalid parameter name to be sorted on... Note: Match case as asked");
}
}
private static void printPlanets(List<Planet> listOfPlanet) {
for(Planet planet: listOfPlanet){
System.out.println(planet.toString());
}
}
}
Data in Input File (planets.txt)
TrES-2b,2016,1.197,1.247,2.47
HAT-P-7b,2008,1.781,1.419,2.204
Kepler-4b,2010,1.077,0.357,3.213
Sample Output:
My Name : David
Program Name : Planet Record Sorting
Enter the name of the file having planets data...
planets.txt
Sorting is expected on which of these parameter: planetName, yearDiscovered, mass, radius, orbitPeriod?
mass
Kepler-4b,2010,1.077,0.357,3.213
TrES-2b,2016,1.197,1.247,2.47
HAT-P-7b,2008,1.781,1.419,2.204
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.