Basically I'm doing the old driver exam program for a uni course and I've drafted out the whole class. When I try to compile it, if working in command prompt, I get the error:
missing return statement
and in Eclipse:
This method must return a result of type int` pertaining to my methods re missed questions.
Now I think I already knew what the problem was before trying to compile - my method is an int
type, and because my answers "array is char", when I try to return 'index + 1' or 'index++) it's returning the actual element, a character (i.e. answer a,b,c, d). What I wanted to do was to return the subscript number + 1 (to remove out by one error), so when I write the program, I can system out print 'you missed question 1, 3, 5 etc'.
I realize there are probably a million other errors in this code, but right now I'm just hoping someone can help me with this one. Realize it's probably simple and stupid but have been reading forums and my textbooks for hours and can't figure this out. Maybe I'm trying to simplify too much by just using the subscript + 1 as a means of showing question number missed.
public class DriverExam {
private char[] rightAnswers = { 'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a',
'c', 'd', 'b', 'c', 'd', 'a', 'd', 'c', 'c', 'b', 'd', 'a' }; //Answers
to test.
char[] Answers; //Student answer input.
int[] missed = {}; //Array for missed questions.
int correct = 0; //Create and initialise to 0.
int qMissed = 0; //Create and initialise to 0.
/**
Constructor that accepts array of answers and copies to the answers array
field.
@parem ans The array of student driver answers */
public DriverExam(char[] ans)
{
Answers = ans;
}
/**
An int array containing the question numbers of those questions that the
student missed.
*/
public int questionsMissed()
{
for (int index = 0; index < 20; index++) //Ask program to step through
each element in array Answers.
{
if (Answers[index] == 0) //I'm assuming here that a question not
answered = 0, not null as it would for string.
return index + 1; //I want to return the subscript assigned to
any questions that = 0 ie haven't been answered, plus 1 to avoid out
by one error.
}
}
public int qMissed()
{
for (int index = 0; index < 20; index++) //Ask program to step through
each element in array Answers.
{
if (Answers[index] == 0) //Asking it to repeat process above.
return index++; //I want to ask it to take the subscript and add
1 to the existing subscript for all additional missed questions.
}
}
/**
A method that returns the total number of correctly answered questions.
@return Returns the number of correctly answered questions.
*/
public int totalCorrect()
{
for (int index = 0; index < Answers.length; index++)
{
if (Answers[index] == rightAnswers[index])
correct++;
}
return correct;
}
/**
A method that returns the total number of incorrectly answered questions.
@return Returns the number of incorrect answers.
*/
public int totalIncorrect()
{
int incorrect = (rightAnswers.length - (totalCorrect() + qMissed));
return incorrect;
}
/**
A method that returns true if the student passed the exam, or false if
the student failed.
*/
public boolean passed()
{
if(totalCorrect() >= 10); return true;
}
}