Question

In: Computer Science

Write a program that reads a file line by line, and reads each line’s tokens to...

Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average are listed on top.

The Student class should have instance data for first and last name, and a float array to hold three test grades.  Create getters and setters for first and last name.  Create one getter and one setter for inserting tests into the array.  Don’t forget to verify that the quiz grades are within a range that makes sense before you insert them into the array.  Also, verify that you do not go outside the index range of the array when inserting quizzes.  Create a method to calculate and return test average. Also, override the toString method and the equals method to something that make sense. Also, implement the Comparable interface on the Student class so that when you compare Student objects by comparing the student's test average. If the average is the same when comparing, then compare the last names. If the last names are the same, then compare the first names.

Here is the file that the program should read:

Text File:

Frank Hanford 78.5 65.9 98.2
John Blake 93.1 85.9 89.1
Alex Sanders 87.1 56.9 67.2
Jane Hope 72.1 85.1 77.9
Donald Davidson 85.1 72.1 77.9
Alan Davidson 77.9 72.1 85.1
Perkins Jenkins 87.5 75.9 90.1
Barbara Thompson 90.1 89.9 99.7
Michael Jones 78.1 69.9 81.2

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.
If you are using eclipse as your IDE, make sure you don't put the input file inside the src folder. Just put it directly under the project folder, otherwise you will get file not found error.

Student.java
-----
public class Student implements Comparable<Student>{
   private String fname;
   private String lname;
   private float[] grades;
  
   public Student() {
       fname = "";
       lname = "";
       grades = new float[3];
   }
   public Student(String fname, String lname) {
       this.fname = fname;
       this.lname = lname;
       grades = new float[3];
   }
   public String getFname() {
       return fname;
   }
   public void setFname(String fname) {
       this.fname = fname;
   }
   public String getLname() {
       return lname;
   }
   public void setLname(String lname) {
       this.lname = lname;
   }
  
   public void setGrade(int index, float g) {
       if(index >= 0 && index < 3 && g >= 0 && g <= 100)
           grades[index] = g;
   }
  
   public float getGrade(int index) {
       if(index >= 0 && index < 3)
           return grades[index];
       else
           return 0;
   }
  
  
   public float calculate() {
       float avg = (grades[0] + grades[1] + grades[2])/3;
       return avg;
   }
  
   public boolean equals(Student s) {
       if(fname.equalsIgnoreCase(s.fname) && lname.equalsIgnoreCase(s.lname)) {
           for(int i = 0; i < grades.length; i++) {
               if(grades[i] != s.grades[i])
                   return false;
           }
           return true;
       }
       return false;
   }
   @Override
   public int compareTo(Student o) {
       float avg1 = calculate();
       float avg2 = o.calculate();
       if(avg1 > avg2)
           return 1;
       else if(avg1 < avg2)
           return -1;
       else {
           if(lname.equalsIgnoreCase(o.lname))
               return fname.compareToIgnoreCase(o.fname);
           else
               return lname.compareToIgnoreCase(o.lname);
       }
   }
  
   public String toString() {
       return String.format("Name: %s %s, Grades: %.2f %.2f %.2f, Average: %.2f", fname, lname, grades[0], grades[1], grades[2], calculate());
   }
}


SortStudents.java
-----------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class SortStudents {
  
   public static void main(String[] args) {
       String filename;
       Scanner in = new Scanner(System.in);
      
       System.out.print("Enter input filename: ");
       filename = in.next();
      
       ArrayList<Student> s = readFile(filename);
       System.out.println("Before sorting");
       printStudents(s);
      
       sortStudents(s);
       System.out.println("After sorting on average");
       printStudents(s);
   }
  
   private static ArrayList<Student> readFile(String filename){
       ArrayList<Student> students = new ArrayList<Student>();

       try {
           Scanner infile = new Scanner(new File(filename));
           while(infile.hasNext()) {
               Student s = new Student();
               s.setFname(infile.next());
               s.setLname(infile.next());
               for(int i = 0; i < 3; i++)
                   s.setGrade(i, infile.nextFloat());
               students.add(s);
           }
           infile.close();
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
           System.exit(1);
       }
       return students;

   }
  
   private static void sortStudents(ArrayList<Student> s){
       int maxIdx;
       for(int i = 0; i < s.size(); i++) {
           maxIdx = i;
           for(int j = i+1; j < s.size(); j++) {
               if(s.get(j).compareTo(s.get(maxIdx)) > 0) {
                   maxIdx = j;
               }
           }
          
           Student temp = s.get(i);
           s.set(i, s.get(maxIdx));
           s.set(maxIdx, temp);
       }
   }
  
   private static void printStudents(ArrayList<Student> students) {
       for(Student s: students)
           System.out.println(s);
       System.out.println("\n");
   }
}


output
----
Enter input filename: grades.txt
Before sorting
Name: Frank Hanford, Grades: 78.50 65.90 98.20, Average: 80.87
Name: John Blake, Grades: 93.10 85.90 89.10, Average: 89.37
Name: Alex Sanders, Grades: 87.10 56.90 67.20, Average: 70.40
Name: Jane Hope, Grades: 72.10 85.10 77.90, Average: 78.37
Name: Donald Davidson, Grades: 85.10 72.10 77.90, Average: 78.37
Name: Alan Davidson, Grades: 77.90 72.10 85.10, Average: 78.37
Name: Perkins Jenkins, Grades: 87.50 75.90 90.10, Average: 84.50
Name: Barbara Thompson, Grades: 90.10 89.90 99.70, Average: 93.23
Name: Michael Jones, Grades: 78.10 69.90 81.20, Average: 76.40


After sorting on average
Name: Barbara Thompson, Grades: 90.10 89.90 99.70, Average: 93.23
Name: John Blake, Grades: 93.10 85.90 89.10, Average: 89.37
Name: Perkins Jenkins, Grades: 87.50 75.90 90.10, Average: 84.50
Name: Frank Hanford, Grades: 78.50 65.90 98.20, Average: 80.87
Name: Jane Hope, Grades: 72.10 85.10 77.90, Average: 78.37
Name: Donald Davidson, Grades: 85.10 72.10 77.90, Average: 78.37
Name: Alan Davidson, Grades: 77.90 72.10 85.10, Average: 78.37
Name: Michael Jones, Grades: 78.10 69.90 81.20, Average: 76.40
Name: Alex Sanders, Grades: 87.10 56.90 67.20, Average: 70.40



Related Solutions

Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
Write a program that reads two strings from an input file (The first line is X,...
Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string. You will need to write 2 methods 1) return LCS length in iterative function // return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y| int lcs_it(int **C, string X, string Y, int m, int n ) 2) return LCS resulting...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
2. Create a java program that reads a file line by line and extract the first...
2. Create a java program that reads a file line by line and extract the first word.        The program will ask for the name of input and output file. Input file: words.txt today tomorrow sam peterson peter small roy pratt Output file: outwords.txt tomorrow peterson small pratt
Write a class Lab7 that reads and evaluates postfix expressions contained in a file. Each line...
Write a class Lab7 that reads and evaluates postfix expressions contained in a file. Each line of the file contains a postfix expression with integers as operands and ‘+’, ‘-’, ‘*’ and ‘/’ as operators. Consecutive operands and operators are separated by spaces. Note the following requirements for the lab: • The main method in Lab7.java should do the following. 1. ask the user for the name of the file containing the expressions, 2. for each line of the file,...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT