java—我应该如何确切地回答这个编程问题?

ioekq8ef  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(291)

问题考虑一次考试有4道试题。一页由一个问题组成,是一个多选题。您可以使用特定命令在问题之间前后移动。每一页都应表示为一个类,最后一页是您在这些问题中选择的内容的摘要。写一个程序,让你用面向对象的方法来实现。
命令-用户可以在程序中使用

start() - To start the test and the first page of the test should be given once this command run.
next_page() - To move to the next page
back_page() - To move back a page
select(int i) - Select an answer on the current page

我被赋予上述问题,我需要写一个程序,工作的意图。这是我到目前为止的情况。我可以自由设计的程序,任何方式,我想没有任何限制。如果我下面的设计是完全错误的,请随时告诉我该如何处理这个问题。
我的问题课是这样的:

class Question1
int answer;
void display(){
    print("This is Question1")
    print("1. Answer")
    print("2. Answer")
    print("3. Answer")
    print("You have selected:" + answer);
}
void select(int i){
  this.answer = i;
}

在其他问题课上。它看起来相似,但问题和选择不同。
在小结课上,它将打印出我选择的内容

class Summary() {
   void display(){
      print("Summary")
      print("In question 1, you've selected: " + Question1.answer);
      print("In question 2, you've selected: " + Question2.answer)
      print("In question 3, you've selected: " + Question3.answer)
      print("In question 4, you've selected: " + Question4.answerr);
    }
}

我有一门应用课程。就像这个程序的大脑。它存储了打印display()方法的正确类所需的所有信息。它记录了我现在在哪一页。此外,我可以转到下一页,向后移动一页,或选择当前页上的答案。另外,我还有一个抽象的page类,其中每个问题都继承自page类。

//How exactly does the page cluster work?
            +--------+----  Page  -+---------+-----------+(This is an abstract class)
            |        |             |         |           |
     Question1     Question2   Question3 Question4  Summary (This the 5 pages, that inherited from Page class with)

class Application
int current_p; // Current position of the question.
Page p[] = new Page[5];

public Application(){//Constructor
   p[0] = new Quesion1;
   p[1] = new Quesion2;
   p[2] = new Quesion3;
   p[3] = new Quesion4;
   p[4] = new Summary;
   current_p = 1;
}
public start(){
   p[current_p].display();
}
public next(){
  current_p++;
  p[current_p].display();
}
public back(){
  current_pn--;
  p[current_p].display();
}
public select(int i){
   p[cuurent_p].select(i);// Go back to my Question class to see how select work.
   p[current_p].display();
}

因为这是如此之长,它可能会对我试图做什么感到困惑。下面是一个输出示例

Main(){
 Application a = new Application();
 a.start();
 a.select(2);
 a.next();
 a.select(1);
 a.next();
 a.next();
 a.next();
}

在命令行中,它应该输出如下内容:

This is Question1
1. Answer
2. Answer
3. Answer
You have selected: 0

This is Question1
1. Answer
2. Answer
3. Answer
You have selected: 2

This is Question2
1. Answer
2. Answer
3. Answer
You have selected: 0 

This is Question2
1. Answer
2. Answer
3. Answer
You have selected: 1

This is Question3
1. Answer
2. Answer
3. Answer
You have selected: 0

This is Question4
1. Answer
2. Answer
3. Answer
You have selected: 0

Summary
In question 1, you've selected:  2
In question 2, you've selected:  1
In question 3, you've selected:  0
In question 4, you've selected:  0

所以我现在的问题是,我找不到一种方法来从每个问题中得到答案,并在summary类中显示出来。如果我的设计仍然有效,我应该如何改变我的设计?

unftdfkk

unftdfkk1#

我将通过以下方式解决这个问题:
你有一个 Application 班级。
你有个摘要 Page 班级。
你有一个 QuestionPage 扩展的类 Page .
你有一个 SummaryPage 类,该类还扩展 Page .
你有一个 QuestionOption 班级。 Application 应该有一个 List<Page> 所有你想要的页面。 Page 应该有一个 display() 方法或类似方法来实现每个不同页面所需的功能。
它有点违背了oop的观点,为每个问题创建一个单独的类,所以我将每个问题示例化为一个新的类 QuestionPage . 因此,要区分每个问题,您需要有一个带有问题参数的构造函数,例如:

// QuestionPage.java (extends Page)
private final QuestionOption questionOptionOne;
private final QuestionOption questionOptionTwo;
private final QuestionOption questionOptionThree;
private QuestionOption inputAnswer;

public QuestionPage(QuestionOption questionOne, QuestionOption questionTwo, QuestionOption questionThree){
    this.questionOptionOne = questionOptionOne;
    this.questionOptionTwo= questionOptionTwo;
    this.questionOptionThree= questionOptionThree;
}

public void setInputAnswer(QuestionOption inputAnswer) {
    this.inputAnswer = inputAnswer;
}

//QuestionOption.java
private final String description;

public QuestionOption(String description) {
    this.description = desctription;
}

当你建造 Application 类,您应该输入 List<Page> . 它应该记录下来( pageNumber )其中 Page 你上了。你应该可以打电话给 nextPage() 方法,该方法增加 pageNumber 重新显示 Page .
当他们回答的时候,你会打电话给他们 setInputAnswer 储存他们的答案。
这个 SummaryPage 然后遍历所有 QuestionPage 并显示它们的 inputAnswers 总的来说,我会小心地尝试遵循面向对象的技术-如果你已经有一个新的类来做这项工作,就不要创建一个新的类。另外,在java方面,我个人会为每个应用程序使用一个接口 Page (而不是抽象类),具有 displayOptions 方法,因为你对任何两个都没有做完全相同的事情 Page s、 所以在抽象类中没有可以共享和受益的功能。不过,这只是我的偏好。

相关问题