Instructions
Requirements and Specifications
- Create, Compile, and Run a Java Program
- Program Errors (Syntax, Logic, Run-time)
- Procedural Decomposition, Pseudo Code and IPO Chart
- Identifiers, Variables, Constants, Assignment Statements, and Expressions
- Primitive Types: byte, short, int, long, float, or double and Casting Data Types
- Operators+,-,*,/, Increment++, Decrement --, Assignment Operator +=,-=,*=,/=
- Scanner
- IF, IF/ELSE, SWITCH,
- MATH Function, Char, String, Formatted Output
- Loops
- Methods
- Arrays
- Create the following for each of the topics from the textbook
- Java code implementing your solution
- The document is a complete response to the topics below
- This midterm project should create a 10 questions game about the domain, core, and majors course.
- Write your Pseudo Code Comments to complete the following steps
- A method should ask the user for their name and return it to the main() method for later use.
- A method should ask the user for their major and return it to the main() method for later use.
- A method should ask the user for their college-level (Freshman, ..., Senior) and return it to the main() method for later use.
- Create the code for a 10 questions game (questions are your choice, but they must meet the requirements).
- Each question must be store in an array using the appropriate datatype
- Each answer to the question must be stored in a variable with the appropriate datatype variable
- A "for" loop should iterate through the array of questions used for the user prompt.
- A switch statement should be utilized to separate each question and response check.
- Each case has a comparision between the correct response stored in the variable and the response supplied by the user will determine the count for correct and incorrect answer
- Question requirements (the actual question is your choice, but it is useful for review to use questions from your other courses)
- 1st question using the boolean type (true or false)
- 2nd question using the char type (multiple choice a,b,c,d)
- 3rd question using the string type (fill in the blank)
- 4th question using the integer type
- 5th question using the float type
- 6th question using the trigonometric function and the appropriate data type(s)
- 7th question using the exponent function and the appropriate data type(s)
- 8th question using the rounding method and the appropriate data type(s)
- 9th question using the min, max, or abs method and the appropriate data type(s)
- 10th question using your choice of question and data type
- A method should be used to print the following a formated and readable way:
- Students name
- Students major
- Student college grade level
- The number of questions correct
- The number of questions incorrect
- Create the code in Java to complete the tasks
- Create the driver in main() execute the sequence
- Comment each line describing its function as you understand it
- Create the IPO chart above main()
- Create the IPO chart above each method()
- Document any issues you had and how you corrected them
- Document the execution of your application in a word document
- Test and document the code where all questions can be answered correctly
- Test and document the code where all questions can be answered correctly
- Test and document the code where some questions can be answered correctly
Source Code
APP
import java.util.Scanner;
public class App {
// Here, we define some constant values used to know the type of Question (simple selection, multiple selection, fill, etc)
static final int MULTIPLE_OPTION = 1;
static final int FILL = 2;
static final int SINGLE_OPTION = 3;
static final int TRUE_FALSE = 4;
// Here, we define some constant values used to know the type of answer that a questions needs
static final int INT = 1;
static final int CHAR = 2;
static final int STRING = 3;
static final int FLOAT = 4;
static final int DOUBLE = 5;
static final int BOOL = 6;
public static void main(String[] args) throws Exception {
// Create scanner object
Scanner sc = new Scanner(System.in);
// get user name
String user_name = getUserName(sc);
// get major
String user_major = getUserMajor(sc);
// get college level
String user_college_level = getUserCollegeLevel(sc);
// Create list of questions
Question questions[] = new Question[10];
// Now, create questions
// Question 1: True or False
Question question = new Question("The sky is blue", TRUE_FALSE, BOOL);
question.setAnswer(true);
questions[0] = question;
// Question 2: Char Type
question = new Question("Which geometric figure has no sides?", MULTIPLE_OPTION, INT);
question.addOption("Square");
question.addOption("Circle");
question.addOption("Triangle");
question.addOption("Cylinder");
question.setAnswer(2);
questions[1] = question;
// Question 3: Fill
question = new Question("The geometric figure called ______ has only three sides.", FILL, STRING);
question.setAnswer("triangle");
questions[2] = question;
// Question 4: Integer
question = new Question("The sum of 50+100 is equal to ", SINGLE_OPTION, INT);
question.setAnswer(150);
questions[3] = question;
// Question 5: Float
question = new Question("The value of 10/100 is ", SINGLE_OPTION, FLOAT);
question.setAnswer(0.1f);
questions[4] = question;
// Question 6: Trigonometric Function
question = new Question("What is the sine of 90º? ", SINGLE_OPTION, DOUBLE);
question.setAnswer(Math.sin(Math.PI/2));
questions[5] = question;
// Question 7: Exponent function
question = new Question("What is the value of 2 to the power of 5? ", SINGLE_OPTION, DOUBLE);
question.setAnswer(Math.pow(2,5));
questions[6] = question;
// Question 8: Using round
question = new Question("What is the value of 57/38 (rounded to cero decimals)?", SINGLE_OPTION, INT);
question.setAnswer(Math.round(57.0f/38.0f));
questions[7] = question;
// Question 9: using min, max or abs
question = new Question("What is the min value between -5 and -8?", SINGLE_OPTION, DOUBLE);
question.setAnswer(Math.min(-5,-8));
questions[8] = question;
// Question 10: My choice
question = new Question("What is the result of the following operation?: |(-2)*5|", MULTIPLE_OPTION, INT);
question.addOption("-10");
question.addOption("10");
question.setAnswer(2);
questions[9] = question;
// Now, start exam
int n_correct = 0;
for(int i = 0; i < 10; i++)
{
Question q = questions[i];
System.out.println(q.toString());
//m get answer
String answer = sc.nextLine();
if(q.evaluate(answer))
n_correct++;
System.out.println("");
}
System.out.println("Results for: " + user_name + ", " + user_major +", " + user_college_level);
System.out.println("Correct: " + n_correct);
System.out.println(String.format("Incorrect: %d", 10 - n_correct));
System.out.println(String.format("Score: %.2f%%", (double)n_correct/(double)10.0 *100.0));
}
public static String getUserName(Scanner sc)
{
/*
This function uses a Scanner to ask to user for name and returns it as a String
This function receives a Scanner object and returns a String
*/
System.out.println("Please enter your name: ");
return sc.nextLine();
}
public static String getUserMajor(Scanner sc)
{
/*
This function uses a Scanner to ask to user for major and returns it as a String
This function receives a Scanner object and returns a String
*/
System.out.println("Please enter your major: ");
return sc.nextLine();
}
public static String getUserCollegeLevel(Scanner sc)
{
/*
This function uses a Scanner to ask to user for college level and returns it as a String
This function receives a Scanner object and returns a String
*/
System.out.println("Please enter your college level: ");
return sc.nextLine();
}
}
QUESTION
public class Question
{
private String text;
private String options[];
private int n_options;
private String answer_text;
private int answer_int;
private float answer_float;
private char answer_char;
private boolean answer_bool;
private double answer_double;
private int question_type;
private int answer_type;
// Here, we define some constant values used to know the type of Question (simple selection, multiple selection, fill, etc)
static final int MULTIPLE_OPTION = 1;
static final int FILL = 2;
static final int SINGLE_OPTION = 3;
static final int TRUE_FALSE = 4;
// Here, we define some constant values used to know the type of answer that a questions needs
static final int INT = 1;
static final int CHAR = 2;
static final int STRING = 3;
static final int FLOAT = 4;
static final int DOUBLE = 5;
static final int BOOL = 6;
// Constructor
public Question(String text, int typ, int answer_typ)
{
this.text = text;
this.question_type = typ;
this.options = new String[100];
this.n_options = 0;
this.answer_type = answer_typ;
}
// method to add options
public void addOption(String option)
{
this.options[this.n_options] = option;
this.n_options++;
}
// methods to check that the given option is correct
public boolean checkAnswer(String answer) {return answer.toLowerCase().compareTo(answer_text) == 0;}
public boolean checkAnswer(int answer) {return answer == answer_int;}
public boolean checkAnswer(float answer) {return answer == answer_float;}
public boolean checkAnswer(boolean answer) {return answer == answer_bool;}
public boolean checkAnswer(char answer) {return answer == answer_char;}
public boolean checkAnswer(double answer) {return answer == answer_double;}
public boolean evaluate(String answer)
{
if(getAnswerType() == STRING)
{
return answer.toLowerCase().compareTo(getAnswerText().toLowerCase()) == 0;
}
else if(getAnswerType() == BOOL)
{
boolean answer_b = false;
if(answer.toLowerCase().compareTo("true") == 0)
answer_b = true;
return getAnswerBool() == answer_b;
}
else if(getAnswerType() == INT)
{
int answer_i = Integer.valueOf(answer);
return answer_i == getAnswerInt();
}
else if(getAnswerType() == DOUBLE)
{
double answer_d = Double.valueOf(answer);
return answer_d == getAnswerDouble();
}
else if(getAnswerType() == FLOAT)
{
float answer_f = Float.valueOf(answer);
return answer_f == getAnswerFloat();
}
return false;
}
// getters
public int getQuestionType() {return question_type;}
public int getAnswerType() {return answer_type;}
public String getQuestionText() {return text;}
public String[] getOptions() {return options;}
public String getAnswerText() {return answer_text;}
public int getAnswerInt() {return answer_int;}
public float getAnswerFloat() {return answer_float;}
public char getAnswerChar() {return answer_char;}
public boolean getAnswerBool() {return answer_bool;}
public double getAnswerDouble() {return answer_double;}
// setters
public void setAnswer(boolean answer) {answer_bool = answer;}
public void setAnswer(String answer) {answer_text = answer;}
public void setAnswer(int answer) {answer_int = answer;}
public void setAnswer(float answer) {answer_float = answer;};
public void setAnswer(char answer) {answer_char = answer;};
public void setAnswer(double answer) {answer_double = answer;};
@Override
public String toString()
{
String ret = text;
ret += "\n";
if(getQuestionType() == MULTIPLE_OPTION)
{
int i = 1;
for(int j = 0; j < n_options;j++)
{
String option = this.options[j];
ret += i + ". " + option + "\n";
i++;
}
}
else if(getQuestionType() == TRUE_FALSE)
ret += "Enter True or False: ";
return ret;
}
}