In: Computer Science
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners.
ANSWER:
package Game;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
// Class QuizTest definition
public class QuizTest
{
// Component and container object declared
JFrame jf;
JTextField ansT;
JButton ansB, newB;
JLabel qusL, imgL, gradeL;
JPanel jp1, jp2, jp3, mainP;
// Random class object declared
Random rand;
// To store final mark and counter to count up to 5
int mark, count;
// To store operators
String operator[] = {"+", "-", "*", "/"};
// Three variables to store random numbers for operand and
operator
int first, second, opt, result;
// Default constructor
QuizTest()
{
// Creates Random class object
rand = new Random();
// Creates a frame
jf = new JFrame("Math Test");
// Creates a text field
ansT = new JTextField(10);
// Creates a button
ansB = new JButton("Check");
newB = new JButton("New Question");
// Creates labels
qusL = new JLabel("");
gradeL = new JLabel("");
imgL = new JLabel("");
// Creates panels
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
mainP = new JPanel();
// Adds the components to panel 1
jp1.add(qusL);
jp1.add(ansT);
jp1.add(ansB);
jp1.add(newB);
// Set the main panel layout to GridLayout
mainP.setLayout(new GridLayout(2, 2));
jp2.add(imgL);
// Adds grade label to panel 2
jp2.add(gradeL);
// Adds the panels to main panel
mainP.add(jp1);
mainP.add(jp2);
// Set the main panel layout to GridLayout
mainP.setLayout(new GridLayout(2, 1));
// Adds the main panel to frame
jf.add(mainP);
jf.setLocationRelativeTo(null);
jf.setSize(400, 600);
// Generate three index numbers between 0 and 50
first = rand.nextInt(50);
second = rand.nextInt(50);
// Generates random number between 0 and 3 for operators
opt = rand.nextInt(3);
// Register listeners with the new button button anonymous
class
newB.addActionListener(new ActionListener()
{
// Overrides actionPerformed method of ActionListener
interface
public void actionPerformed(ActionEvent e)
{
// Checks if question counter is 5
if(count == 5)
{
// Reset the question counter and mark to 0
count = 0;
mark = 0;
}// End of if condition
// Increase the counter by one
count = count + 1;
// Generate three index numbers between 0 and 50
first = rand.nextInt(50);
second = rand.nextInt(50);
// Generates a random number between 0 and 3 for operator
opt = rand.nextInt(3);
// Generates a question and set it in label
qusL.setText("What is " + first + " " + operator[opt] + " " +
second + "? ");
ansT.setText("");
}// End of method
});// End of anonymous class
// Register listeners with the answer button anonymous
class
ansB.addActionListener(new ActionListener()
{
// Overrides actionPerformed method of ActionListener
interface
public void actionPerformed(ActionEvent e)
{
// Set the grade label text and icon part to null
gradeL.setIcon(null);
gradeL.setText("");
// Attaches the images to image icon
ImageIcon iconLogo1 = new ImageIcon("Right.jpg");
ImageIcon iconLogo2 = new ImageIcon("Wrong.png");
ImageIcon iconLogo3 = new ImageIcon("Smile.jpg");
ImageIcon iconLogo4 = new ImageIcon("Sad.jpg");
// Checks the operator
switch(operator[opt])
{
// For addition operator
case "+":
// Calculates the result
result = first + second;
// Checks computer calculated result with user entered result
// Converts the user entered result to integer and then
checks
if(result == Integer.parseInt(ansT.getText()))
{
// Sets the images to right image
imgL.setIcon(iconLogo1);
// Increase the mark by ten
mark += 5;
}// End of if
// Otherwise wrong answer
else
// Sets the images to wrong image
imgL.setIcon(iconLogo2);
break;
// For multiplication operator
case "-":
// Calculates the result
result = first - second;
// Checks computer calculated result with user entered result
// Converts the user entered result to integer and then
checks
if(result == Integer.parseInt(ansT.getText()))
{
// Sets the images to right image
imgL.setIcon(iconLogo1);
// Increase the mark by ten
mark += 5;
}// End of if
// Otherwise wrong answer
else
// Sets the images to wrong image
imgL.setIcon(iconLogo2);
break;
case "*":
// Calculates the result
result = first * second;
// Checks computer calculated result with user entered result
// Converts the user entered result to integer and then
checks
if(result == Integer.parseInt(ansT.getText()))
{
// Sets the images to right image
imgL.setIcon(iconLogo1);
// Increase the mark by ten
mark += 5;
}// End of if
// Otherwise wrong answer
else
// Sets the images to wrong image
imgL.setIcon(iconLogo2);
break;
// For division operator
case "/":
// Calculates the result
result = first / second;
// Checks computer calculated result with user entered result
// Converts the user entered result to integer and then
checks
if(result == Integer.parseInt(ansT.getText()))
{
// Sets the images to right image
imgL.setIcon(iconLogo1);
// Increase the mark by ten
mark += 5;
}// End of if
// Otherwise wrong answer
else
// Sets the images to wrong image
imgL.setIcon(iconLogo2);
break;
}// End of switch - case
// Checks if the counter value is 5
if(count == 5)
{
// Reset the counter to zero
count = 0;
// Checks if marks is greater than or equals to 25
if(mark >= 25)
{
// Set the imgL to null
imgL.setIcon(null);
// Sets the gradeL text to grade A
gradeL.setText("Grade A" + mark);
// Sets the gradeL image icon to smile image
gradeL.setIcon(iconLogo3);
}// End of if condition
// Otherwise checks if marks is greater than or equals to 20
else if(mark >= 20)
{
// Set the imgL to null
imgL.setIcon(null);
// Sets the gradeL text to grade B
gradeL.setText("Grade B" + mark);
// Sets the gradeL image icon to smile image
gradeL.setIcon(iconLogo3);
}// End of else if condition
// Otherwise checks if marks is greater than or equals to 10
else if(mark >= 10)
{
// Set the imgL to null
imgL.setIcon(null);
// Sets the gradeL text to grade C
gradeL.setText("Grade C" + mark);
// Sets the gradeL image icon to smile image
gradeL.setIcon(iconLogo3);
}// End of else if condition
// Otherwise marks is less than 10
else
{
// Set the imgL to null
imgL.setIcon(null);
// Sets the gradeL text to grade F
gradeL.setText("Grade F" + mark);
// Sets the gradeL image icon to sad image
gradeL.setIcon(iconLogo4);
}// End of else
}// End of if condition
}// End of method
});// End of anonymous class
// Sets the visible property of the frame to true
jf.setVisible(true);
// Sets the size of the frame
jf.setSize(250, 500);
// Close the frame when close button of the frame is clicked
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// End of constructor
// main method definition
public static void main(String[] args)
{
// Calls the constructor
new QuizTest();
}// End of main method
}// End of class
Sample Output:
If you do not get anything in this solution,please put a comment and i will help you out.
Do not give a downvote instantly.It is a humble request.
If you like my answer ,please give an upvote....Thank you.