According to the given UML class diagram and classes, complete the missing part of the given program by using polymorphism in the main method.
In the main method there are:
Source Code:
import java.util.ArrayList;
abstract class Question {
String question, correctAnswer;
public Question(String question, String
correctAnswer) {
super();
this.question = question;
this.correctAnswer =
correctAnswer;
}
}
class FillInTheBlank extends Question {
public FillInTheBlank(String question, String
correctAnswer) {
super(question,
correctAnswer);
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
class MultipleChoice extends Question {
String choices[];
public MultipleChoice(String question, String
correctAnswer, String choice1, String choice2, String
choice3,
String choice4)
{
super(question,
correctAnswer);
String choices[] = { choice1,
choice2, choice3, choice4 };
this.choices = choices;
}
public char getCorrectChoice() {
int i;
int a = 65; // 'A'
char c = 'A';
for (i = 0; i < choices.length;
i++) {
if
(choices[i].equals(correctAnswer)) {
c = (char) (a + i);
break;
}
}
return c;
}
}
public class Test {
public static void main(String[] args) {
MultipleChoice q1 = new
MultipleChoice("2+1=?", "3", "2", "3", "4", "5");
FillInTheBlank q2 = new
FillInTheBlank("3 is _____ number.", "an odd");
FillInTheBlank q3 = new
FillInTheBlank("2+2=___", "4");
ArrayList<Character>
selectionAnswers = new ArrayList<Character>();
ArrayList<String> textAnswers
= new ArrayList<String>();
// Add your code here
Question test[] = { q1, q2, q3
};
for (int i = 0; i < test.length;
i++) {
System.out.println(test[i].correctAnswer);
if (test[i]
instanceof MultipleChoice) {
char answer = ((MultipleChoice)
test[i]).getCorrectChoice();
selectionAnswers.add(answer);
}
if (test[i]
instanceof FillInTheBlank) {
String answer = ((FillInTheBlank)
test[i]).getCorrectAnswer();
textAnswers.add(answer);
}
}
System.out.println();
System.out.println(selectionAnswers.toString());
System.out.println(textAnswers.toString());
}
}
Ans Code: Test.java
package com.app;
import java.util.ArrayList;
abstract class Question {
String question, correctAnswer;
public Question(String question, String correctAnswer) {
super();
this.question = question;
this.correctAnswer = correctAnswer;
}
}
class FillInTheBlank extends Question {
public FillInTheBlank(String question, String correctAnswer)
{
super(question, correctAnswer);
}
public String getCorrectAnswer() {
return correctAnswer;
}
public String toString(){
return
"FillInTheBlank["+question+":"+correctAnswer+"]";
}
}
class MultipleChoice extends Question {
String choices[];
public MultipleChoice(String question, String correctAnswer,
String choice1, String choice2, String choice3,
String choice4) {
super(question, correctAnswer);
String choices[] = { choice1, choice2, choice3, choice4 };
this.choices = choices;
}
//this method returns 'B' because 65+1(1st index matching) and
cast to char is 'B'
public char getCorrectChoice() {
int i;
int a = 65; // 'A'
char c = 'A';
for (i = 0; i < choices.length; i++) {
if (choices[i].equals(correctAnswer)) {
c = (char) (a + i);
break;
}
}
return c;
}
public String toString(){
return
"MultipleChoice["+question+":"+correctAnswer+"]";
}
}
public class Test {
public static void main(String[] args) {
MultipleChoice q1 = new MultipleChoice("2+1=?", "3", "2", "3", "4",
"5");
FillInTheBlank q2 = new FillInTheBlank("3 is _____ number.", "an
odd");
FillInTheBlank q3 = new FillInTheBlank("2+2=___", "4");
ArrayList<Character> selectionAnswers = new
ArrayList<Character>();
ArrayList<String> textAnswers = new
ArrayList<String>();
// Add your code here
//Here Question is an Parent class and q1,q2,q3 are childs to the
Question class
//child objects are referring by using parent reference
Question test[] = { q1, q2, q3 };
for (int i = 0; i < test.length; i++) {
System.out.println(test[i].correctAnswer);
if (test[i] instanceof MultipleChoice) {
char answer = ((MultipleChoice) test[i]).getCorrectChoice();
selectionAnswers.add(answer);
}
if (test[i] instanceof FillInTheBlank) {
String answer = ((FillInTheBlank)
test[i]).getCorrectAnswer();
textAnswers.add(answer);
}
}
System.out.println();
System.out.println(selectionAnswers.toString());
System.out.println(textAnswers.toString());
}
}
Output:
![X Problems @ Javadoc Declaration Console X <terminated Test [Java Application] C:\Program Files Vavaljdk1.8.0_181\bin\javaw.e](http://img.homeworklib.com/questions/182e0cd0-aacb-11eb-8e47-51cc3a944752.png?x-oss-process=image/resize,w_560)
X Problems @ Javadoc Declaration Console X <terminated Test [Java Application] C:\Program Files Vavaljdk1.8.0_181\bin\javaw.exe (May 4, 2020, 9:53:48 PM) an odd [B] [an odd, 4]
According to the given UML class diagram and classes, complete the missing part of the given...
Java help: 1.1) Create a class TA that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...
Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B extends C implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b...
Templates
Apartment.java
package hwk7;
public class Apartment {
int numOfApartments; // the number of apartments of
this type
Room[] rooms; // rooms in this type of
apartment
Apartment(int numOfApartments, Room[] rooms) {
this.numOfApartments =
numOfApartments;
this.rooms = rooms;
}
// Return the window orders for one apartment of
this type as TotalOrder object
TotalOrder orderForOneUnit() {
// TODO
}
// Return the window...
Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...
Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...
Draw a UML class diagram (with associations) to show the design of the Java application. public class OOPExercises { public static void main(String[] args) { //A objA = new A(); B objB = new B(); System.out.println("in main(): "); //System.out.println("objA.a = "+objA.getA()); System.out.println("objB.b = "+objB.getB()); //objA.setA (222); objB.setB (333.33); //System.out.println("objA.a = "+objA.getA()); System.out.println("objB.b = "+objB.getB()); } } public class A { int a = 100; public A() {...
What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”);...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() { super(); System.out.println(“B() called”); } public...
In this lab, a piece of working software is given. You are asked to make changes to the software according to some requirement. The scenario in this lab mimics the situation that most old software applications are facing---need to update. When covering inheritance and polymorphism, we introduced a pattern that has three elements: instanceof operator, base pointers, and a collection data structure. In the given code, the collection used is a dynamic array (will change its length automatically) called Vector....