Question

In: Computer Science

Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste...

Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance !

Lab 5 – Class Statistics

Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information:

-A table containing the student name, total points they earned and the letter grade that would be given.

-The student with the highest score and the average points earned and average letter grade given

You may use single single dimension arrays, multi-dimension arrays or both.

Capture screen output of your report and save as GradeReport.txt

(Optionally, you may add code to create this file using java; see note below)

Name your program Grade.java. Upload files Grade.java, and GradeReport.txt to Canvas.

Note: the data files mentioned downloaded from the Canvas files area.

Refer to the files TipsForLab5.pdf, ReadData.txt, WriteData.txt for additional information that may be usefull in completing this lab.

Solutions

Expert Solution

The required code is given below in case of any doubts you can ask me in comments and please do upvote.

Main.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
public static void main(String args[]) throws FileNotFoundException {
Scanner read = new Scanner(new File("Grades.txt"));

  
int numberOfStudents = 0;
while (read.hasNextLine()) {
numberOfStudents += 1;
read.nextLine();
}
read.close();

  
Grade[] students = new Grade[numberOfStudents];

  
read = new Scanner(new File("Grades.txt"));
for (int i = 0; i < students.length; ++i) {
String line = read.nextLine();
if (line == null || line.isEmpty()) continue;
String[] tokens = line.split("\\s+");
double[] scores = new double[5];
for (int j = 0; j < 5; ++j) scores[j] = Double.parseDouble(tokens[2 + j]);
students[i] = new Grade(tokens[0], tokens[1], scores);
}
read.close();

  
System.out.format("%s\t\t%5s\t%5s\t%5s\t%5s\t%5s\t%5s\t%5s\n",
"Name", "Test1", "Test2", "Test3", "Test4", "Test5",
"Total", "Grade");

  
for (int i = 0; i < students.length; ++i) {
System.out.println(students[i]);
}

System.out.println("Class average = " + Grade.getClassAverage(students));
}
}

Grade.java

import java.util.Arrays;

public class Grade {
private String firstName, lastName;
private double testScores[] = new double[5];

public Grade() {}

  
public Grade(String f, String l, double t[]) {
firstName = f;
lastName = l;
System.arraycopy(t, 0, testScores, 0, 5);
}

public Grade(Grade gradebook) {
firstName = gradebook.getFirstName();
lastName = gradebook.getLastName();
System.arraycopy(gradebook.getTestScores(), 0, testScores, 0, 5);
}

public static double getClassAverage(Grade[] students) {
if (students == null || students.length == 0) throw new IllegalArgumentException();
double sum = 0.0;
for (int i = 0; i < students.length; ++i) {
sum += students[i].getAverage();
}
return sum / students.length;
}

public double getAverage() {
double avg = 0;
for (int i = 0; i < 5; i++) {
avg += testScores[i];
}
avg /= 5;
return avg;
}

public double getTotal() {
double tot = 0;
for (int i = 0; i < 5; i++) {
tot += testScores[i];
}
return tot;
}

public String getGrade() {
double avg = getAverage();
if (avg >= 90) {
return "A";
} else if (avg >= 80) {
return "B";
} else if (avg >= 70) {
return "C";
} else if (avg >= 60) {
return "D";
} else {
return "F";
}
}

public Grade getCopy() {
return new Grade(this);
}

public boolean isEqual(Grade gradebook) {
return firstName.toLowerCase().equals(gradebook.getFirstName())
&& lastName.toLowerCase().equals(gradebook.getLastName())
&& Arrays.equals(testScores, gradebook.getTestScores());
}

public String toString() {
return String.format("%s %s\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%5.2f\t%s\n",
firstName,
lastName,
testScores[0],
testScores[1],
testScores[2],
testScores[3],
testScores[4],
getTotal(),
getGrade());
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public double[] getTestScores() {
return testScores;
}

public void setTestScores(double[] testScores) {
this.testScores = testScores;
}
}

Grades.txt

Jack Johnson 85 83 77 91 76
Lisa Aniston 80 90 95 93 48
Andy Cooper 78 81 11 90 73
Ravi Gupta 92 83 30 69 87
Bonny Blair 23 45 96 38 59
Danny Clark 60 85 45 39 67
Saman Kennedy 77 31 52 74 83
Robin Bronson 93 94 89 77 97
Sheila Sunny 79 85 28 93 82
Kiran Smith 85 72 49 75 63

OUTPUT


Related Solutions

R studio questions Write up your answers and paste the R code Copy and paste all...
R studio questions Write up your answers and paste the R code Copy and paste all plots generated. First create a sample drawn from a normal random variable. R has many distributions for which you can get probabilities and draw random numbers. We are going to use the normal. Go to help in R and type in rnorm. You will see a write up for functions associated with the normal distribution. dnorm is the density; pnorm is the probability distribution...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is...
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code. import java.util.*; public class OddManOut {    public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many random Integers to produce?"); int num = sc.nextInt();    ArrayList<Integer> randomInts = createRandomList(num); System.out.println("The random list is: "); System.out.println(randomInts);    removeOdds( randomInts ); System.out.println("The random list with only...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such...
It is straightforward to copy-paste code to achieve repetitive actions, what is the downside of such an approach?
There are two answer already posted up on this website please do not copy and paste...
There are two answer already posted up on this website please do not copy and paste from one of those I can not use those, Please help I have posted this before but all I get is a copy paste, Please no less then 3 paragraphs the more the better THank you for your help and I will rate well. Select a familiar or local Common Resource or Public Good (see text for examples to consider). Assess the existence of...
Need IN JAVA Please DO NOT COPY AND PASTE old solutions WILL UPVOTE The Babysitters Club...
Need IN JAVA Please DO NOT COPY AND PASTE old solutions WILL UPVOTE The Babysitters Club Employment Agency has decided to computerize their payroll. A babysitter earns a specific fee per hour until 9:00 PM (while the children are awake and active), a lower rate between 9:00 PM and midnight (after bedtimes and the babysitter can study), and a higher fee after midnight (when babysitting cuts into prime sleeping time). They maintain two datafiles are one for the employees and...
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp...
4. Create a new project AccountPolymorphism and copy and paste the code from the attached AccountPolymorphism.cpp file. Compile and run. You will see that it is not showing output for Base class pointer to base class object data, not showing derived class output completely(missing word "Saving" in the output) and not showing any output for Base class pointer to derived class object(Saving). Fix these issue and submit the output. // AccountPolymorphism.cpp : This file contains the 'main' function. Program execution...
Directly copy the source code and paste into the Word file. Screenshot of running result must...
Directly copy the source code and paste into the Word file. Screenshot of running result must be presented. 1. (20 points) Write the “Hello, world!” program. 2. (30 points) Write a program to calculate the sum from -5 to10. Use the for loop to do the calculation. 3. (20 points) Write a complete C++ program that asks the user to enter the necessary information about the cylinder, calculate the volume in a function (named as calculate_vol, using reference to pass...
Use R to answer the following question. Copy and paste the code and answer from R...
Use R to answer the following question. Copy and paste the code and answer from R into your paper. On the average,five cars arrive at a particular car wash every hour. Let X count the number of cars that arrive from 10 AM to 11 AM. Then X ∼pois(lambda = 5). Also, μ = σ2 = 5.  What is the probability that no car arrives during this period?  Suppose the car wash above is in operation from 8AM...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
ONLY NEED A-D. DON'T POST A PICTURE! Copy and Paste Introduction: Having the ability to effectively...
ONLY NEED A-D. DON'T POST A PICTURE! Copy and Paste Introduction: Having the ability to effectively communicateis one of the most important skills a business executive can possess. As French businesswoman and author Mirelle Guilliano has said, “Intelligence, knowledge or experienceare important and might get you a job, but strong communication skills are what will get you promoted.” My own business experience supports this statement. By the time individuals have a few years of experience, they have great technical skills...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT