This program was created to administer an exam. It is constructed around the principles of inheritence and polymorphism. The program can display three types of questions: multiple choice, true or false, and short answer.
Each question type inherits basics properties from a parent class (specifically, the question, itself, and its weight). Of course, each type of question has distinct properties; for instance, multiple choice questions give the test taker a selection of answers. To properly model the nuances of each question type, I used abstract methods. This enabled me to handle displaying the question and accepting and validating the user's answer polymorphically, and it was consequently much easier.
public abstract class Question {
private int weight;
private String text;
public Question(String s) {
text = s;
}
public void setWeight(int w) {
if (w < 1) {
System.exit(1);
}
weight = w;
}
public int getWeight() {
return weight;
}
public String getText() {
return text;
}
public abstract boolean ask();
}
|
The parent class for each question (condensed)
kevin@LightningJoe:~/public_html/portfolio/exam$ java ExamDemo The capital of the state of Nebraska is Lincoln? (t)rue or (f)alse Answer: TrUe Invalid Answer; try again: T The answer you chose, true, is the correct answer. What U.S. state has the largest population? a) Florida b) California c) Rhode Island d) Nevada e) Michigan Answer: f Invalid answer; try again: B The answer you chose, California, is the correct answer. What U.S. city has the nickname "The Windy City"? Answer: ChiCaGo The answer you chose, "ChiCaGo," is the correct answer. There are 51 states in the U.S.? (t)rue or (f)alse Answer: f The answer you chose, false, is the correct answer. What is the capital of Arizona? a) Phoenix b) Flagstaff c) Parker Answer: |