作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是继承新手,在完成作业时遇到问题。我的错误是收到堆栈溢出错误。
作业由类层次结构组成,用于打印测试和答案键,每个不同的类是不同类型的问题。我的程序可以打印对象问题,但不能打印填空问题
任何帮助或指出正确的方向将不胜感激。
驱动程序.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();
}
}
问题.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;
}
}
测试.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();
}
}
错误信息:
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:124)
at assignment9testpapers.FillInTheBlankQuestion.getQuestionText(FillInTheBlankQuestion.java:10)
最佳答案
在“public class FillInTheBlankQuestion”方法“public String getQuestionText() 中,您正在调用其自身内部的方法,导致无限循环。如果您尝试使用父类(super class)中的方法,请使用 super.getQuestionText()。
关于java - 如何让 FillInTheBlank 打印问题(继承),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795870/
我是一名优秀的程序员,十分优秀!