Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment 1. Download “pa5.zip”, extract the NetBeans project folde

ID: 3731505 • Letter: P

Question

Programming Assignment 1. Download “pa5.zip”, extract the NetBeans project folder “Pa5” from it, then open it in NetBeans. 2. Examine the class TrueFalseQuestion. Note that it (a) has a protected String field questionText and a private boolean answer. (b) has a constructor that assigns an initial value to the field questionText. (c) contains four methods, one to set the question text, one to return a string containing the question with instructions, a final method to set the answer and a final method to check an answer against the set answer. 3. Create code in class Pa5 to demonstrate the use of an object of type TrueFalseQuestion class as a data type. (See the example run for ideas.) 4. Create a class to manage a simple test question. This class (a) inherits class TrueFalseQuestion . (b) must contain a constructor or constructors that use super to call the constructor of class TrueFalseQuestion. (c) must use overriding polymorphism to override the inherited method getQuestion so that the string returned is appropriate for a question needing a string answer instead of a boolean answer. (d) must use overloading polymorphism to create appropriate versions of methods setAnswer and checkAnswer. (e) may contain any other methods or fields (data members) needed. 5. Create code in class Pa5 to demonstrate that your simple test question class is an effective data type. (See the example run for ideas.) CMPS 260 Spring 2018 Programming Assignment #5 2 6. Create a class to manage a multiple choice type question. This class (a) inherits class TrueFalseQuestion (b) must contain a default constructor that uses super to call the constructor of class TrueFalseQuestion and sets the number of answer choices to a fixed number greater than 1. (c) must contain a parametrized constructor that uses super to call the constructor of class TrueFalseQuestion and sets the number of answer choices to that of a passed parameter, but only if the value of the parameter is greater than 1. (d) must use overriding polymorphism to override the inherited method getQuestion so that the string returned is appropriate for a question needing a string answer instead of a boolean answer. (e) must use overloading polymorphism to create appropriate versions of methods setAnswer and checkAnswer. (f) may contain any other methods or fields (data members) needed. Suggested methods include a method for adding a choice to the list of multiple guess choices, returning the maximum number of multiple guess choices and returning the current number of multiple guess choices. 7. Create code in class Pa5 to demonstrate that your multiple choice test question class is an effective data type. (See the example run for ideas.) Requirements • No changes are allowed to class TrueFalseQuestion. • All programmer created classes must be in separate files, i. e. each programmer created class must be the only class in its file. • All user I/O is to occur in class Pa5. User I/O is not allowed in the classes that inherit class TrueFalseQuestion. I. e. there can be no code that accepts input from the user and or sends output to the user in TrueFalseQuestion, simple question or the multiple choice question classes. Tips • Write and test the code to demonstrate the use of a TrueFalseQuestion object in Pa5 first. • Of the two classes to be created, write the simple question class first and test it by writing the simple class solution code. Then tackle the choice question class. • Adding “ ” to a string results in a chance of line when that string is output.

package xxa!i; public class TrueFalseQuestion i protected String questionText = null; private boolean answer= false; public TrueFalseQuestion) this.questionText "No Question " ; public void setQuestionText (String questionText) this.questionText questionText public String getQuestion) return questionText " (true/false)" public final void setAnswer (boolean answer) t this . answer = answer ; public final boolean checkAnswer (boolean attempt) t return answer attempt: static class java i public java)

Explanation / Answer

import java.util.*;

class TrueFalseQuestion // base class
{
protected String questionText = null;
private boolean answer = false;

public TrueFalseQuestion() //default constructor
{
  this.questionText = "No question";
}
public void setQuestionText(String questionText)
{
  this.questionText = questionText;
}

public String getQuestion()
{
  return questionText + "true/false";
}

public final void setAnswer(boolean answer)
{
  this.answer = answer;
}
public final boolean checkAnswer(boolean attempt)
{
  return answer == attempt;
}


}

class SimpleQuestion extends TrueFalseQuestion   //derived class
{
private String answer;
public SimpleQuestion()
{
  super();
}
public String getQuestion()
{
  return questionText ;
}

public void setAnswer(String answer)// overriding base class function
{
  this.answer = answer;
}
public boolean checkAnswer(String attempt)// overriding base class function
{
  return answer.equals(attempt);
}

}
class ChoiceQuestion extends TrueFalseQuestion   // derived class
{
private String answer;
public ChoiceQuestion()
{
  super();
}
public void setAnswer(String answer)// overriding base class function
{
  this.answer = answer;
}
public boolean checkAnswer(String attempt)// overriding base class function
{
  return answer.equals(attempt);
}

}

class Questions
{
public static void main (String[] args)
{

Scanner input = new Scanner(System.in);
System.out.println("Demonstration of making questions:");

System.out.println("Enter a true false question");
String question = input.nextLine();

System.out.println("Enter the answer to the true/false question (true or false)");
String answer = input.nextLine();

TrueFalseQuestion q1 = new TrueFalseQuestion();// object
q1.setQuestionText(question);
q1.setAnswer(true);


if(q1.checkAnswer(true))
System.out.println("You scored one point");

System.out.println("Enter a simple question");
question = input.nextLine();

System.out.println("Enter the answer to the simple question ");
answer = input.nextLine();
SimpleQuestion q2 = new SimpleQuestion();// object

q2.setQuestionText(question);
q2.setAnswer(answer);

if(q2.checkAnswer("gold"))
System.out.println("You scored one point");

System.out.println("Enter the number of choices(>1) : ");
int choices = input.nextInt();


System.out.println("Enter a choice question");
question = input.nextLine();

String[] choice = new String[choices];
for(int i =0;i<choices;i++)
{
  System.out.println("Enter choice "+(i+1));
  choice[i] = input.nextLine();
  System.out.println(choice[i]);
}
System.out.println("Enter the answer to the choice question ");
answer = input.nextLine();

ChoiceQuestion q3 = new ChoiceQuestion();// object
q3.setQuestionText(question);
q3.setAnswer(answer);

if(q3.checkAnswer(choice[1]))
System.out.println("You scored one point");
}
}

Output:

Demonstration of making questions:
Enter a true false question terra firma means dry land
Enter the answer to the true/false question (true or false) true
You scored one point
Enter a simple question Which is heavier, gold or silver
Enter the answer to the simple question gold
You scored one point
Enter the number of choices(>1) : 3
Enter a choice question What is the longest river on the earth?
Enter choice 1 Missisippi
Enter choice 2 Nile
Enter choice 3 Amazon
Enter the answer to the choice question Nile

Do ask if any query. Please upvote if the answer is useful.