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.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...
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...
given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...
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...
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...
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() {...
For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...
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...