Reduzir métodos

1 resposta
S

Eu tenho duas classes que derivam de uma classe mãe e ambas possuem 2 métodos iguais, como é que eu posso fazê-los antes na classe mãe ? os métodos em questão são o showAnswer() e o execute()

class mãe

package trab13;



public abstract class ArithmeticQuestion extends Question{
	
	protected int oper1;
	protected int oper2;

	public ArithmeticQuestion(int points){
		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);
	}

	
	
	}

subclass 1

package trab13;

import java.util.*;

	

public class AddQuestion extends ArithmeticQuestion {


	public AddQuestion() {
		super(5);
	}

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

subclass2

package trab13;
import java.util.*;

public class MultQuestion extends ArithmeticQuestion {
	
	public MultQuestion() {
		super(5);
	}

	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; }
	
	
	public void showAnswer() { 
		showQuestion();		
		System.out.println( execOper() );
	}
	
	public void showQuestion() { 
		System.out.print(oper1 + "*" + oper2 + " = " );		
	}
        	
	
}

1 Resposta

peron

assim não dá?:

package trab13;
 
 
 
 public abstract class ArithmeticQuestion extends Question{
 	
 	protected int oper1;
 	protected int oper2;
 
 	public ArithmeticQuestion(int points){
   		super(5);
 	}
 	
 	protected static int random(int li,int ls ) {
 		return (int) Math.floor(Math.random() * (ls-li+1) +li);
 	}
 	
 	public void showQuestion() { 
 	    //nao faz nada aqui
 	}

       protected int execOper() { return 0; }
       
       public void showAnswer() { 
 		showQuestion();		
 		System.out.println( execOper() );
 	} 

 	public void reformulate() {
 		oper1 = random(1,9);
 		oper2= random(1,9);
 	}
 
 	
 	
 }

subclass 1

package trab13;
 
 import java.util.*;
 
 	
 
 public class AddQuestion extends ArithmeticQuestion {
 
 
 	public AddQuestion() {
 		super(5);
 	}
 
 	public void showQuestion() { 
 		System.out.print(oper1 + "+" + oper2 + " = " );		
 	}
 	
 	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; }
 }

subclass 2

package trab13;
 import java.util.*;
 
 public class MultQuestion extends ArithmeticQuestion {
 	
 	public MultQuestion() {
 		super(5);
 	}
 
 	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; }
 	
 	public void showQuestion() { 
 		System.out.print(oper1 + "*" + oper2 + " = " );		
 	}
         	
 	
 }

como nunca irá existir instancia da 1 classe, nunca vai imprimir zero, exceto se vc nao sobrescrever o metodo execOper() na subclasse :slight_smile:

Colegas, me corrijam se eu estiver errado…

Espero que tenha ajudado,

att.

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