Write a Java application that does each of the following: For each student Reads
ID: 3762913 • Letter: W
Question
Write a Java application that does each of the following:
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 an arrayList and do NOT use multiple arrays.
Explanation / Answer
#include <iostream>
#include <cstdio>
struct Student
{
void AssignEvals(char, FILE*);
public:
Student();
int Quiz;
int Assign;
int Labs;
int Tests;
void GetEvals(FILE*);
};
void Student::AssignEvals(char Letter, FILE* ReadFrom)
{
switch(Letter)
{
case 'q':
fscanf(ReadFrom, "%d", &Quiz);
break;
case 'a':
fscanf(ReadFrom, "%d", &Assign);
break;
case 'l':
fscanf(ReadFrom, "%d", &Labs);
break;
case 't':
fscanf(ReadFrom, "%d", &Tests);
break;
default:
break;
}
}
Student::Student()
{
Quiz = Assign = Labs = Tests = 0;
}
void Student::GetEvals(FILE* ReadFrom)
{
char symbol;
do
{
fscanf(ReadFrom, "%c", &symbol);
symbol = tolower(symbol);//convert everything to lowercase
AssignEvals(symbol, ReadFrom);
} while (symbol != 'f' && !feof(ReadFrom));
}
int main(int argc, char *argv[])
{
if (argc<2)
{
std::cerr << "Format for running program: " << *argv << " filename ";
return 1;
}
FILE* readStream;
readStream = fopen(argv[1], "r+");
if (NULL == readStream)
{
std::cerr << "Corrupt file. Aborting ";
return 1;
}
else
{
Student myStu;
myStu.GetEvals(readStream);
cout <<endl << myStu.Quiz << " Quizzes, " << myStu.Assign << " Assignments, "
<< myStu.Labs << " Labs, " << myStu.Tests << " Tests" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.