Reputation: 13
I need help figuring out what is wrong with my code. I was trying to use driver1.DriverExam(answers) to call object.method(array) in hopes that it would be able to properly grade the user's input, but that produces errors. How do I properly call driver1.DriverExam(answers) in the second file to execute the DriverExam constructor from the first file? I'm sure it's some simple fix that you all will notice within a minute of looking at it, but I can't seem to figure it out. Thank you in advance!
I have this so far (one file to process, the other as user input):
/**
DriverExam class
Chapter 8, Programming Challenge 5
*/
public class DriverExam
{
final int PASSING = 15; // Minimum # of correct answers to pass
// Array of correct answers
private char[] correct = { 'B', 'D', 'A', 'A',
'C', 'A', 'B', 'A',
'C', 'D', 'B', 'C',
'D', 'A', 'D', 'C',
'C', 'B', 'D', 'A' };
//step 1: declare an array to hold the student's answers
private char[] studentAnswers = new char[20];
//step 2: declare two int variables, one is to hold the number of correct answers.
// The other is to hold the number of incorrect answers.
private int correctAnswers = 0;
private int incorrectAnswers = 0;
/**
step 3: The constructor copies an array of answers
to the student field.
@param s The array of answers to copy.
*/
public DriverExam(char[] answers)
{
studentAnswers = answers;
}
/**
step 4: The gradeExam method determines the number of
correct and incorrect answers. */
public void gradeExam()
{
for (int k = 0; k < correct.length; k++)
{
if (studentAnswers[k] == correct[k])
{
correctAnswers += 1;
}
else
{
incorrectAnswers += 1;
}
}
}
/**
step 5: The passed method determines whether the student
passed or failed the exam.
@return true if the student passed, false otherwise.
*/
public boolean passed()
{
if (correctAnswers >= 15)
{
return true;
}
return false;
}
/**
step 6: The totalCorrect method returns the number of
questions correctly answered.
@return The number of questions correctly answered.
*/
public char[] totalCorrect()
{
return correctAnswers;
}
}
/**
DriverTest program
Chapter 8, Programming Challenge 5
*/
import java.util.Scanner;
public class DriverTest
{
public static void main(String[] args)
{
String input; // To hold keyboard input
final int NUM_ANSWERS = 20; // Number of answers
char[] answers = new char[NUM_ANSWERS]; // Array to hold answers
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the user's answers to the questions.
System.out.println("Enter your answers to the " +
"exam questions. (Make sure " +
"Caps Lock is ON!)");
//step 7: Get the student's answers to the questions
for (int x = 0; x < NUM_ANSWERS; x++)
{
System.out.print("Enter answer to question " + (x + 1) + ": ");
answers[x] = keyboard.next().charAt(0);
}
//step 8: Create a DriverExam object.
DriverExam driver1 = new DriverExam(answers);
//step 9: Display a report to print the number of correct answers,
//the number of incorrect answers, and whether the student passes the exam or not by calling
//gradeExam, totalCorrect, and passed methods.
driver1.gradeExam();
System.out.println("The number of correct answers is: " + driver1.totalCorrect());
System.out.println("The number of incorrect answers is: " + (NUM_ANSWERS - driver1.totalCorrect()));
if (driver1.passed() == true)
{
System.out.println("Congratulations! You have passed!");
}
else
{
System.out.println("You have failed.");
}
}
}
Upvotes: 0
Views: 223
Reputation: 6451
I see two problems, first ..
public char[] totalCorrect() {
return correct;
}
I think it should return correctAnswers
as per your usage of that function and comment on it. Remember you need change the return type as well. Second, the way you create the DriverExam
object. It should be
DriverExam driver1 = new DriverExam(answers);
Upvotes: 1
Reputation: 285405
You should use the DriverExam constructor that allows you to pass in answers, not the default constructor as you're doing.
i.e., not this:
DriverExam driver1 = new DriverExam();
driver1.DriverExam(answers); // this makes no sense and is not legal Java
But this:
DriverExam driver1 = new DriverExam(answers);
Upvotes: 1