0% found this document useful (0 votes)
2 views

Java Lab 12

The document provides instructions for a Java lab focused on arrays, loops, and file handling. It outlines the creation of a Java program to grade multiple-choice tests using a predefined answer key stored in an array, and student responses stored in a text file. The lab emphasizes using loops for grading and includes example code snippets for reading files and comparing answers.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Lab 12

The document provides instructions for a Java lab focused on arrays, loops, and file handling. It outlines the creation of a Java program to grade multiple-choice tests using a predefined answer key stored in an array, and student responses stored in a text file. The lab emphasizes using loops for grading and includes example code snippets for reading files and comparing answers.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Lab 12 – Arrays, loops and files

 Create a new Java file (empty Java file), save it to your H: drive, call it lab12
 Insert the following code to set up your program:
 Be sure to copy the test text files to the location where you saved your Java file

import java.util.Scanner;

import java.io.*;

public class lab12 {

public static void main (String args[]) throws IOException{

File doc = new File("lab12pt1.txt");

Scanner studentGrades = new Scanner(doc);

String[] testAnswers = {"A","C","D","B","C","D","B","A","D","A"};

// Insert your while loop code here

studentGrades.close();

100pt:

 You are a lazy teacher with a lot of tests to grade. Luckily, you know how to program to make
your life somewhat easier.
 The test had 10 multiple choice questions, with answers A, B, C or D. The key to this part of the
test is listed in the String array testAnswers.
 All of the student answers have been stored in a text file, along with their name. An example
line of text from the file looks like this:

S631092,A,C,D,B,C,D,B,A,D,A

 Using the testAnswers array as a key, grade every student in the test and give them a
percentage grade. You must use a loop to do the comparisons. Your example output for one
student should look as follows:

S631092: 100%

Notes:
String split():

String line = "Edgeley, Ian";


String[] splitLine = line.split(",");
System.out.println(splitLine[0]); //Prints "Edgeley"

String comparisons:

if(splitLine[2].equals(testAnswers[0]){

// Do Something

Use the following while loop to read in the file, line by line:

while(fileInput.hasNextLine()){

String line = fileInput.nextLine();

// Do whatever you want with the String variable line

You might also like