Answer to Question #4926 in Java | JSP | JSF for diptika

Question #4926
Write a program which keeps track of scores for three test for a class of unknown number of students. Your program can assume that when receiving information scores for all 3 tests will be received at the same time. Your program should be able to have a user enter test scores, load test scores from a file, save them to a file, display test scores, provide the average score for a particular test and an average score across the 3 tests for a particular student. Your program must also utilize a menu which runs until told to quit. To accomplish this you will need 3 arrays where each array represents scores for one test and the index locations for the arrays will correspond with one student. For example, to refer to the test score for the 3rd test for student 7 you would call test3[7]. You can assume in your program that a user will not enter scores for more than 100 students.
1
Expert's answer
2012-06-12T07:02:28-0400
import java.io.*;
import java.util.Scanner;


public class Test {
private static int[] test1 = new int[100];
private static int[] test2 = new int[100];
private static int[] test3 = new int[100];
private static int[] average = new int[100];
private static Scanner sc = new Scanner(System.in);

public static void main(String[] args) throws IOException {
& System.out.print("Do you want enter score(1) or read from file(2): ");
& int choice = sc.nextInt();
& boolean status = true;
& switch (choice) {
case 1: while(status){
& enterScore();
& System.out.println("Do you want enter score of other student? (1 - yes, 2 - no)");
& choice = sc.nextInt();
& if(choice == 1){
status = true;
& }
& if(choice == 2){
status = false;
& }
}
break;
case 2:
readFromFile();
break;
& }
& while(true){
System.out.println("1. Enter other student\n"+"2. Display all scores\n"+"3. Save score into file\n"+
& quot;4. Display average score\n"+"5. Exit\n");
choice = sc.nextInt();
switch (choice) {
case 1 : enterScore();
& break;
case 2 : displayAll();
& break;
case 3 : saveScore();
& break;
case 4 : displayAverage();
& break;
case 5 : System.exit(0);
& break;
}
& }
}

private static void readFromFile() throws IOException {
& BufferedReader file = new BufferedReader(new FileReader(new File("input.txt")));
& String line;
& String[] lines;
& while((line = file.readLine()) != null){
lines = line.split(" ");
test1[Integer.parseInt(lines[0])] = Integer.parseInt(lines[1]);
test2[Integer.parseInt(lines[0])] = Integer.parseInt(lines[2]);
test3[Integer.parseInt(lines[0])] = Integer.parseInt(lines[3]);
& }
}

private static void displayAverage() {
& for(int i = 0; i < test1.length; i++){
if(test1[i] != 0 || test2[i] != 0 || test3[i] != 0){
average[i] = test1[i] + test2[i] + test3[i];
average[i] /= 3;
System.out.println("Student " + i + ":" + average[i]);
}
& }
}

private static void saveScore() throws IOException {
& BufferedWriter file = new BufferedWriter(new FileWriter(new File("save.txt")));
& for(int i = 0; i < test1.length; i++){
if(test1[i] != 0 || test2[i] != 0 || test3[i] != 0){
String line = Integer.toString(i)+" "+Integer.toString(test1[i])+" "+Integer.toString(test2[i])
+" "+Integer.toString(test3[i])+"\n";
file.write(line);
}
& }
& file.close();
}

private static void displayAll() {
& for(int i = 0; i < test1.length; i++){
if(test1[i] != 0 || test2[i] != 0 || test3[i] != 0){
System.out.println("Student " + i + ":");
System.out.println("Test 1 = "+test1[i]);
System.out.println("Test 2 = "+test2[i]);
System.out.println("Test 3 = "+test3[i]);
}
& }
}

public static void enterScore() {
& int choice = 0;
& int score1 = 0;
& int score2 = 0;
& int score3 = 0;
& System.out.print("Enter number of student(0-100): ");
& choice = sc.nextInt();
& System.out.print("Enter score for test 1: ");
& score1 = sc.nextInt();
& test1[choice] = score1;
& System.out.print("Enter score for test 2: ");
& score2 = sc.nextInt();
& test2[choice] = score2;
& System.out.print("Enter score for test 3: ");
& score3 = sc.nextInt();
& test3[choice] = score3;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS