Transformação

4 respostas
S

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); 

	}

}

4 Respostas

S

Outras classes do mesmo programa:

AddQuestion
package trab13;

import java.util.*;

	

public class AddQuestion extends Question {

	protected int oper1;
	protected int oper2;

	public AddQuestion() {
		super(5);
	}

	protected static int random(int li,int ls ) {
		return (int) Math.floor(Math.random() * (ls-li+1) +li);
	}
	
	public void reformulate() {
		oper1 = random(1,9);
		oper2= random(1,9);
	}
	public void showQuestion() { 
		System.out.print(oper1 + "+" + oper2 + " = " );		
	}
	public void showAnswer() { 
		showQuestion();		
		System.out.println( execOper() );
	}
	public int execute () {
		showQuestion();		
		Scanner s = new Scanner(System.in);
        	if ( s.nextInt() == execOper() ) return this.getPoints();
       		System.out.print(" R: "); showAnswer();
        	return 0;		
	}	
	protected int execOper() { return oper1 + oper2; }
}

Question

package trab13;


public abstract class Question {

	private int points;

	public Question() {
		points=0;
	}
	
	public Question(int points) {
		this.points=points;
	}
	
	public int getPoints() {
		return points;
	}

	protected void setPoints(int points) {
		this.points = points;
	}
	
	public abstract void showQuestion();
	public abstract void showAnswer();
	public abstract void reformulate();
	public abstract int execute();

}

TimedQuestion

package trab13;

public class TimedQuestion extends Question {
	private Question question = null;
	
	public TimedQuestion(Question q) { 
		super(q.getPoints());	
		question = q;	
	}
	public void reformulate() {	question.reformulate(); }
	public void showAnswer()  { question.showAnswer();	}
	public void showQuestion(){ question.showQuestion();}
	public int execute () {
		long startTime = System.currentTimeMillis();
		System.out.print("!");
		int res=question.execute();
		long endTime = System.currentTimeMillis();
		return ((int) Math.max(0, res - (endTime - startTime)/1000));
	}
}
S

Ao fim de Debug e mais Debug verifiquei que tenho um problema no método:
public void add(Question q) da classe CompoundQuestion, porque eu estou a adicionar em todas as posições do array em vez de estar a adicionar só numa de cada vez, como é que eu faço isso sem modificar a classe QuizGame ? É que sem um for no método add não sei como aceder ás várias posições do array.

Ricardo_Siqueira

saunde:
Ao fim de Debug e mais Debug verifiquei que tenho um problema no método:
public void add(Question q) da classe CompoundQuestion, porque eu estou a adicionar em todas as posições do array em vez de estar a adicionar só numa de cada vez, como é que eu faço isso sem modificar a classe QuizGame ? É que sem um for no método add não sei como aceder ás várias posições do array.

Existe um loop for nesse método que adciona em todas as posições. Tira esse loop e faça o método aceitar um índice como argumento.

A propósito você é português? Não é zuando não…e q vc usou expressões tipo “aceder” e “a adcionar”

S

obrigado, era isso mesmo, problema resolvido !

Criado 1 de abril de 2006
Ultima resposta 3 de abr. de 2006
Respostas 4
Participantes 2