10.3,10.5,10.6 please Thank you RIO. I s what access attribute s instance and .o
ID: 3823059 • Letter: 1
Question
10.3,10.5,10.6 please Thank you RIO. I s what access attribute s instance and .out are public. Is it possible to system. P10.1 R10. 16 The static variables system. static variables them? If so, how? Are public R 10.17 variables dangerous? Why are public instance variables gerous than public instance Programming Exercises. unt class to comput the i P10.1 Enhance the addinterest of need to on the minimum balance since the last call to instance variable to the method as well, and need to add an The time d you P10.2 minimum balance. class to the bank account hierarchy. the mone in th Add a Ti you promise to leave epos account is just like a savings account, but and penalty for ea account for a particular number of months, interest rate the number of withdrawal. Construct the account wit the count of o maturity. the the withdrawal penalty. is positive during a withdrawal, charge f How To 10.1. the P10.3 class Numericouestion to the question no more than o.01, then accept as it response and the expected answer correct. hierarchy of How To 10 A ob P10.4 Add a class Fil1InQuestion the question the answer, surrounded b this class is constructed with a string that contains question should be for example, of Java was James Gosling. played as The inventor of Java was that P10.5 Modify the method of the question class of How To 10.1 so e not take into account different spaces or characters. For the response gosling" should match an answer of
Explanation / Answer
Question 10.3:
Question.java
public class Question {
private String text;
private String answer;
public Question() {
text = "";
answer = "";
}
public void addText(String additionalText) {
text += " " + additionalText;
}
public void setText(String questionText) {
text = questionText;
}
public void setAnswer(String correctResponse) {
answer = correctResponse;
}
public boolean checkAnswer(String response) {
return response.equals(answer);
}
public void display() {
System.out.println(text);
}
}
NumericQuestion.java
public class NumericQuestion extends Question {
private double answer;
public void setAnswer(double correctResponse) {
answer = correctResponse;
}
public boolean checkAnswer(String response) {
double responseDouble = Double.parseDouble(response);
return Math.abs(responseDouble - answer) <= 0.01;
}
}
10.5:
Question.java
public class Question {
private String text;
private String answer;
public Question() {
text = "";
answer = "";
}
public void addText(String additionalText) {
text += " " + additionalText;
}
public void setText(String questionText) {
text = questionText;
}
public void setAnswer(String correctResponse) {
answer = correctResponse.trim();
}
public boolean checkAnswer(String response) {
return answer.equalsIgnoreCase(response.trim());
}
public void display() {
System.out.println(text);
}
}
10.6:
ChoiceQuestion.java:
public class ChoiceQuestion extends Question {
private int nChoices;
public ChoiceQuestion() {
super();
}
public void addChoice(String choice, boolean correct) {
nChoices += 1;
super.addText(String.format(" %d)%s", nChoices, choice));
if (correct) {
// Convert choices.size() to string
String choiceString = "" + nChoices;
setAnswer(choiceString);
}
}
public String toString() {
return getClass().getName();
}
}
MultiChoiceQuestion.java:
public class MultiChoiceQuestion extends ChoiceQuestion {
private ArrayList<String> allAnswers;
public MultiChoiceQuestion() {
super();
this.allAnswers = new ArrayList<String>();
}
public void setAnswer(String correctResponse) {
this.allAnswers.add(correctResponse);
}
public boolean checkAnswer(String response) {
Scanner parser = new Scanner(response);
ArrayList<String> correctAnswersSeen = new ArrayList<String>();
int totalAnswers = 0;
while (parser.hasNext()) {
String answer = parser.next();
if (this.allAnswers.contains(answer) && !correctAnswersSeen.contains(answer)) {
correctAnswersSeen.add(answer);
}
totalAnswers += 1;
parser.close();
}
return correctAnswersSeen.size() == this.allAnswers.size() && totalAnswers == allAnswers.size();
}
public void display() {
super.display();
System.out.println("Note: there are several correct answers. List all of them separated by spaces");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.