1) Create a class Course with instance members – Course Code and Course Title. Include default and parameterized constructors to initialize the instance members and a display method.
Create another class called CAT1MCQ that extends Course class. The class should include the instance members – Question, Choice1, Choice2, Choice3, and correctChoice. Include default and parameterized constructors to initialize the instance members and a display method.
Create an interface VerifyAnswer with a method verify().
Create another class that extends CAT1MCQ and implements VerifyInterface. The class should override verify() to display the course details, question and choices and wait for the user to enter the answer. Once the user inputs the answer, check is if matches with the correctChoice or not.
Create a main class that test the above hierarchy for an array of 'n' objects.
import java.util.Scanner;
class Course {
private String CourseCode;
private String CourseTitle;
public Course() {
}
public Course(String courseCode, String courseTitle) {
this.CourseCode = courseCode;
this.CourseTitle = courseTitle;
}
public void display() {
System.out.println("Course code: " + CourseCode);
System.out.println("Course title: " + CourseTitle);
}
}
interface VerifyAnswer {
public boolean verify();
}
class CAT1MCQ extends Course implements VerifyAnswer {
private String Question;
private String Choice1;
private String Choice2;
private String Choice3;
private int CorrectChoice;
private Scanner keyBoard = new Scanner(System.in);
public CAT1MCQ() {
}
public CAT1MCQ(String Question, String Choice1, String Choice2, String Choice3, int CorrectChoice) {
super("55145", "Course CDD");
this.Question = Question;
this.Choice1 = Choice1;
this.Choice2 = Choice2;
this.Choice3 = Choice3;
this.CorrectChoice = CorrectChoice;
}
public void display() {
System.out.println("Question: " + Question);
System.out.println("Choice 1: " + Choice1);
System.out.println("Choice 2: " + Choice2);
System.out.println("Choice 3: " + Choice3);
}
@Override
public boolean verify() {
display();
System.out.print("Enter your choice [1-3]: ");
int userChoice = keyBoard.nextInt();
return (this.CorrectChoice == userChoice);
}
}
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
CAT1MCQ questions[] = new CAT1MCQ[3];
questions[0] = new CAT1MCQ("5+6", "15", "11", "17", 2);
questions[1] = new CAT1MCQ("2*9", "18", "28", "25", 1);
questions[2] = new CAT1MCQ("6-7", "1", "-5", "-1", 3);
for (int i = 0; i < questions.length; i++) {
if (questions[i].verify()) {
System.out.println("Correct answer");
} else {
System.out.println("Wrong answer");
}
}
}
}
Comments
Leave a comment