This programme is done by a chegg expert. Can anyone explained this whole code?
ID: 3881842 • Letter: T
Question
This programme is done by a chegg expert.
Can anyone explained this whole code? And how it's work. Please answer as soon as possible...
import java.awt.BorderLayout;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.applet.AudioClip;
import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Jeopardy implements ActionListener { private JButton firstButton; private JButton secondButton; private JButton thirdButton, fourthButton; private JPanel quizPanel; int score = 0; JLabel scoreBox = new JLabel("0"); int buttonCount = 0; public static void main(String[] args) { new Jeopardy().start(); } private void start() { JFrame frame = new JFrame(); quizPanel = new JPanel(); frame.setLayout(new BorderLayout()); // 1. Make the frame show up frame.setVisible(true); // 2. Give your frame a title frame.setTitle("Jeopardy"); // 3. Create a JPanel variable to hold the header using the createHeader // method JPanel panel = createHeader("Quantum Physics"); // 4. Add the header component to the quizPanel quizPanel.add(panel); // 5. Add the quizPanel to the frame frame.add(quizPanel); // 6. Use the firstButton variable to hold a button using the // createButton method firstButton = createButton("$100"); // 7. Add the firstButton to the quizPanel quizPanel.add(firstButton); // 8. Write the code inside the createButton() method below. Check that // your game looks like Figure 1 in the Jeopardy Handout - // 9. Use the secondButton variable to hold a button using the // createButton method secondButton = createButton("$200"); // 10. Add the secondButton to the quizPanel quizPanel.add(secondButton); // 11. Add an action listeners to the buttons (2 lines of code) firstButton.addActionListener(this); secondButton.addActionListener(this); // 12. Fill in the actionPerformed() method below frame.pack(); quizPanel.setLayout(new GridLayout(buttonCount + 1, 3)); frame.add(makeScorePanel(), BorderLayout.NORTH); frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().height, Toolkit.getDefaultToolkit().getScreenSize().width); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /* * 13. Use the method provided to play the Jeopardy theme music * * 14. Add buttons so that you have $200, $400, $600, $800 and $1000 * questions * * [optional] Use the showImage or playSound methods when the user answers a * question [optional] Add new topics for the quiz */ private JButton createButton(String dollarAmount) { // Create a new JButton JButton button = new JButton(""); // Set the text of the button to the dollarAmount button.setText(dollarAmount); // Increment the buttonCount (this should make the layout vertical) buttonCount++; // Return your new button instead of the temporary button return button; } public void actionPerformed(ActionEvent arg0) { // Remove this temporary message: JOptionPane .showMessageDialog(null, "pressed " + ((JButton) arg0.getSource()).getText() + " button"); // Use the method that plays the jeopardy theme music. JButton buttonPressed = (JButton) arg0.getSource(); // If the buttonPressed was the firstButton if (buttonPressed == firstButton) { askQuestion("How many dimensions are there?", "11", 100); } // Call the askQuestion() method // Fill in the askQuestion() method. When you play the game, the score // should change. // Or if the buttonPressed was the secondButton // Call the askQuestionRecipe with a harder question // Clear the button text (set the button text to nothing) } private void askQuestion(String question, String correctAnswer, int prizeMoney) { // Remove this temporary message JOptionPane.showMessageDialog(null, "this is where the question will be asked"); // Use a pop up to ask the user the question // If the answer is correct // Increase the score by the prizeMoney // Call the updateScore() method // Pop up a message to tell the user they were correct // Otherwise // Decrement the score by the prizeMoney // Pop up a message to tell the user the correct answer // Call the updateScore() method } public void playJeopardyTheme() { try { AudioInputStream audioInputStream = AudioSystem .getAudioInputStream(new URL( "jeopardy.wav")); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); } catch (Exception ex) { ex.printStackTrace(); } } private void playSound(String fileName) { AudioClip scream = JApplet.newAudioClip(getClass() .getResource(fileName)); scream.play(); } private Component makeScorePanel() { JPanel panel = new JPanel(); panel.add(new JLabel("score:")); panel.add(scoreBox); panel.setBackground(Color.CYAN); return panel; } private void updateScore() { scoreBox.setText("" + score); } private JPanel createHeader(String topicName) { JPanel panelj = new JPanel(); panelj.setLayout(new BoxLayout(panelj, BoxLayout.PAGE_AXIS)); JLabel l1 = new JLabel(topicName); l1.setAlignmentX(Component.CENTER_ALIGNMENT); panelj.add(l1); return panelj; } void showCorrectImage() { showImage("correct.jpg"); } void showIncorrectImage() { showImage("incorrect.jpg"); } private void showImage(String fileName) { JFrame frame = new JFrame(); URL imageURL = getClass().getResource(fileName); Icon icon = new ImageIcon(imageURL); JLabel image = new JLabel(icon); frame.add(image); frame.setVisible(true); frame.pack(); } }Explanation / Answer
Overall the code is just displaying some constant scores at the top and then a label named as "Quantum Physics" and the two buttons and on clicking first button two messages pope up and on clicking second button one message popes up. that's it nothing else is done by code.
Many methods are declared but not used. like (A) playJeopardyTheme() (B) playSound() (C) updateScore() (D) showCorrectImage()
And method "askQuestion()" is not implemented properly. only one message is poped not asking question.
The below code contains comments as per the working of methods and "whether called or not"
import java.awt.BorderLayout;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Jeopardy implements ActionListener {
// just declaring two buttons
private JButton firstButton;
private JButton secondButton;
// you don't need these buttons
//private JButton thirdButton, fourthButton;
private JPanel quizPanel;
int score = 0;
JLabel scoreBox = new JLabel("0");
int buttonCount = 0;
public static void main(String[] args) {
new Jeopardy().start();
}
private void start() {
// create the frame and panel
JFrame frame = new JFrame();
quizPanel = new JPanel();
// set the layout, visibility, title and header of frame ( window).
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setTitle("Jeopardy");
JPanel panel = createHeader("Quantum Physics");
// Add the "header panel" into panel and finally add panel into frame ( window).
quizPanel.add(panel);
frame.add(quizPanel);
// create the buttons and add them into panel
firstButton = createButton("$100");
quizPanel.add(firstButton);
secondButton = createButton("$200");
quizPanel.add(secondButton);
// make buttons actionable on clicking
firstButton.addActionListener(this);
secondButton.addActionListener(this);
frame.pack();
quizPanel.setLayout(new GridLayout(buttonCount + 1, 3));
// this calls makeScorePanel() and fixes it's location also
frame.add(makeScorePanel(), BorderLayout.NORTH);
// this sets the size of frame or window
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().height, Toolkit.getDefaultToolkit().getScreenSize().width);
// window closes on clicking cross button ( on the top-left)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// This method creates the button with the corresponding label and also increases the count of button variable "buttonCount"
private JButton createButton(String dollarAmount) {
JButton button = new JButton("");
button.setText(dollarAmount);
buttonCount++;
return button;
}
// This is called when we press buttons
// When first button is pressed , a message is displayed and also it's if condition become true and this method calls askQuestion().
// When second button is pressed , a message is displayed.
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"pressed " + ((JButton) arg0.getSource()).getText()+ " button");
JButton buttonPressed = (JButton) arg0.getSource();
if (buttonPressed == firstButton) {
askQuestion("How many dimensions are there?", "11", 100);
}
}
// Method is called by actionPerformed()
// Method shows message only and donot display any question
private void askQuestion(String question, String correctAnswer,int prizeMoney) {
JOptionPane.showMessageDialog(null,"this is where the question will be asked");
}
// Method plays the audio
/* Method is never called in the program anywhere */
public void playJeopardyTheme() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new URL("jeopardy.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// plays an audio and get the audio from fileName
/* Method is never called */
private void playSound(String fileName) {
AudioClip scream = JApplet.newAudioClip(getClass().getResource(fileName));
scream.play();
}
// Method returns a panel and in panel scores are added
// Method is called once in starting by start() Method to display starting scores which are '0'
private Component makeScorePanel() {
JPanel panel = new JPanel();
panel.add(new JLabel("score:"));
panel.add(scoreBox);
panel.setBackground(Color.CYAN);
return panel;
}
/* Method is never called by any object */
private void updateScore() {
scoreBox.setText("" + score);
}
// Method returns a panel which will be added in the frame as header
// Method is called once to add "Quantum Physics" in the frame
private JPanel createHeader(String topicName) {
JPanel panelj = new JPanel();
panelj.setLayout(new BoxLayout(panelj, BoxLayout.PAGE_AXIS));
JLabel l1 = new JLabel(topicName);
l1.setAlignmentX(Component.CENTER_ALIGNMENT);
panelj.add(l1);
return panelj;
}
/* this below code is not used in the program anywhere */
void showCorrectImage() {
showImage("correct.jpg");
}
void showIncorrectImage() {
showImage("incorrect.jpg");
}
// Method takes image as icon from fileName and add this into the window
private void showImage(String fileName) {
JFrame frame = new JFrame();
URL imageURL = getClass().getResource(fileName);
Icon icon = new ImageIcon(imageURL);
JLabel image = new JLabel(icon);
frame.add(image);
frame.setVisible(true);
frame.pack();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.