null output in multiple choice array

Viewed 36

my output of my java application is running null values for the output of Q4 and 5, tried fixing it but had no luck swapping out the null values for the actual string question needed to be shown just before the user input

MultipleChoiceQuestions.java


    import java.util.Arrays;
import java.util.Scanner;

public class MultipleChoiceQuestion extends Question{
    private int count = 0 ;
    private String cho1, cho2, cho3, cho4 ; 
    private boolean bool1, bool2, bool3, bool4 ; 
  private final String[] newchoice = new String[4];
  
    public MultipleChoiceQuestion(String text, String cho1, boolean bool1,
                                  String cho2, boolean bool2,
                                  String cho3, boolean bool3,
                                  String cho4, boolean bool4) {
    
        super(text);
        this.cho1 = cho1; this.cho2 = cho2;
        this.cho3 = cho3; this.cho4 = cho4;
        this.bool1 = bool1; this.bool2 = bool2;
        this.bool3 = bool3; this.bool4 = bool4;
    }
  
    public void addChoice(String Choices, boolean isCorrect){
    this.newchoice[count]= Choices;  
    if(isCorrect) {
    super.setCorrectResponse(Choices);
    } 
    count++ ;
    
    }
    public void initQuestion(){
        addChoices(cho1, bool1);
        addChoices(cho2, bool2);
        addChoices(cho3, bool3);
        addChoices(cho4, bool4);
    }

    @Override
    public String toString(){
        StringBuilder sbf = new StringBuilder(super.toString());
        sbf.append(Arrays.toString(newchoice));
        return sbf.toString();
    }

    private void addChoices(String cho1, boolean bool1) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    
}

FillInTheBlankQuestion.java

import java.util.Scanner;

public class FillInTheBlankQuestion extends Question{
    
    public FillInTheBlankQuestion(String text){
        super(text);
    }
    
    public void extractQA(){
        Scanner scan = new Scanner(super.getQuestionText());
        scan.useDelimiter("_");
        super.setQuestionText(scan.next());
        super.setCorrectResponse(scan.next());
    }
    
    public String toString(){
        return super.toString() + "______________";
    }
}

Question.java



import java.util.Scanner;

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

/**
 *
 * @author --
 */
public class Question {
    private String questionText;
    private String correctResponse;
    
    //Creates a question with an empty question and answer
    public Question(){
        this.setQuestionText("");
        this.setCorrectResponse("");
    }
    
    public Question(String text){
        this.setQuestionText(text);
    }
    public Question(String text, String answer){
        this.setQuestionText(text);
        this.setCorrectResponse(answer);
    }
    //sets the text of this question
    public void setQuestionText(String text){
        this.questionText = text;
    }
    //sets the answer for this question
    public void setCorrectResponse(String answer){
        this.correctResponse = answer;
    }
    
    public String getQuestionText(){
        return this.questionText;
    }
    
    public String getCorrectResponse(){
        return this.correctResponse;
    }
    
    /*
      checks the response/answer given for correctness
      @param givenResponse The response to check
      return true if the response is correct, false otherwise
    */
    public boolean verifyAnswer(String givenResponse){
        //it does not take into account upper/lower case characters.
        return givenResponse.equalsIgnoreCase(this.getCorrectResponse());
    }
    
    //allows user to type the answer
    public void inputAnswer(Scanner scan){
        System.out.print("Type your answer:");
        System.out.println(this.verifyAnswer(scan.nextLine()));
    }
    
    //display the question
    @Override
    public String toString(){
        return this.getQuestionText();
    }
    
}

QuestionTester.java

import java.util.Scanner;

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */

/**
 *
 * @author ---
 */
public class QuizTester {

    public QuizTester(){
        Scanner in = new Scanner(System.in);
      
        
        //declare an array quiz that can hold a mixture of Question and Fill in blank type
        Question[] quiz = new Question[6];
        quiz[0] = new Question("Which class is used to get user input?", "Scanner");
        quiz[1] = new Question("How many primitive data types are there in Java? Enter in words.", "eight");
        quiz[2] = new FillInTheBlankQuestion("The inventor of Java was _James Gosling_");
        quiz[3] = new FillInTheBlankQuestion("Every class in Java inherits from _Object_");
        quiz[4] = new MultipleChoiceQuestion("What represents the collection of related data?",
                "String", false,"Array", true, "Integer", false,
                "Iterator", false);
        quiz[5] = new MultipleChoiceQuestion("Which method does not belong to Scanner class?",
                "nextInt()", false,
                "next()", false,
                "nextboolean()", false,
                "nextChar()", true);
        for(int i = 0; i < quiz.length; i++){
            if(quiz[i] instanceof FillInTheBlankQuestion){
                ((FillInTheBlankQuestion)quiz[i]).extractQA();
            }
            System.out.println("Q" + (i + 1) + ": " + quiz[i].toString());
            quiz[i].inputAnswer(in);
        }
        
    }
    public static void main(String[] args) {
        new QuizTester();
    }
    
}

I thank you so much in advance

0 Answers
Related