Please dont use hashmap or Apache commons, its a basic computer science II cours
ID: 3877754 • Letter: P
Question
Please dont use hashmap or Apache commons, its a basic computer science II course.
You have FillInTheBlank.java:
package question1;
public class FillInTheBlank {
/**
* Data fields question and answer
*/
private String question, answer;
/**
* Copy constructor to access the data fields.
*/
public FillInTheBlank(String question, String answer) {
this.question = question;
this.answer = answer;
}
/**
* Constructor
*/
public FillInTheBlank(FillInTheBlank in) {
this.question = in.question;
this.answer = in.answer;
}
/**
* The getQuestion method, which accesses, the question.
* @return, the question itself.
*/
public String getQuestion() {
return this.question;
}
/**
* The setQuestion method, which sets the questions.
* @param question, sets the question
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* The getAnswer method, which accesses the answer.
* @return the answer
*/
public String getAnswer() {
return this.answer;
}
/**
* The setAnswer mehod, which sets the answer.
* @param answer, the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return string representation of question
*/
@Override
public String toString() {
return this.question;
}
}
Then you have Quiz.java:
package question1;
public class Quiz {
private int numQuestions;
private String title;
private FillInTheBlank quiz[];
/**
* Constructors
*/
public Quiz() {
this.numQuestions = 5;
this.title = "My Own Quiz";
this.quiz = new FillInTheBlank[numQuestions];
}
/**
* Constructors
*/
public Quiz(int numQuestions, String title) {
this.numQuestions = numQuestions;
this.title = title;
this.quiz = new FillInTheBlank[numQuestions];
}
/**
* Constructors
*/
public Quiz(Quiz in) {
this.numQuestions = in.numQuestions;
this.title = in.title;
this.quiz = new FillInTheBlank[numQuestions];
// deep copy
for(int i=0; i<in.quiz.length; i++) {
this.quiz[i] = new FillInTheBlank(in.quiz[i]);
}
}
/**
* @return the title
*/
public String getTitle() {
return this.title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* sets the question at some index (deep clone)
*/
public FillInTheBlank setQuestion(int index, FillInTheBlank inQuestion) {
if(index < 0 || index >= numQuestions) {
return null;
} else {
this.quiz[index] = new FillInTheBlank(inQuestion);
return this.quiz[index];
}
}
/**
* returns the question at some index
*/
public FillInTheBlank getQuestion(int index) {
if(index < 0 || index >= numQuestions) {
return null;
} else {
return this.quiz[index];
}
}
/**
* prints the questions
*/
public void printText() {
System.out.println("Quiz Title: " + title);
System.out.println("The questions you have set for the quiz are: ");
for(int i=0; i<numQuestions; i++) {
if(quiz[i] != null) {
System.out.printf("%d. %s ", (i+1), quiz[i].getQuestion());
}
}
}
/**
* prints the answers
*/
public void printAnswers() {
for(int i=0; i<numQuestions; i++) {
if(quiz[i] != null) {
System.out.printf("%d. %s ", (i+1), quiz[i].getAnswer());
}
}
}
/**
* string representation of quiz
*/
public String toString() {
return "Quiz: " + title + " " + "Can hold " + numQuestions + " Qusetions.";
}
}
And finally you have the QuizTester.java:
package question1;
import java.util.*;
public class QuizTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the title of the quiz: ");
String title = in.nextLine();
System.out.print("How many questions are there in the quiz? ");
int n = Integer.parseInt(in.nextLine());
// create quiz
Quiz quiz = new Quiz(n, title);
// ask user for the questions and answers and fill the quiz
System.out.println();
for(int i=0; i<n; i++) {
System.out.printf("Enter question for Q%d : ", (i+1));
String q = in.nextLine();
System.out.printf("Enter answer for Q%d : ", (i+1));
String a = in.nextLine();
quiz.setQuestion(i, new FillInTheBlank(q, a));
System.out.println();
}
// print
quiz.printText();
System.out.println();
System.out.println("The answer to the questions are: ");
quiz.printAnswers();
in.close();
}
}
The question I would like for you to complete is the following:
Create a second quiz using your quiz copy constructor. For your second (copied) test, swap the first and last questions and for the middle question change the question AND the answer (do this by access the question’s set methods, not by replacing the question). Finally, print the two tests and two answer sets to show that they are distinct and that the changes to one do not affect the other (i.e., your deep copy is working).
Thanks in advacne!
Explanation / Answer
Given below is the modified QuizTester.java as per the requirements.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
package question1;
import java.util.*;
public class QuizTester {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the title of the quiz: ");
String title = in.nextLine();
System.out.print("How many questions are there in the quiz? ");
int n = Integer.parseInt(in.nextLine());
// create quiz
Quiz quiz1 = new Quiz(n, title);
// ask user for the questions and answers and fill the quiz
System.out.println();
for(int i=0; i<n; i++) {
System.out.printf("Enter question for Q%d : ", (i+1));
String q = in.nextLine();
System.out.printf("Enter answer for Q%d : ", (i+1));
String a = in.nextLine();
quiz1.setQuestion(i, new FillInTheBlank(q, a));
System.out.println();
}
Quiz quiz2 = new Quiz(quiz1);//use copy constructor to create a copy
//interchange 1st and last questions
FillInTheBlank temp = quiz2.getQuestion(0);
quiz2.setQuestion(0, quiz2.getQuestion(n-1));
quiz2.setQuestion(n-1, temp);
System.out.printf("Enter question for middle question in quiz 2 : ");
String q = in.nextLine();
System.out.printf("Enter answer for the question : ");
String a = in.nextLine();
int mid = n/2;
FillInTheBlank q1 = quiz2.getQuestion(mid); //get the middle question
q1.setQuestion(q);
q1.setAnswer(a);
// print
System.out.println(" Quiz 1");
quiz1.printText();
System.out.println();
System.out.println("The answer to the questions are: ");
quiz1.printAnswers();
System.out.println(" ************************* Quiz 2");
quiz2.printText();
System.out.println();
System.out.println("The answer to the questions are: ");
quiz2.printAnswers();
in.close();
}
}
output
======
Enter the title of the quiz: My Java Quiz
How many questions are there in the quiz? 3
Enter question for Q1 :
There are ________ bits in a byte
Enter answer for Q1 :
8
Enter question for Q2 :
________ data type is used to store real numbers
Enter answer for Q2 :
float or double
Enter question for Q3 :
______________ class is use to read input from user
Enter answer for Q3 :
Scanner
Enter question for middle question in quiz 2 :
______________ is called when an object is created
Enter answer for the question :
Constructor
Quiz 1
Quiz Title: My Java Quiz
The questions you have set for the quiz are:
1. There are ________ bits in a byte
2. ________ data type is used to store real numbers
3. ______________ class is use to read input from user
The answer to the questions are:
1. 8
2. float or double
3. Scanner
Quiz 2
Quiz Title: My Java Quiz
The questions you have set for the quiz are:
1. ______________ class is use to read input from user
2. ______________ is called when an object is created
3. There are ________ bits in a byte
The answer to the questions are:
1. Scanner
2. Constructor
3. 8