2

I have some struggle with an exercise. I want to save question1 and question2 in the method addQuestion. But I have no idea how to access f and add it into something that is accessible. In my schoolbook they told us that we should do it with polymorphism. When I'm starting my debugger I see that when q.addQuestion is called, there is a property f with the fields that I need. But how can I save them for later? My first though was I can do it with ArrayList but I can't use any Imports (it's a rule in the schoolbook).

Probably I don't understand polymorphism correctly.

public class something{
    public static void main(String[] args) {
        Quiz q = new Quiz(2);
        q.addQuestion(new question1("SomeText",5,157,5));
        q.addQuestion(new question2("SomeText",5,157,5,123));
        ...
    }
}

public class Quiz {
    int myNumQuestions;
    Quiz[] quizQuestion;

    Quiz(int numQuestions){
        this.myNumQuestions = numQuestions;
        quizQuestion = new Quiz[myNumQuestions];
    }

    public boolean addQuestion(Interface f){
        quizQuestion = new f(); //that does not work but how can I access the field from f?
}

Regards.

EDIT:

I changed my public class Quiz a little bit and I can now allocate the f

MyInterfaceName[] = quizQuestion;

Quiz(int numQuestions){
    this.myNumQuestions = numQuestions;
    quizQuestion = new Quiz[myNumQuestions];
}

public boolean addQuestion(Interface f){
    quizQuestion[mycounter++] = f;
}
3
  • 1) "Interface f" - what is this suppose to be? 2) Which the original skeleton program and which bit you added? The class Quiz looks to be nested of Array Quiz which to me doesn't make sense. So in simple term, a quiz has many questions but the question should not be more quiz...? Where is the question class? Commented May 27, 2017 at 8:08
  • In the code you've attached - what's yours and what is given? Commented May 27, 2017 at 8:08
  • obviously not the best name, Interface should be the name of the "interface" class, i have 2 "question" class (question1 and question2) -> 1 AbstractQuestion and the interface is implemented by the AbstractQuestion. public boolean addQuestion(Interface f) is defined in the book. the class somethign is given ans also the method signature like "boolean addQuestion(Interface f)" Commented May 27, 2017 at 8:11

3 Answers 3

1

Your class Quiz contains an array for Question references. So: Question should either be an interface (that other classes simply implement); or it can be a (abstract) base class which gets extended by your Q1, Q2, ... class.

So change the type from "interface" to Question and put the incoming object into your array. You have to keep track of free slots in the array.

In other words: you want your Quiz class to work like a box; and you can put Question thingies into that box. Now you simply have to decide if Question should be an interface (without any specific implementation); or if a hierarchy of classes works better.

That is about it. See here for further reading.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the "further reading" you bring me also a little bit closer to the result!
Just for the record: you don't need a list. One can also use arrays and "grow" those manually. So the advice in the accepted answer is simply incorrect. And if you find my answer helpful, consider upvoting it once you reach upvote levels.
I would suggest to use List instead of array...so if he create a Quiz(10000000) that will cause OOM exception as all the array space need to be allocated upfront. I had to work with the original code...I am not saying its correct but it works.
Please step back for a while. Digest the input you got. You see the idea behind homework is not to have a solution in the end. It is about getting there. Asking other people too quickly prevents you from learning! Try to find your way yourself!
@GhostCat - And you are a teacher? The great thing about this website is to learn from each others...sure you can read everything on the web. I am not saying your going about wrong but don't be an up tied. Cya.
|
1
    public class Quiz<T> {

    T[] quizQuestion;
    private int i = 0; 
    Quiz(int numQuestions){
        quizQuestion = new T[myNumQuestions];
    }


    public boolean addQuestion(T f){
     quizQuestion[i++] = f;
    }
}

5 Comments

obviously, you only allocate 2 questions so if you add more than 2 then you need to use List instead.
Okay thanks, your question bring me a big step further, i changed "Quiz[] quizQuestion;" to InterfaceName[] quizQuestion and i can now allocate the "f"
Your comment is simply wrong. A) he explicitly says that he can't use lists B) of course you can use arrays to store a unknown number of objects. You have to grow the array and copy stuff around then. Sure that is tedious, but possibly to do.
I never say its impossible :) There is more than one way to skin a cat. My suggestion of not using array because of OOM exception. "Use the best tool for the best job :)
It just doesn't help much when the instructions tell to not use that thing. Beyond that: just putting down code without any explanations isn't a great way to answer. Especially as you introduce another complex topic with generics here. They even don't make much sense here.
0

This would be my approach...making use of generic so your Quiz can take on any class or inteface.

public class Quiz<T> {

            List<T> quizQuestion;
            Quiz(){
                quizQuestion = new ArrayList<T>;
            }

            public boolean addQuestion(T f){
             quizQuestion.add(f);
            }
        }

    class Question {}

    public void main(String[] args) {
      Quiz quiz = new Quiz<Question>();
      quiz.add(new Question());
    }

1 Comment

And you never ever put two answers! Edit the first one to be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.