I have to find the minimum and maximum first grade from a List
of students and then write those values into an output file with only the first name.
I tried to picture this problem as a matrix of values, where each row corresponds to a Student and each column to their fields (name, last name, etc.). I've tried to read the students into an ArrayList
and then transfer their float grades from the Arraylist
into an array using a for loop but it's not working.
does anyone know how to do this?
public class student implements Comparable<student>{
private String fname;
private String lname;
private String major;
private float grades1;
private float grades2;
private float grades3;
public String getfname() {return fname;}
public String getlname() {return lname;}
private String getmajor() {return major;}
public float getg1() {return grades1;}
public float getg2() {return grades2;}
public float getg3() {return grades3;}
public student(String f,String l,String m,float g1,float g2, float g3) {
fname=f;
lname=l;
major=m;
grades1=g1;
grades2=g2;
grades3=g3;
}
}
public static void main (String[]args) throws IOException
{
PrintWriter pw1 = new PrintWriter ("input.txt");
pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
pw1.println ("Donald Duck NET 56.8 74.2 78.4");
pw1.println ("Bob Builder CS 78.5 89.4 82.5");
pw1.println ("Snow White MAT 56.6 32.4 56.6");
pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
pw1.close ();
PrintWriter pw2 = new PrintWriter ("input2.txt");
pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
pw2.close ();
File in = new File ("input.txt");
java.util.ArrayList < student > list = uploadStudents (in);
float[] gg = new float[list.size()];
for (int i = 0; i < list.size(); i )
gg[i] = he.getg1();
float min = gg[0];
for (int i = 0; i < gg.length; i ) {
if (gg[i] < gg[0]) {
gg[0] = gg[i];
}
}
}
public static ArrayList < student > uploadStudents (File inputfilename) throws IOException ///
{
java.util.ArrayList < student > student = new java.util.ArrayList <> ();
Scanner sc = new Scanner (inputfilename);
while (sc.hasNext ())
{
//new student object??
student s =
new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
sc.nextFloat (), sc.nextFloat ());
student.add (s);
}
return student;
}
//I want a float[] gg array to be the one that starts with 78.88
I've tried to use a for loop, but apparently it just copies all the values into gg[0]; which is not good because I also have to find the minimum of each column of the ArrayList` and then print output them in a file with the min and max of each grade and their name.
CodePudding user response:
If I understood correctly your case, you're trying to represent your list of students as a matrix of values where each row represents a student and each column one of their fields.
You don't need to map the students' grades to an array of float to find the minimum and maximum grade. You can already achieve this with the list of students you already have.
You could simply iterate your list with a basic for loop, starting from the second element and keeping the first student as the one with both the minimum and the maximum. At every iteration, you simply check whether your minimum and maximum students have respectively a higher and lower grade than the ones you're currently iterating for the field grades1
.
EDIT: In case you want to find the students with the minimum and maximum grade among the three grades they have, then you could define in your Student
class the methods getMinimumGrade
and getMaximumGrade
to respectively return the lowest and greatest grades among the three.
Student
class Student {
//your implemenyation
//Method to get the lowest grade among the three
public float getMinimumGrade() {
return Math.min(Math.min(grades1, grades2), grades3);
}
//Method to get the greatest grade among the three
public float getMaximumGrade() {
return Math.max(Math.max(grades1, grades2), grades3);
}
}
Partial Main Implementation
File in = new File("input.txt");
ArrayList<Student> listStudents = uploadStudents(in);
Student studentMinGrade = null;
Student studentMaxGrade = null;
if (listStudents.size() > 0) {
//Assuming that the first student corresponds to the student with both minimum and maximum grade
studentMinGrade = listStudents.get(0);
studentMaxGrade = listStudents.get(0);
for (int i = 1; i < listStudents.size(); i ) {
//Checking if the current student has a grade lower than the minimum student
if (listStudents.get(i).getMinimumGrade() < studentMinGrade.getMinimumGrade()) {
studentMinGrade = listStudents.get(i);
}
//Checking if the current student has a grade higher than the maximum student
if (listStudents.get(i).getMaximumGrade() > studentMaxGrade.getMaximumGrade()) {
studentMaxGrade = listStudents.get(i);
}
}
}
PrintWriter pw = new PrintWriter("output.txt");
//Printing the student with the minimum grade
if (studentMinGrade != null) {
pw.print(String.format("Student with minimum grade: %s => %f%n", studentMinGrade.getfname(), studentMinGrade.getMinimumGrade()));
}
//Printing the student with the maximum grade
if (studentMaxGrade != null) {
pw.print(String.format("Student with maximum grade: %s => %f", studentMaxGrade.getfname(), studentMaxGrade.getMaximumGrade()));
}
pw.close();
Instead, in case you're looking for something a bit more sophisticated and compact you could achieve this also with streams.
Partial Main with Stream Implementation
File in = new File("input.txt");
ArrayList<Student> listStudents = uploadStudents(in);
Student studentMinGrade = listStudents.stream()
.min(Comparator.comparingDouble(Student::getMinimumGrade)) //Finding the student with the lowest grade
.orElse(null); //Retrieving the student or null if non has been found
Student studentMaxGrade = listStudents.stream()
.max(Comparator.comparingDouble(Student::getMaximumGrade)) //Finding the student with the greatest grade
.orElse(null); //Retrieving the student or null if non has been found
PrintWriter pw = new PrintWriter("output.txt");
//Printing the student with the minimum grade
if (studentMinGrade != null) {
pw.print(String.format("Student with minimum grade: %s => %f%n", studentMinGrade.getfname(), studentMinGrade.getMinimumGrade()));
}
//Printing the student with the maximum grade
if (studentMaxGrade != null) {
pw.print(String.format("Student with maximum grade: %s => %f", studentMaxGrade.getfname(), studentMaxGrade.getMaximumGrade()));
}
pw.close();