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

Application requiring modularity with arrays. Both screen and file i/o should be

ID: 3562144 • Letter: A

Question

Application requiring modularity with arrays. Both screen and file i/o should be used. Create a Java Source file : Mixusername3.java This lab you will again be dealing with both screen and file i/o. You will be managing a gradebook. The is "Student" is provided, feel free to look over it. There has to be a separate method which reads in the numbers in the comma separated variable file into an array. This method will be invoked initially in your main method which reads information from the file "grades.csv" into an array. Once the values are read, objects of type "Student" will be created with the data and stored in an array of type Student. public static void reader(Student[] arr); The menu will have options. (The menu will have to be iterated until the user selects to "Exit" ) Search for a particular student and display all of his grades (input: sNumber) Find the average grade for a given student (input: sNumber) Display the sNumber and average grade for every student Write the sNumber and average grade for every student to a file with the user inputted file name. Exit The menu should prompt the user to input a selection to determine which operation to be performed. If the user selects an appropriate option, then the program will prompt for the values if needed any respectively. After storing the values you should run them through the appropriate method listed under and show the result of that operation:

Explanation / Answer

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.io.File;

class Student
{

/**
* Array of integers, each
* one specifying a different grade percentage.
*/
public int grades[];

/**
* The unique identifier for each
* student object.
*/
public int sNumber;

/**
* Class constructor specifying the sNumber and grades.
*/
public Student(int sNumber, int[] grades)
{
this.sNumber = sNumber;
this.grades = new int[10];
for(int i=0; i<10; i++)
{
this.grades[i] = grades[i];
}
}

/**
* Returns the sNumber of the object
*
*/
public int getSNumber() {
return this.sNumber;
}

/**
* Returns the grades of the object
*
*/
public int[] getGrades()
{
return this.grades;
}

/**
* Returns the ith grade of the object
*
* @param i the index of the grade to be returned
*/
public int getGrade(int i)
{
return this.grades[i];
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
String ret="";
ret += Integer.toString(this.sNumber);
for(int i=0; i<10; i++)
{
ret += " " + Integer.toString(this.grades[i]);
}
return ret;
}
}

public class MixUsername3
{

public static void reader(Student stArray[]) throws FileNotFoundException, IOException
{

FileReader fr = new FileReader("grade.csv.txt");
BufferedReader br = new BufferedReader(fr);

String s = br.readLine();
int temp = 0;

while(s != null)
{
String line[] = s.split(",");

// 1st Argument is IdNumber
int idNum = Integer.parseInt(line[0]);
int grade[] = new int[10];
for(int i = 0; i < 10; i++)
{
int t = Integer.parseInt(line[i+1]);
grade[i] = t;
}

// create Student Object

Student st = new Student(idNum,grade);
stArray[temp] = st;

temp++;

// Read the Next Line in File

s = br.readLine();

}


}


public static Student search(Student stArray[],int idNumber)
{
for(Student s : stArray)
{
if(s.sNumber == idNumber)
{
return s;
}
}
return null;

}

public static double getAverage(Student stArray[], int idNumber)
{
double avg = 0;
for(Student s : stArray)
{
if(s.sNumber == idNumber)
{
int temp[] = s.getGrades();
System.out.println(temp);
int sum = 0;
for(int t : temp)
sum+=t;

avg = sum/10;

}
}

return avg;
}

public static String averagesToString(Student stArray[])
{
String ret = "";

for(Student s : stArray)
{
int temp[] = s.getGrades();

int sum = 0;
for(int t : temp)
sum+=t;

double avg = sum/10;

ret+=s.sNumber+" , "+avg+" ";


}

return ret;

}

public static void writeAverages(String avgOfStudents, String fileName) throws Exception
{
       FileWriter fw = new FileWriter(new File(fileName),true);
       BufferedWriter bw = new BufferedWriter(fw);
       bw.write(avgOfStudents);
       bw.newLine();
       bw.flush();

       bw.close();


}

public static void main(String args[]) throws FileNotFoundException, IOException
{
Scanner sc = new Scanner(System.in);

   // as per Test Cases i am taking 15 Students

Student[] stArray = new Student[15];

// call to separate method which reads data from FILE and Crete Student Objects

reader(stArray);

         

         
       do
       {

           try
           {

              
   System.out.println(" Menu : ");
       System.out.println("1.Search Student 2.Average Grade for Student 3.Display All Students 4.Write Student data in to file 5.Exit ");

System.out.print(" Enter Your Choice : ");
int choice = sc.nextInt();

switch(choice)
{
case 1 :
System.out.print("Enter Student Id Number : ");
int idNum = sc.nextInt();
Student s = search(stArray,idNum);

System.out.println(s.toString());
              
break;

case 2 :
System.out.print("Enter Student Id Number : ");
int idNum2 = sc.nextInt();
double avg = getAverage(stArray,idNum2);

System.out.println("Average : "+avg);
               break;

case 3 :
String avgOfAllStudents = averagesToString(stArray);
System.out.println("Average Of All Students : "+avgOfAllStudents);
               break;

case 4 :
               System.out.println("Enter File name : ");
           String fileName = sc.next();
               String avgOfStudents = averagesToString(stArray);

writeAverages(avgOfStudents,fileName);

break;

case 5 :
System.exit(0);

default :
System.out.println("Please Enter Valid Choice ");

             

}

       }
           catch (Exception e)
           {
e.printStackTrace();
               System.out.println(" Student Not Found ");
           }

       } while (true);



      

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote