Eu tenho de acrescentar à hierarquia a classe CompoundQuestion, usada na aplicação QuizGame, de forma a que esta aplicação tenha o mesmo funcionamento que QuizGame1.
É-me dada a class QuizGame1 e a class QuizGame, eu já fiz a CompoundQuestion, mas estou com um problema porque ela faz todas as contas com os mesmos valores, ou seja… ela cria o oper1 e o oper2 e põe em todas as posições do array.
O que estarei fazendo de errado ?
QuizGame1 ( class fornecida e a funcionar )
package trab13;
public class QuizGame1 {
private Question[] questionary;
private int points;
public void BuildQuiz( int nq ) {
questionary = new Question[nq];
points = 0;
Question q;
for ( int i= 0; i < questionary.length; ++i ) {
q = new AddQuestion();
if (AddQuestion.random(0, 1) == 0)
q = new TimedQuestion( q );
questionary[i] = q;
points+= q.getPoints();
}
}
public void play() {
if ( questionary == null ) BuildQuiz(5);
int npo = 0;
for ( int i= 0; i < questionary.length; ++i ) {
questionary[i].reformulate();
npo += questionary[i].execute();
}
System.out.println("obteve " + npo + " pontos em " + points);
}
public static void main(String[] args) {
QuizGame1 game = new QuizGame1();
game.play();
}
}
QuizGame ( class fornecida )
package trab13;
public class QuizGame {
private CompoundQuestion questionary = null;
public QuizGame( ) { }
public void BuildQuiz(int nq ) {
questionary = new CompoundQuestion(nq);
while ( questionary.size() != questionary.capacity() ) {
Question q;
q = new AddQuestion();
if (AddQuestion.random(0, 1) == 1) q = new TimedQuestion(q);
questionary.add(q);
}
}
public void play() {
if ( questionary == null ) BuildQuiz(5);
questionary.reformulate();
int npo = questionary.execute();
System.out.println("obteve " + npo + " pontos em " + questionary.getPoints());
}
public static void main(String[] args) {
QuizGame game = new QuizGame();
game.play();
}
}
CompoundQuestion ( classe implementada por mim que junto com a class QuizGame devia ficar com o mesmo funcionamento de QuizGame1 )
package trab13;
public class CompoundQuestion {
private int points;
public int nq;
public int npo;
private Question[] questionary;
public CompoundQuestion(int nq) {
this.nq = nq ;
questionary = new Question[nq];
points = 0;
}
public int size() {
int size=0;
for(int i =0; i<questionary.length;++i){
if(questionary[i]!=null)++size;
}
return size;
}
public int capacity() {
return nq;
}
public void add(Question q) {
//q = new AddQuestion();
for ( int i= 0; i < questionary.length; ++i ) {
questionary[i] = q;
points+= q.getPoints();
}
}
public void reformulate() {
for ( int i= 0; i < questionary.length; ++i ) {
questionary[i].reformulate();
}
}
public int execute() {
for ( int i= 0; i < questionary.length; ++i ) {
npo += questionary[i].execute();
}
return 0 ;
}
public String getPoints() {
return String.valueOf(points);
}
}