In: Computer Science
NOTE: Projects will not be graded if:
---------------------------
Your complete project is not in a SINGLE xxxxxp2.java file, where
xxxxx is at most
the 1st the first 5 characters of your last name and p2 is the
project number.
Using package or have compile error.
It does not read input from any data file.
It’s not uploaded in canvas.
Those who work jointly, only one should submit and group names must
be written at
the bottom of the output.
---------------------------
Write an efficient java program xxxxxp2.java that reads n names and
grades from any
data file, stores them in an array and performs selection sort on
names and grades (see
below sample input/output). Your constructor should read the input
similar to
project-1 solution. The main method should be:
// main program
public static void main(String args[]) throws Exception{
// Create an instance of xxxxxp2 class
xxxxxp2 p = new xxxxxp2();
System.out.print(“Initial input:”);
p.print();
// Selection sort on name
p. sortbyname();
System.out.print(“Input sorted by grade:”);
p.print();
// Selection sort on grade
p. sortbygrade();
System.out.print(“Input sorted by name:”);
p.print();
} // end main
To compile at the command prompt: javac xxxxxp2.java
To execute at the command prompt: java xxxxxp2 < any data
file
------------------------------
Sample input:
10 Zaid 75.0 Sason 92.5 Mohamed 88.0 Sarah 100.0 Joel 69.0 Gisela
55.0 Keith 48.4
Suzee 79.2 Steven 95.9 Balal 72.4
Sample Output:
Performing Selection Sort on names and grades.
Prepared by: Date:
Initial input:
Zaid 75.2
Sason 92.5
Mohamed 88.0
Sarah 100.0
Joel 69.0
Gisela 55.3
Keith 62.4
Suzee 79.2
Steven 95.9
Balal 72.4
Input sorted by name
Balal 72.4
Gisela 55.3
Joel 69.0
Keith 62.4
Mohamed 88.0
Sarah 100.0
Sason 92.5
Steven 95.9
Suzee 79.2
Zaid 75.2
Input sorted by grade
Gisela 55.3
Keith 62.4
Joel 69.0
Balal 72.4
Zaid 75.2
Suzee 79.2
Mohamed 88.0
Sason 92.5
Steven 95.9
Sarah 100.0
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
GradeSystem.java
package c13;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class GradeSystem {
private String name[];
private double grade[];
private int n;
GradeSystem(){
Scanner sc = new
Scanner(System.in);
//loading name of file
File file = new File("input.txt");
//reading data from this file
Scanner reader;
try {
reader = new
Scanner(file);
n =
reader.nextInt();
name = new
String[n];
grade = new
double[n];
for(int
i=0;i<n;i++){
name[i] = reader.next();
grade[i] = reader.nextDouble();
}
System.out.println("File scan done...");
} catch (FileNotFoundException e)
{
System.out.println("file not found");
}
sc.close();
}
private void print() {
System.out.printf("\n\n%-10s%-10s\n","Name","Grade");
for(int i=0;i<n;i++){
System.out.printf("%-10s%-10.2f\n",name[i],grade[i]);
}
System.out.println();
}
public void sortbygrade(){
for (int i = 0; i < n-1;
i++)
{
int min_idx =
i;
for (int j =
i+1; j < n; j++)
if (grade[j] < grade[min_idx])
min_idx = j;
double temp =
grade[min_idx];
grade[min_idx] =
grade[i];
grade[i] =
temp;
}
}
private void sortbyname() {
for (int i = 0; i < n-1;
i++)
{
int min_idx =
i;
for (int j =
i+1; j < n; j++)
if (name[j].compareTo(name[min_idx])<0)
min_idx = j;
String temp =
name[min_idx];
name[min_idx] =
name[i];
name[i] =
temp;
}
}
public static void main(String[] args) {
// Create an instance of xxxxxp2
class
GradeSystem p = new
GradeSystem();
System.out.print("Initial input:
input.txt");
p.print();
// Selection sort on name
p. sortbygrade();
System.out.print("Input sorted by
grade:");
p.print();
// Selection sort on grade
p. sortbyname();
System.out.print("Input sorted by
name:");
p.print();
}
}
input.txt
10 Zaid 75.0 Sason 92.5 Mohamed 88.0 Sarah 100.0 Joel 69.0
Gisela 55.0 Keith 48.4
Suzee 79.2 Steven 95.9 Balal 72.4