JAVA HELP . . Question: . Write a Java console application that reads, sorts, an
ID: 3864011 • Letter: J
Question
JAVA HELP
.
.
Question:
.
Write a Java console application that reads, sorts, and writes planet data. Create text file PlanetsIn.txt and paste this data into it:
.
Planet Diameter (miles) Length of Day (hours)
Mercury 3032 4222.6
Venus 7521 2802.0
Earth 7926 24.0
Moon 2159 708.7
Mars 4221 24.7
Jupiter 88846 9.9
Saturn 74897 10.7
Uranus 31763 17.2
Neptune 30775 16.1
Pluto 1485 153.3
.
Here is the file specification for PlanetsIn.txt:
.
Field Type Start-End
Planet string 1-14
Diameter (miles) integer 15-21
Length of Day (hours) real 22-40
-This file contains a header row.
.
Read and parse the data into three parallel arrays: planets, diameters, and lengths. Create method printArrays to print the unsorted data including the header row. Sort the data by diameter using a bubble sort, and use method printArrays to print the sorted data including the header row. Note that when data is sorted by diameter and values are swapped in that array, corresponding values must also be swapped in the other two arrays so that the name and length stay with the correct diameter. Write the header row and sorted data per the file specification to text file PlanetsOut.txt file.
.
Note: Please include comments explaining your code. Thanks!
Explanation / Answer
JAVA PROGRAM :
import java.io.*;
import java.util.*;
public class Main {
private static final String input = "P:/PlanetsIn.txt";
static String planets[];
static int diameters[];
static double days[];
static final int LEN = 10;
static void printArrays(){
System.out.println("Planet Diameter (miles) Length of Day (hours)");
for(int i=0;i<LEN;i++){
System.out.println(planets[i]+" "+diameters[i]+" "+days[i]);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(new File(input)));
String str = br.readLine();
planets = new String[LEN];
diameters = new int[LEN];
days = new double[LEN];
int ind = 0;
while((str=br.readLine())!=null){
// System.out.println(str);
StringTokenizer stk = new StringTokenizer(str);
String name = stk.nextToken();
int diameter = Integer.parseInt(stk.nextToken());
double day = Double.parseDouble(stk.nextToken());
planets[ind] = name;
diameters[ind] = diameter;
days[ind] = day;
ind++;
}
printArrays();
//Performing bubble sort
for(int i=0;i<LEN;i++){
for(int j=1;j<LEN-i;j++){
if(diameters[j]<diameters[j-1]){
int t = diameters[j];
diameters[j] = diameters[j-1];
diameters[j-1] = t;
String n = planets[j];
planets[j] = planets[j-1];
planets[j-1] = n;
double d = days[j];
days[j] = days[j-1];
days[j-1] = d;
}
}
}
System.out.println(" After sorting");
printArrays();
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("P:/PlanetsOut.txt")));
bw.write("Planet Diameter (miles) Length of Day (hours) ");
for(int i=0;i<LEN;i++){
bw.write(planets[i]+" "+diameters[i]+" "+days[i]+" ");
}
bw.flush();
bw.close();
}
}
OUTPUT :
Planet Diameter (miles) Length of Day (hours)
Mercury 3032 4222.6
Venus 7521 2802.0
Earth 7926 24.0
Moon 2159 708.7
Mars 4221 24.7
Jupiter 88846 9.9
Saturn 74897 10.7
Uranus 31763 17.2
Neptune 30775 16.1
Pluto 1485 153.3
After sorting
Planet Diameter (miles) Length of Day (hours)
Pluto 1485 153.3
Moon 2159 708.7
Mercury 3032 4222.6
Mars 4221 24.7
Venus 7521 2802.0
Earth 7926 24.0
Neptune 30775 16.1
Uranus 31763 17.2
Saturn 74897 10.7
Jupiter 88846 9.9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.