This is by far the hardest section in computer science I hav
This is by far the hardest section in computer science I have encountered because I still can\'t see the difference with arrays and arraylist or how to print them out properly without getting an error. How do I create an array verson of this section and what difference would it look like with an arrayList.
For each student
---Reads from a file the name and a series of labs for each person (students do not all have the same number of labs)
----Calculates the average of the labs
----Stores the name and average in an array of class Grades.
Prints the information from the array using a “for loop”
Prints the following information with appropriate documentation:
----Class lab average
----Name and average of student with highest average
----Name and average of student with lowest average
Searches for particular students
----Asks the user for a name
----If the name is in the array, prints the name and average for that person.
----If the name is not in the array, prints a message to the user indicating the name does not exist.
----Continues to ask the user for names until the user wants to stop
You must use one, and only one, array for this application. do NOT use multiple arrays.
Create a new program but you must use one, and only one, arrayList for this application.
Solution
import java.util.*;
 import java.io.*;
 public class Driver{
 
           Grades grades[]=new Grades[10];
           int size=-1;
     public void read()
     {
         BufferedReader br = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader(\"grades.txt\"));
            while ((sCurrentLine = br.readLine()) != null) {
                 boolean found=false;
                String token[]=sCurrentLine.split(\",\");
                for(int i=0;i<size;i++)
                {
                     if(grades[i].getName().equals(token[0]))
                     {
                         found=true;
                         grades[i].setGrade((grades[i].getGrade()+Integer.parseInt(token[1]))/2);
                      }
                }
                if(!found)
                {
                     size++;
                     grades[size]=new Grades(token[0],Integer.parseInt(token[1]));
                }
           }
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
   
     public double calAvg()
     {
         double avg=0;
         for(int i=0;i<=size;i++)
         {
             avg+=grades[i].getGrade();
         }
         avg/=grades.length;
         return avg;
     }
   
     public void display()
     {
         for(int i=0;i<=size;i++)
         {
            System.out.println(grades[i]);
         }
     }
   
     public double getMax()
     {
          double max=-99;
         for(int i=0;i<=size;i++)
         {
             if(grades[i].getGrade()>max)
             max=grades[i].getGrade();
         }
         return max;
     }
   
     public double getMin()
     {
          double min=999;
         for(int i=0;i<=size;i++)
         {
             if(grades[i].getGrade()<min)
             min=grades[i].getGrade();
         }
         return min;
     }
   
     public void search(String n)
     {
         boolean found=false;
         for(int i=0;i<=size;i++)
         {
             if(grades[i].getName().equals(n))
             {
                 found=true;
                 System.out.println(\"Student found!!!\");
                 System.out.println(\"Average of Labs: \"+grades[i].getGrade());
             }
         }
       
         if(!found)
         System.out.println(\"Student not found!!\");
     }
     public static void main(String []args){
                 Scanner read=new Scanner(System.in);
          Driver driver=new Driver();
          driver.read();
          driver.display();
          System.out.println(\"Average of labs: \"+driver.calAvg());
          System.out.println(\"Max of average labs: \"+driver.getMax());
          System.out.println(\"Min of average labs: \"+driver.getMin());
          boolean flag=true;
          do
          {
              System.out.print(\"\ Enter name of student: \");
              String search=read.next();
              driver.search(search);
          }while(flag);
         System.out.println(\"Hello World\");
      }
 }
class Grades
 {
     String name;
     double grade;
     Grades(String n,double l)
     {
         name=n;
         grade=l;
     }
     public String toString()
     {
         return \"\ Name: \"+name+\"\\tGrade: \"+grade;
     }
     public String getName()
     {
         return name;
     }
     public double getGrade()
     {
         return grade;
     }
     public void setGrade(double g)
     {
         grade=g;
     }
 }
Post another question for arraylist implementation



