Question

In: Computer Science

Describe how you would develop object-oriented features of Java for the Quiz program developed in the...

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.

Solutions

Expert Solution

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.


Related Solutions

Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
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.
Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
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. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
The application uses Java Object-Oriented features for its implementation. Students select from a menu of courses...
The application uses Java Object-Oriented features for its implementation. Students select from a menu of courses for which they wish to register. The program then validates the user selection against the registration business rules. If the selection is valid, the program prints out a confirmation message. Otherwise, the program prints out the current list of registered classes along with total registered credit hours. The program terminates when the user does not want to register for classes any more. There are...
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
Describe the types of hierarchy found in object-oriented systemdescriptions.
Describe the types of hierarchy found in object-oriented system descriptions.
Java language This is your first homework on objects and classes, and the basics of object-oriented...
Java language This is your first homework on objects and classes, and the basics of object-oriented programming. For each exercise, your task is to first write the class that defines the given object. Compile it and check it for syntax errors. Then write the “demo class” with the main program that creates and uses the object. a)You are planning to purchase a new motor boat for cool cruises on Porter’s Lake this summer, but you want to simulate it before...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT