Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Planet Record Sorting Sorting is one of the most common applications of

ID: 3865163 • Letter: P

Question

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: l. 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-separted fields with no spaces between them (this is called a csv file). The fields will be: planetName, year Discovered. mass, radius orbit Period 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:

Explanation / Answer

JAVA Program :

import java.io.*;
import java.util.*;


//Planet class
class Planet {

String name;
int year;
double mass, radius, orbit;

Planet(String name, int year, double mass, double radius, double orbit) {
this.name = name;
this.year = year;
this.mass = mass;
this.radius = radius;
this.orbit = orbit;
}

//This prints the planet object
@Override
public String toString() {
return name + " " + year + " " + mass + " " + radius + " " + orbit;
}
}

public class Main{

//Replace with your name
static String yourName = "Programmer";
static String programName = "Sortings";

public static void main(String[] args) throws FileNotFoundException, IOException {
System.out.println("Name : " + yourName + " Program Name : " + programName);
Scanner obj = new Scanner(System.in);
System.out.println("Enter name of the file");
String file = obj.next();
System.out.println("Enter the attribute on which planets to be sorted");
String sort = obj.next();
//Reader for reading the file
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(file)));
} catch (FileNotFoundException fe) {
System.out.println("File not found");
return;
}
ArrayList<Planet> al = new ArrayList();
String str = br.readLine();
//Read all the info from a planet
while ((str = br.readLine()) != null) {
String t[] = str.trim().split(",");
if (t.length != 5) {
System.out.println("Wrong input specified");
return;
}
String name = t[0];
int year = 0;
try {
year = Integer.parseInt(t[1]);
} catch (Exception e) {
System.out.println("Year is not in required format");
return;
}
double mass = 0;
try {
mass = Double.parseDouble(t[2]);
} catch (Exception e) {
System.out.println("Mass is not in correct format");
return;
}
double radius = 0;
try {
radius = Double.parseDouble(t[3]);
} catch (Exception e) {
System.out.println("Radius is not in correct format");
return;
}
double orbit = 0;
try {
orbit = Double.parseDouble(t[4]);
} catch (Exception e) {
System.out.println("Orbit is not in correct format");
return;
}

Planet p = new Planet(name, year, mass, radius, orbit);
al.add(p);
}
//Sort based on selected attribute
if (sort.equals("planetName")) {
Collections.sort(al, new Comparator<Planet>() {
@Override
public int compare(Planet o1, Planet o2) {
return o1.name.compareTo(o2.name);
}
});
} else if (sort.equals("yearDiscovered")) {
Collections.sort(al, new Comparator<Planet>() {
@Override
public int compare(Planet o1, Planet o2) {
return o1.year - o2.year;
}
});
} else if (sort.equals("mass")) {
Collections.sort(al, new Comparator<Planet>() {
@Override
public int compare(Planet o1, Planet o2) {
return (int) Math.ceil(o1.mass - o2.mass);
}
});
} else if (sort.equals("radius")) {
Collections.sort(al, new Comparator<Planet>() {
@Override
public int compare(Planet o1, Planet o2) {
return (int) Math.ceil(o1.radius - o2.radius);
}
});
} else if(sort.equals("orbit")){
Collections.sort(al, new Comparator<Planet>() {
@Override
public int compare(Planet o1, Planet o2) {
return (int) Math.ceil(o1.orbit - o2.orbit);
}
});
}
else{
System.out.println("Please enter proper value");
return;
}
//Print the values after sorting
System.out.println("After sorting");
for (Planet p : al) {
System.out.println(p);
}

}
}

OUTPUT 1:

Name : Programmer Program Name : Sortings
Enter name of the file
P:/planets.txt
Enter the attribute on which planets to be sorted
planetName
After sorting
Jupiter 2001 1.781 1.419 2.204
Mars 2006 1.197 1.247 2.47
Pluto 2010 0.077 0.357 3.213

OUTPUT 2 :

Name : Programmer Program Name : Sortings
Enter name of the file
P:/planets
Enter the attribute on which planets to be sorted
mass
File not found

OUTPUT 3 :

Name : Programmer Program Name : Sortings
Enter name of the file
P:/planets.txt
Enter the attribute on which planets to be sorted
mass
After sorting
Pluto 2010 0.077 0.357 3.213
Mars 2006 1.197 1.247 2.47
Jupiter 2001 1.781 1.419 2.204

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote