Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; import java.util.*; class StudentData{ private int id; private

ID: 3549223 • Letter: I

Question

import java.io.*;

import java.util.*;


   class StudentData{


private int id;

private String firstName;

private String lastName;

private double averageScore;

   char grade;

public StudentData()

{

id = 0;

   firstName = "";

   lastName = "";

   averageScore = 0.0;

   grade = 0;

}

public StudentData(int id, String firstName, String lastName, double averageScore)

{

this.id = id;

this.firstName = firstName;

this.lastName = lastName;

this.averageScore = averageScore;

}

public void setId( int id )

{

this.id = id;

}

public int getId()

{

return id;

}

public void setFirstName(String firstName)

{

this.firstName = firstName;

}

public String getFirstName()

{

return firstName;

}

public void setLastName(String lastName)

{

this.lastName = lastName;

}

public String getLastName()

{

return lastName;

}

public void setAverageScore(double averageScore)

{

this.averageScore = averageScore;

}

public double getAverageScore()

{

return averageScore;

}

public char getGrade()

{

char grade = 0;

      

      if (averageScore <= 59){

         grade = 'F';}

      

      else if (averageScore >= 60 && averageScore <= 69){

         grade = 'D';}

      

      else if (averageScore >= 70 && averageScore <= 79){

         grade = 'C';}   

         

      else if (averageScore >= 80 && averageScore <= 89){

         grade = 'B';}

           

      else if (averageScore >= 90 && averageScore <= 100){

         grade = 'A';}      

                     

      

return grade;

}

public String toString()

{

      return (id + " " + firstName + " " + lastName + " " + averageScore + " "  + grade + " ");

}

}//end of class

public class GradeBook{ //start of class


public static void main(String[] args) throws FileNotFoundException {//start of void


   Scanner in = new Scanner(new File("gradebook.txt"));

   Scanner local_in = new Scanner(System.in);

   StudentData[] local_StudentData = new StudentData[10];

   

   int studentdata_count = 0;

   

   while(in.hasNext()){

   

   int id = in.nextInt();

   String firstName = in.next();

   String lastName = in.next();

   double averageScore = in.nextDouble();

   

   local_StudentData[studentdata_count++] = new StudentData(id, firstName, lastName, averageScore);}

   

   boolean not_done = true;

   

   while(not_done) {

   

   System.out.println("");

   System.out.println("1: To display students information (id, name, score and grade)");

   System.out.println("2: To display the class average.");

   System.out.println("3: To find the student with the highest score and display his or her information.");

   System.out.println("0: To end the program.");

   

   int choice = local_in.nextInt();

   

   switch(choice) {//begin switch

   

      case 1:

         {//start case 1

            System.out.println("Student Information: ");

            

            for(int i=0; i<studentdata_count; i++)

            System.out.println(local_StudentData[i].getId()+" " +

            local_StudentData[i].getFirstName()+" " +

            local_StudentData[i].getLastName()+" " +

            local_StudentData[i].getAverageScore()+" " +

            local_StudentData[i].getGrade());

         }//end case 1

         break;

         

     ********** case 2:***********

         {//start case 2


         

         }

     ********** break;*************


      case 3:

         {//start case 3

            int grademax = 0;

            

            for(int i=1; i<studentdata_count; i++){//start for

            

            if(local_StudentData[i].getAverageScore()>local_StudentData[grademax].getAverageScore())

            grademax = i;

            }//end for

            

            System.out.println("The Student with the highest score is: ");

            System.out.println(

            local_StudentData[grademax].getId()+ " " +

            local_StudentData[grademax].getFirstName()+ " " +

            local_StudentData[grademax].getLastName()+ " " +

            local_StudentData[grademax].getAverageScore()+ " " +

            local_StudentData[grademax].getGrade());

         }//end case 3

         break;

         

       case 0: not_done = false;

       break;

       

       default: System.out.println("Invalid Choice");

       break;

       

            }//end of switch

         }//end of while

      }//end of public

   }//end of class        

  

         

Explanation / Answer

//You need to add two static variables to maintain class total and no of students. Please check my code below and let me know if you have doubts



import java.io.*;


import java.util.*;



class StudentData{



private int id;


private String firstName;


private String lastName;


private double averageScore;

public static double total=0;

public static int count=0;


char grade;


public StudentData()


{


id = 0;


firstName = "";


lastName = "";


averageScore = 0.0;


grade = 0;


}


public StudentData(int id, String firstName, String lastName, double averageScore)


{


this.id = id;


this.firstName = firstName;


this.lastName = lastName;


this.averageScore = averageScore;

total+=averageScore;

count++;


}


public void setId( int id )


{


this.id = id;


}


public int getId()


{


return id;


}


public void setFirstName(String firstName)


{


this.firstName = firstName;


}


public String getFirstName()


{


return firstName;


}


public void setLastName(String lastName)


{


this.lastName = lastName;


}


public String getLastName()


{


return lastName;


}


public void setAverageScore(double averageScore)


{


this.averageScore = averageScore;


}


public double getAverageScore()


{


return averageScore;


}


public char getGrade()


{


char grade = 0;


if (averageScore <= 59){


grade = 'F';}


else if (averageScore >= 60 && averageScore <= 69){


grade = 'D';}


else if (averageScore >= 70 && averageScore <= 79){


grade = 'C';}


else if (averageScore >= 80 && averageScore <= 89){


grade = 'B';}


else if (averageScore >= 90 && averageScore <= 100){


grade = 'A';}


return grade;


}


public String toString()


{


return (id + " " + firstName + " " + lastName + " " + averageScore + " " + grade + " ");


}


}//end of class


public class GradeBook{ //start of class



public static void main(String[] args) throws FileNotFoundException {//start of void



Scanner in = new Scanner(new File("gradebook.txt"));


Scanner local_in = new Scanner(System.in);


StudentData[] local_StudentData = new StudentData[10];


int studentdata_count = 0;


while(in.hasNext()){


int id = in.nextInt();


String firstName = in.next();


String lastName = in.next();


double averageScore = in.nextDouble();


local_StudentData[studentdata_count++] = new StudentData(id, firstName, lastName, averageScore);}


boolean not_done = true;


while(not_done) {


System.out.println("");


System.out.println("1: To display students information (id, name, score and grade)");


System.out.println("2: To display the class average.");


System.out.println("3: To find the student with the highest score and display his or her information.");


System.out.println("0: To end the program.");


int choice = local_in.nextInt();


switch(choice) {//begin switch


case 1:


{//start case 1


System.out.println("Student Information: ");


for(int i=0; i<studentdata_count; i++)


System.out.println(local_StudentData[i].getId()+" " +


local_StudentData[i].getFirstName()+" " +


local_StudentData[i].getLastName()+" " +


local_StudentData[i].getAverageScore()+" " +


local_StudentData[i].getGrade());


}//end case 1


break;


case 2:


System.out.println("Class Average: " + StudentData.total/StudentData.count);


break;



case 3:


{//start case 3


int grademax = 0;


for(int i=1; i<studentdata_count; i++){//start for


if(local_StudentData[i].getAverageScore()>local_StudentData[grademax].getAverageScore())


grademax = i;


}//end for


System.out.println("The Student with the highest score is: ");


System.out.println(


local_StudentData[grademax].getId()+ " " +


local_StudentData[grademax].getFirstName()+ " " +


local_StudentData[grademax].getLastName()+ " " +


local_StudentData[grademax].getAverageScore()+ " " +


local_StudentData[grademax].getGrade());


}//end case 3


break;


case 0: not_done = false;


break;


default: System.out.println("Invalid Choice");


break;


}//end of switch


}//end of while


}//end of public


}//end of class