0

I am new to inheritance and am having issues finishing an assignment. My error is that I am receiving a stack overflow error.

The assisgnment is consisting of a class heirarchy to print a test and answer key each different class is a different type of question. My program can print an object quesiton but not a fill in the blank question

Any help or point in the right direction would be appreciated.

Driver.java

package assignment9testpapers;

public class Driver {
    public static void main(String[] args) {
        Test trial = new Test( 3, 3);
        ObjectiveQuestion obj = new ObjectiveQuestion( 3,  2,  1, "what is 1 + 1",  "2");
        trial.addObjectiveQuestion(obj);
        ObjectiveQuestion fitbl = new FillInTheBlankQuestion(3 ,2 ,1 , "Who is the professor of 1068", "John Fiore");
        trial.addFillInTheBlankQuestion(fitbl);
        trial.printTest();
        trial.printTestAnswers();
    }
}

question.java

1 package assignment9testpapers;
2
3 public class Question {
4   private int points;
5   private int difficulty;
6   private int answerSpace;
7   private String questionText;
8   
9   public final int MIN_DIFFICULTY = 1;
10  public final int MAX_DIFFICULTY = 5;
11
12  public Question(int points, int difficulty, int answerSpace, String 
 questionText) {
13      this.points = points;
14  this.answerSpace = answerSpace;
15      this.questionText = questionText;
16      
17      if(difficulty>=MIN_DIFFICULTY && difficulty <= MAX_DIFFICULTY) {
18          this.difficulty=difficulty;
19      }else if(difficulty<MIN_DIFFICULTY) {
20          this.difficulty=MIN_DIFFICULTY;
21      } else {
22          this.difficulty=MAX_DIFFICULTY;
23      }
24  }
25  
26  public String toString(){
27      return "Points" + points+ "difficulty" + difficulty +"question" + questionText;
28  }
29  
30  public String getQuestionText() {
31      return questionText+makeAnswerSpace();
32  }
33  
34  public int getPoints() {
35      return points;
36  }
37  
38  private String makeAnswerSpace() {
39      String space="";
40      for(int i = 0; i<answerSpace; i++) {
41          space+='\n';
42      }
43      return space;
44  }

ObjectiveQuestion.java

package assignment9testpapers;

public class ObjectiveQuestion extends Question {
    private String correctAnswer;

    public ObjectiveQuestion(int points,int difficulty, int answerSpace, String questionText, String correctAnswer ) {
        super(points, difficulty, answerSpace, questionText);
        this.correctAnswer=correctAnswer;
    }

    public String getCorrectAnswer() {
        return correctAnswer;
    }


}

test.java

package assignment9testpapers;

public class Test {
    private String[] questions;
    private String[] answer;
    private int totalPoints;

    private int questionCount = 0;

    public Test(int numQuestions, int totalPoints) {
        this.questions = new String[numQuestions];
        this.answer = new String[numQuestions];
        this.totalPoints=totalPoints;
    }

    public void addObjectiveQuestion(ObjectiveQuestion q) {
        questions[questionCount] = q.getQuestionText();
        answer[questionCount]=q.getCorrectAnswer();
        questionCount++;
    }

    public void addMultipleChoiceQuestion(MultipleChoiceQuestion q) {
        questions[questionCount] = q.getQuestionText();
        answer[questionCount]=q.getCorrectAnswer();
        questionCount++;
    }

    public void addFillInTheBlankQuestion(ObjectiveQuestion q) {
        questions[questionCount] = q.getQuestionText();
        answer[questionCount]=q.getCorrectAnswer();
        questionCount++;
    } 




    public void printTest() {
        for (int i = 0; i<questions.length; i++) {
            System.out.println(questions[i]);
        }
    }

    public void printTestAnswers() {
        for (int i = 0; i<answer.length; i++) {
            System.out.println(answer[i]);
        }
    }   
}

FillInTheBlankQuestion.java

package assignment9testpapers;

public class FillInTheBlankQuestion extends ObjectiveQuestion{

    public FillInTheBlankQuestion(int points,int difficulty, int answerSpace, String questionText, String correctAnswer ) {
        super(points, difficulty, answerSpace, questionText, correctAnswer);
    }

    public String getQuestionText() {
        return "______________" + getQuestionText();
    }
    public String getCorrectAnswer() {
        return "______" + getQuestionText() + getCorrectAnswer();
    }


}

error message:

Exception in thread "main" java.lang.StackOverflowError
    at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:124)
    at assignment9testpapers.FillInTheBlankQuestion.getQuestionText(FillInTheBlankQuestion.java:10)

1 Answers1

0

In "public class FillInTheBlankQuestion" method "public String getQuestionText() you are calling the method inside itself, causing in infintite loop. If you are trying to use the method from superclass use super.getQuestionText().

Richard
  • 5
  • 1