Reputation: 81
I am working on a program for intermediate Java and I am I feel I have the logic right, however I get this error in the class introduction:
The type QuestionSet must implement the inherited abstract method IQuestionSet.add(IQuestion)
I do have the method but I have a Question object for the parameter instead of IQuestion(The Interface for Question)
QuestionSet:
package com.jsoftware.test;
import java.io.Serializable;
import java.util.ArrayList;
public class QuestionSet implements Serializable, IQuestionSet{
ArrayList<Question> test = new ArrayList<Question>();
public QuestionSet emptyTestSet(){
QuestionSet set = new QuestionSet();
return set;
}
public QuestionSet randomSample(int size){
QuestionSet set = new QuestionSet();
if(size>test.size()-1){
for(int i =1; i<test.size(); i++){
int num = (int)(Math.random()*test.size());
set.add(test.get(num));
}
}else{
for(int i =1; i<size; i++){
int num = (int)(Math.random()*test.size());
set.add(test.get(num));
}
}
return set;
}
public boolean add(Question q){
try{
test.add(q);
return true;
}catch(Exception e){
return false;
}
}
public boolean remove(int index){
try{
test.remove(index);
return true;
}catch(Exception e){
return false;
}
}
public Question getQuestion(int index){
return test.get(index);
}
public int size(){
return test.size();
}
}
IQuestionSet:
package com.jsoftware.test;
/**
* This interface represents a set of question.
*
* @author thaoc
*/
public interface IQuestionSet {
/**
* Create an empty test set.
* @return return an instance of a test set.
*/
public IQuestionSet emptyTestSet();
/**
* return a test set consisting of a random questions.
* @param size The number of random questions.
* @return The test set instance containing the random questions.
*/
public IQuestionSet randomSample(int size);
/**
* add a question to the test set.
* @param question The question
* @return True if successful.
*/
public boolean add(IQuestion question);
/**
*
* @param index Remove question using index
* @return true if index is valid
*/
public boolean remove(int index);
/**
* Retrieving a question using an index
* @param index
* @return the question if index is valid, null otherwise.
*/
public IQuestion getQuestion(int index);
/**
* Return the number of questions in this test set.
* @return number of questions.
*/
public int size();
}
Upvotes: 4
Views: 11969
Reputation: 11832
First change your list of questions to use the type IQuestion:
ArrayList<IQuestion> test = new ArrayList<IQuestion>();
Then change the method declaration:
public boolean add(IQuestion q){
try{
test.add(q);
return true;
}catch(Exception e){
return false;
}
}
Upvotes: 2