Question: need java program help:( given my code could you please add to it so t
ID: 671345 • Letter: Q
Question
Question:
need java program help:(
given my code could you please add to it so that it could output the lists in alphabetical order been trying but kept getting errors.
______________________
GPACalculation.java code
____________________
import java.util.*;
import java.io.*;
public class GPACalculation
{
public static void main(String args[])
{
File file;
GPAInformation gpas[] = new GPAInformation[80];
ArrayList freshMan = new ArrayList();
ArrayList senior = new ArrayList();
ArrayList junior = new ArrayList();
ArrayList sophomore = new ArrayList();
String array[] = new String[3];
String line;
Scanner in;
double average=0;
int count = 0;
System.out.println("**************************************************************");
System.out.println(" 1)Information in the text file is: ");
System.out.println("**************************************************************");
try
{
file = new File("program1.txt");
in = new Scanner(file);
while (in.hasNextLine())
{
line = in.nextLine();
array = line.split(" ");
System.out.println(line);
GPAInformation newGPA = new GPAInformation();
newGPA.setName(array[0].trim());
newGPA.setGradeLevel(array[1].trim());
newGPA.setGpaValue(Double.parseDouble(array[2].trim()));
gpas[count] = newGPA;
count++;
}
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println();
System.out.println("**************************************************************");
System.out.println( "2)The ordered list(alphabetically) for all students is: ");
System.out.println("**************************************************************");
gpas = selectionSort(gpas, count);
print(gpas, count);
average = gpaAverage(gpas, count);
System.out.println("The average value of GPA of overall students is: "+average);
System.out.println("**************************************************************");
System.out.println();
System.out.println( "3)The ordered (alphabetically) list of freshmans: ");
System.out.println("**************************************************************");
freshMan = getGPAInformation(gpas, count, "Freshman");
printList(freshMan);
average = gpaAverage(freshMan);
System.out.println("The average value of GPA of overall freshman is: "+average);
System.out.println("**************************************************************");
System.out.println();
System.out.println("The ordered (alphabetically)list of sophomores : ");
System.out.println("**************************************************************");
sophomore = getGPAInformation(gpas, count, "Sophomore");
printList(sophomore);
average = gpaAverage(sophomore);
System.out.println("The average value of GPA of overall sophomore is: "+average);
System.out.println("**************************************************************");
System.out.println();
System.out.println("The ordered (alphabetically)list of juniors : ");
System.out.println("**************************************************************");
junior = getGPAInformation(gpas, count, "Junior");
printList(junior);
average = gpaAverage(junior);
System.out.println("The average value of GPA of overall juniors is: "+average);
System.out.println("**************************************************************");
System.out.println();
System.out.println("The ordered (alphabetically) list of seniors : ");
System.out.println("**************************************************************");
senior = getGPAInformation(gpas, count, "senior");
printList(senior);
average = gpaAverage(senior);
System.out.println("The average value of GPA of overall seniors is: "+average);
System.out.println("**************************************************************");
System.out.println();
}
public static ArrayList getGPAInformation(GPAInformation[] gpas, int count, String level)
{
ArrayList newList = new ArrayList();
for (int i = 0; i < count; ++i)
{
if (gpas[i].getGradeLevel().equalsIgnoreCase(level))
{
newList.add(gpas[i]);
}
}
return newList;
}
// Selection sort code
public static GPAInformation[] selectionSort(GPAInformation[] studs,int counts)
{
for (int i = 0; i < counts; ++i)
{
int minIndex = i;
for (int j = i + 1; j < counts; ++j)
{
if (studs[j].name.equalsIgnoreCase(studs[minIndex].name))
;
minIndex = j;
}
swap(studs, minIndex, i);
}
return studs;
}
private static void swap(GPAInformation[] studs, int i, int j)
{
GPAInformation temp = studs[i];
studs[i] = studs[j];
studs[j] = temp;
}
public static double gpaAverage(GPAInformation[] studs, int counts)
{
double avg = 0;
double total = 0;
for (int i = 0; i < counts; ++i)
{
total += studs[i].getGpaValue();
}
avg = total / counts;
return avg;
}
public static double gpaAverage(ArrayList list)
{
double avg = 0;
double total = 0;
for (int i = 0; i < list.size(); ++i)
{
total += list.get(i).getGpaValue();
}
avg = total / list.size();
return avg;
}
public static void print(GPAInformation[] studs, int counts)
{
for(int i=0;i
System.out.println(studs[i]);
}
public static void printList(ArrayList list)
{
for(int i=0;i
System.out.println(list.get(i));
}
}
____________
constructor GPAInformation.java code
______________________
public class GPAInformation
{
String name, GradeLevel;
double gpaValue;
public GPAInformation()
{
name = "";
GradeLevel = "";
gpaValue = 0;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGradeLevel()
{
return GradeLevel;
}
public void setGradeLevel(String gradeLevel)
{
GradeLevel = gradeLevel;
}
public double getGpaValue()
{
return gpaValue;
}
public void setGpaValue(double gpaValue)
{
this.gpaValue = gpaValue;
}
public String toString()
{
return getName() + " " + getGradeLevel() + " " + getGpaValue();
}
}
____________________
my .txt file
_______________________
Williams,Leonard Freshman 1.85
Smith,Sheila Senior 2.99
Anderson,Andy Sophomore 3.01
Wiser,Bud Freshman 4.00
Robertson,Jully Junior 2.78
Koran,korn Junior 3.50
smith,sam Junior 2.14
Johnson,Jim Junior 3.05
Johnson,Jane Junior 3.75
Potter,Pam Senior 2.98
Brown,Bill Sophomore 2.55
Crooks,Cathy Freshman 1.99
Gregg,Howard Senior 2.44
Nicholas,Judy Senior 3.69
White,Bob Sophomore 1.64
Walsh,Fred Junior 4.00
Dennis,Susan Senior 2.06
Roberts,Rachel Sophomore 4.00
Fredericks,Mary freshman 2.89
Holmes,Wendy Senior 2.56
Edwards,James Sophomore 3.00
Green,Barbara Sophomore 3.67
Brown,David Freshman 2.00
Williamson,Walt Sophomore 2.95
Carson,Jim Sophomore 2.03
______________________
code ouput
___________________
----jGRASP exec: java GPACalculation
**************************************************************
1)Information in the text file is:
**************************************************************
Williams,Leonard Freshman 1.85
Smith,Sheila Senior 2.99
Anderson,Andy Sophomore 3.01
Wiser,Bud Freshman 4.00
Robertson,Jully Junior 2.78
Koran,korn Junior 3.50
smith,sam Junior 2.14
Johnson,Jim Junior 3.05
Johnson,Jane Junior 3.75
Potter,Pam Senior 2.98
Brown,Bill Sophomore 2.55
Crooks,Cathy Freshman 1.99
Gregg,Howard Senior 2.44
Nicholas,Judy Senior 3.69
White,Bob Sophomore 1.64
Walsh,Fred Junior 4.00
Dennis,Susan Senior 2.06
Roberts,Rachel Sophomore 4.00
Fredericks,Mary freshman 2.89
Holmes,Wendy Senior 2.56
Edwards,James Sophomore 3.00
Green,Barbara Sophomore 3.67
Brown,David Freshman 2.00
Williamson,Walt Sophomore 2.95
Carson,Jim Sophomore 2.03
**************************************************************
2)The ordered (alphabetically) list for all students is:
**************************************************************
Carson,Jim Sophomore 2.03
Williams,Leonard Freshman 1.85
Smith,Sheila Senior 2.99
Anderson,Andy Sophomore 3.01
Wiser,Bud Freshman 4.0
Robertson,Jully Junior 2.78
Koran,korn Junior 3.5
smith,sam Junior 2.14
Johnson,Jim Junior 3.05
Johnson,Jane Junior 3.75
Potter,Pam Senior 2.98
Brown,Bill Sophomore 2.55
Crooks,Cathy Freshman 1.99
Gregg,Howard Senior 2.44
Nicholas,Judy Senior 3.69
White,Bob Sophomore 1.64
Walsh,Fred Junior 4.0
Dennis,Susan Senior 2.06
Roberts,Rachel Sophomore 4.0
Fredericks,Mary freshman 2.89
Holmes,Wendy Senior 2.56
Edwards,James Sophomore 3.0
Green,Barbara Sophomore 3.67
Brown,David Freshman 2.0
Williamson,Walt Sophomore 2.95
The average value of GPA of overall students is: 2.8608
**************************************************************
3)The ordered (alphabetically) list of freshmans:
**************************************************************
Williams,Leonard Freshman 1.85
Wiser,Bud Freshman 4.0
Crooks,Cathy Freshman 1.99
Fredericks,Mary freshman 2.89
Brown,David Freshman 2.0
The average value of GPA of overall freshman is: 2.5460000000000003
**************************************************************
The ordered (alphabetically)list of sophomores :
**************************************************************
Carson,Jim Sophomore 2.03
Anderson,Andy Sophomore 3.01
Brown,Bill Sophomore 2.55
White,Bob Sophomore 1.64
Roberts,Rachel Sophomore 4.0
Edwards,James Sophomore 3.0
Green,Barbara Sophomore 3.67
Williamson,Walt Sophomore 2.95
The average value of GPA of overall sophomore is: 2.8562499999999997
**************************************************************
The ordered (alphabetically) list of juniors :
**************************************************************
Robertson,Jully Junior 2.78
Koran,korn Junior 3.5
smith,sam Junior 2.14
Johnson,Jim Junior 3.05
Johnson,Jane Junior 3.75
Walsh,Fred Junior 4.0
The average value of GPA of overall juniors is: 3.203333333333333
**************************************************************
The ordered (alphabetically) list of seniors :
**************************************************************
Smith,Sheila Senior 2.99
Potter,Pam Senior 2.98
Gregg,Howard Senior 2.44
Nicholas,Judy Senior 3.69
Dennis,Susan Senior 2.06
Holmes,Wendy Senior 2.56
The average value of GPA of overall seniors is: 2.7866666666666666
**************************************************************
----jGRASP: operation complete.
Explanation / Answer
Strings should never be sorted using an algorithm like "Selection Sort. Mostly because of the time taken and the worst cases experienced. In order to achieve sorting for string, you should use TreeMap.
TreeMap tm = new TreeMap();
and instead of using gpas[count] = newGPA; you should use
tm.put( newGPA.getName(), newGPA); //first is the key on which basis we want to sort, and second is our object.
Now when you iterate the treemap, it will automatically give sorted output as TreeMap always stores values in sorted manner. To iterate through a TreeMAp, use
for(Entry<String, GPAInformation> entry : tm.entrySet())
{
system.out.println(entry.getValue().getName());
system.out.println(entry.getValue().getGradeValue());
system.out.println(entry.getValue().getGpaValue());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.