Question

For this lab assignment, you will be writing a few classes that can be used by...

For this lab assignment, you will be writing a few classes that can be used by an educator to grade multiple choice exams. It will give you experience using some Standard Java Classes (Strings, Lists, Maps), which you will need for future projects. The following are the required classes:

  1. Student – a Student object has three private instance variables: lastName, a String; firstName, a String; and average, a double. It has accessor methods for lastName and firstName, and an accessor and mutator method for average. It has two constructors: a default constructor that creates a default Student, and one that has two String parameters to initialize firstName and lastName. The initial average of all Student objects is 0.
  1. UnitTest – a UnitTest object represents an exam for a class of Students. It has two instance variables: input, a Scanner object, and answers, an ArrayList of Strings that represents the correct answers on the exam. In the constructor, you must read from the file “answers.txt” which represents the correct answers for this exam. You must write one method in this class: calculateGrade, which has an ArrayList of Strings as a parameter. This ArrayList has all of the Students answers for their exam. To grade the exam, you compare the answers and, if they are equal, count it as correct. The method returns a percentage, as a double in the range [0.0...100.0].
  1. UnitTestRunner – A UnitTestRunner is the runner for the lab. There is a lot of leeway as to how you can define this class. It needs to do the following:
    • Create a UnitTest object.
    • Read in the Student information from the file allExams.txt. The file is of the format last name, first name, and answers for their exam (always ten letters).
    • Create Student objects by using the last name and first name String objects read from the file.
    • Add the Student objects to an ArrayList of Students.
    • Add all answers to an ArrayList of Strings.
    • Add these answers to a HashMap. The HashMap is of type >. The String key must be the concatenation of a Student’s last name followed by their first name, with no spaces and in lower case. Hence, the String for John Smith would be smithjohn. The value to put into the HashMap is their ArrayList answers.
    • Grade all of the exams. Go through the ArrayList of Student objects to get the answers from the HashMap, and use the calculateGrade method from the UnitTest object you created to grade the Student’s exam. Use the mutator method for average in the Student object to save the grade as the average.
    • Finally, print all of the Students’ names, in the form first name and last name, separated by a space, and their grade after.
  1. Can the UnitTest object be modified easily? As it is, it only initializes an answer key and calculates the percentage correct. Are there any other functions that you think a UnitTest may hold responsibility? Think about if you were writing a more robust program for actual teachers – what other functions may prove useful to them? Add this response to your output file.

AllExam.txt

Tobacco Christian A B C D D C B A A B
Russo James D A C B A C A D A A 
Smith John A A A A A A A A A A
Henderson James B B B B B B B B B B
Russo Michael C C C C C C C C C C
DeMartino John A B C D D C B A C D
Scafuri Frank A B C D A C B A A D

Answer.txt

A B C D D C B A A B
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Student.java

public class Student {
   String lastName;
   String firstName;
   double average;
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public double getAverage() {
       return average;
   }
   public void setAverage(double average) {
       this.average = average;
   }
  
   public Student() {
      
   }
  
   public Student(String lastName, String firstName) {
       this.lastName=lastName;
       this.firstName=firstName;
       this.average=0;
   }

}

UnitTest.java

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class UnitTest {
  
   Scanner input;
   ArrayList<String> answers;
  
   public UnitTest() {
      
       answers = new ArrayList<>();
      
       String[] fields;
       FileInputStream filestream;
       String path = "Answers.txt";
         
       try {
               filestream = new FileInputStream(path);
               BufferedReader br = new BufferedReader(new InputStreamReader(filestream));

               String str = br.readLine();
  
fields = str.split(" ");

for(int i=0;i<10;i++)
   answers.add(fields[i]);
              
               br.close();

           } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
         
      
      
      
   }
  
   public double calculateGrade(ArrayList<String> answer) {
      
       int count=0;
       for(int i=0;i<answers.size();i++) {
           if(answers.get(i).compareTo(answer.get(i))==0)
               ++count;
       }
      
       return count*100/answer.size();
      
   }

}

UnitTestRunner.java

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class UnitTestRunnner {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       UnitTest unit = new UnitTest();
      
       String name ;
      
       ArrayList<String> grades;
      
       ArrayList<Student> students = new ArrayList<>();
      
       HashMap<String,ArrayList<String>> map = new HashMap<>();
      
       String[] fields;
      
      
       FileInputStream filestream;
       String path = "AllExams.txt";
         
       try {
               filestream = new FileInputStream(path);
               BufferedReader br = new BufferedReader(new InputStreamReader(filestream));

               String str = br.readLine();

  
               while (str != null) {
                  
                   name = new String();
                   grades=new ArrayList<String>();
                  
                   fields = str.split(" ");
                  
                   name= fields[1]+fields[0];
                  
                   students.add(new Student(fields[0],fields[1]));
                  
                   for(int i=0;i<10;i++) {
                       grades.add(fields[i+2]);
                   }
                  
                   map.put(name, grades);
                  
                   str = br.readLine();  
               }

               br.close();

           } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
         
       ArrayList<ArrayList<String>> values = new ArrayList<>(map.values());
         
       for(int i=0; i<students.size();i++) {
             
           System.out.println(students.get(i).getFirstName()+" " + students.get(i).getLastName()+ " " + unit.calculateGrade(values.get(i)));
       }
         
      
   }

}

Output

Christian Tobacco 30.0
James Russo 30.0
John Smith 80.0
James Henderson 20.0
Michael Russo 100.0
John DeMartino 80.0
Frank Scafuri 30.0

Screenshot

Student.java UnitTest.java UnitTestRunnner.java X while (str != null) { name = new String(); grades=new ArrayList<String>();

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful

Add a comment
Know the answer?
Add Answer to:
For this lab assignment, you will be writing a few classes that can be used by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by...

    Java: student directory GUI You need to implement three classes: Person Student StudentDirectory StudentMain Start by implementing Person and Student classes. Once you are sure you can serialize and deserialize and ArrayList of Students to and from a file, move on to building the GUI application. Person: The Person class should implement serializable interface. It contains the following: Person's first name (String) Person's last name (String) Person's id number Person's date of birth (Date) public String toString(): This method method...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file:...

    help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file: UWECPerson.java uWECPerson.java UWECPerson uwecld: int firstName: String lastName : String +UWECPerson(uwecld: int, firstName : String, lastName: String) +getUwecid): int setUwecld(uwecld: int): void +getFirstName): String +setFirstName(firstName: String): void getLastName): String setLastName(lastName: String): void +toString): String +equals(other: Object): boolean The constructor, accessors, and mutators behave as expected. The equals method returns true only if the parameter is a UWECPerson and all instance variables are equal. The...

  • using C++ language!! please help me out with this homework In this exercise, you will have...

    using C++ language!! please help me out with this homework In this exercise, you will have to create from scratch and utilize a class called Student1430. Student 1430 has 4 private member attributes: lastName (string) firstName (string) exams (an integer array of fixed size of 4) average (double) Student1430 also has the following member functions: 1. A default constructor, which sets firstName and lastName to empty strings, and all exams and average to 0. 2. A constructor with 3 parameters:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT