Question: I need to figure out why this code is not running and I found some issues that are causing the program to run but I

I need to figure out why this code is not running and I found some issues that are causing the program to run but I am stuck on the rest.

import javax.swing.*; import java.awt.*; import java.awt.event.*;

/** * Debugging Exercise 2 * * To complete this exercise, you must correct each error, and you must add * a short comment by each correction explaining what the error was and what * you did to correct it. * * There are a total of 8 errors in this file * * @author * @version 1.0 */

public class DebuggingExercise2 extends JFrame implements ActionListener { // Declare variables for the controls that will appear on the screen private JButton castBallot; private JButton resetBallot; private JRadioButton choice1; private JRadioButton choice2; private ButtonGroup choices; private JPanel choicePanel; private JLabel instructionLabel; private JLabel results; // The ballot class which is declared below private Ballot myBallot;//moved class here class Ballot { private boolean hasBeenCast; private int choice; public Ballot() { } public boolean castBallot(int choice)//change to public { if (!hasBeenCast) { this.choice = choice; hasBeenCast = true; return hasBeenCast; } else { return false; } } public void setChoice(int choice) { if (!hasBeenCast) { this.choice = choice; } } public int getChoice() { return choice; } } public static void main(String[] args) { DebuggingExercise2 debugMe = new DebuggingExercise2(); debugMe.setSize(400, 200); debugMe.createGUI(); debugMe.setVisible(true);//set to true }

private void createGUI() { setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane(); window.setLayout(new GridBagLayout()); // The GridBagLayout requires this additional object to function properly // You use the GridBagConstraints object to set the location of each // component before you add it to the panel GridBagConstraints gbc = new GridBagConstraints(); instructionLabel = new JLabel("Select a choice and cast your ballot"); instructionLabel.setHorizontalAlignment(SwingConstants.CENTER); // These lines set the coordinates of where the component should go // on the screen. None of the lines for the gbc object in this method // are valid errors. gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.weightx = 0.5;

window.add(instructionLabel, gbc); choicePanel = new JPanel(); choicePanel.setLayout(new GridLayout(2, 1)); choice1 = new JRadioButton("Candidate 1"); choice2 = new JRadioButton("Candidate 2"); choices = new ButtonGroup(); choices.add(choice1); choices.add(choice2); choicePanel.add(choice1); choicePanel.add(choice2); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 2; gbc.weightx = 0.5; gbc.weighty = 0.5; window.add(choicePanel, gbc); castBallot = new JButton("Cast your ballot"); castBallot.addActionListener(this); castBallot.setPreferredSize(new Dimension(75, 25)); gbc.gridx = 1; gbc.gridheight = 1; window.add(castBallot, gbc); resetBallot = new JButton("Reset ballot"); resetBallot.addActionListener(this); resetBallot.setPreferredSize(new Dimension(75, 25)); resetBallot.setEnabled(false); gbc.gridy = 2; window.add(resetBallot, gbc); results = new JLabel(" "); results.setHorizontalAlignment(SwingConstants.CENTER); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2; } public void actionPerformed(ActionEvent ae) { JButton source = (JButton) ae.getSource(); if (source == castBallot)//added second = sign { if (!choice1.isSelected() & !choice2.isSelected()) { results.setText("You must select a candidate before you can cast your vote."); } else { if (myBallot == null) { int choice;//moved variable outside if (choice1.isSelected()) { choice = 1;//this is an int not string } else { choice = 2; } myBallot = new Ballot(); myBallot.castBallot(choice); results.setText("Thank you for voting."); resetBallot.setEnabled(true); } else { results.setText("You already voted for " + (myBallot.getChoice() == 2? choice2.getText() : choice1.getText()) + ".");//changed number to 2 } } } else { myBallot = null; choices.clearSelection(); results.setText(" "); return; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!