Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello! I need help with this assignment. Here is a photo of what the GUI should

ID: 672226 • Letter: H

Question

Hello! I need help with this assignment. Here is a photo of what the GUI should like.

Basically I need help displaying the scores.

This is what I got so far:

import javax.swing.*;

public class BowlingGameViewer
{
public static void main(String[] args)
{
JFrame frame = new BowlingGameFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

---------

public BowlingGame(int initialScore)
{   
score = initialScore;
}

  
/**
Gets the score of the bowling game
@return the score
*/
public int getScore()
{   
return score;
}

public int compareTo(Object otherObject)
{
BowlingGame otherGame = (BowlingGame) otherObject;
return score - otherGame.getScore();
}

// Override toString method - display name of class and instance fields in square bracket
@Override
public String toString()
{
return "BowlingGame[score = " + getScore() + "]";
}
private int score;
}

--------------------------------------


import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

public class BowlingGameFrame extends JFrame implements ActionListener
{

private ArrayList<BowlingGame> games;

// Declare GUI components as instance fields
// Add rest of GUI components here


private JPanel panel;
private JButton button;
private JLabel resultLabel;
private JTextField field1;
private JTextArea area1;
private JScrollPane pane1;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 250;
private int score;


public BowlingGameFrame ()
{
games = new ArrayList<BowlingGame>();

// Populate with 10 scores
games.add(new BowlingGame(150));
games.add(new BowlingGame(250));
games.add(new BowlingGame(125));
games.add(new BowlingGame(166));
games.add(new BowlingGame(133));
games.add(new BowlingGame(202));
games.add(new BowlingGame(188));
games.add(new BowlingGame(177));
games.add(new BowlingGame(144));
games.add(new BowlingGame(199));
Collections.sort(games);

// Use helper methods
createButton();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

private void createButton()
{
// create button, create listener, attach listener to button
button = new JButton("Display Games");
button.addActionListener(this);
}

private void createPanel()
{
panel = new JPanel();
// add GUI components to panel
resultLabel = new JLabel("Games Over: ");
field1 = new JTextField(5);
area1 = new JTextArea();
pane1 = new JScrollPane();
panel.add(resultLabel);
panel.add(field1);
panel.add(area1);
panel.add(button);
panel.add(pane1);
add(panel);
}

@Override
public void actionPerformed(ActionEvent event)
{
if (button == event.getSource())
{
int score = Integer.parseInt(field1.getText());

for (int i = 0; i < games.size(); i ++)
{
if (games.get(i).getScore() >= score)

}
}
  
  
}
  
}

Explanation / Answer

modifications:

public void actionPerformed(ActionEvent event)

{

if (button == event.getSource())

{

int score = Integer.parseInt(field1.getText());

for (int i = 0; i < games.size(); i ++)

{

if (games.get(i).getScore() >= score)

{

area1.append(games.get(i).toString()+" ");

}

}

}

}

--------

Collections.sort(games,new Comparator<BowlingGame>()

{

@Override

public int compare(BowlingGame bg1, BowlingGame bg2)

{

if (bg1.getScore() <=bg1.getScore())

return -1;

else

return 1;

}

});

-----------

Sample output:

Modified code to copy:

/*BowlingGameFrame.java*/

import java.awt.event.ActionEvent;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JScrollPane;

import java.util.ArrayList;

public class BowlingGameFrame extends JFrame implements ActionListener

{

    private ArrayList<BowlingGame> games;

// Declare GUI components as instance fields

// Add rest of GUI components here

    private JPanel panel;

    private JButton button;

    private JLabel resultLabel;

    private JTextField field1;

    private JTextArea area1;

    private JScrollPane panel1;

    private static final int FRAME_WIDTH = 400;

    private static final int FRAME_HEIGHT = 250;

    private int score;

    //define a custom comparator to compare employee salaries

   

    public BowlingGameFrame ()

    {

       games = new ArrayList<BowlingGame>();

     // Populate with 10 scores

      games.add(new BowlingGame(150));

      games.add(new BowlingGame(250));

      games.add(new BowlingGame(125));

      games.add(new BowlingGame(166));

      games.add(new BowlingGame(133));

      games.add(new BowlingGame(202));

      games.add(new BowlingGame(188));

      games.add(new BowlingGame(177));

      games.add(new BowlingGame(144));

      games.add(new BowlingGame(199));

      Collections.sort(games,new Comparator<BowlingGame>()

         {

          @Override

          public int compare(BowlingGame bg1, BowlingGame bg2)

          {

              if (bg1.getScore() <=bg1.getScore())

                   return -1;

              else

                        return 1;

          }

         });

     

       // Use helper methods

       createButton();

       createPanel();

       setSize(FRAME_WIDTH, FRAME_HEIGHT);

    }

   

    private void createButton()

    {

// create button, create listener, attach listener to button

       button = new JButton("Display Games");

       button.addActionListener(this);

    }

   private void createPanel()

    {

       panel = new JPanel();

// add GUI components to panel

       resultLabel = new JLabel("Games Over: ");

       field1 = new JTextField(5);

       area1 = new JTextArea();

       panel1 = new JScrollPane();

       panel.add(resultLabel);

       panel.add(field1);

       area1.add(panel1);

       panel.add(button);

       panel.add(area1);

        add(panel);

    }

    @Override

     public void actionPerformed(ActionEvent event)

     {

         if (button == event.getSource())

         {

           int score = Integer.parseInt(field1.getText());

       

            for (int i = 0; i < games.size(); i ++)

            {

                if (games.get(i).getScore() >= score)

                {

                     area1.append(games.get(i).toString()+" ");

                }

               

            }

         }

        

        

     }

  

}

/*BowlingGame.java*/

import java.util.Comparator;

public class BowlingGame

{

         public BowlingGame(int initialScore)

         {  

            score = initialScore;

         }

        

         /**

            Gets the score of the bowling game

            @return the score

         */

         public int getScore()

         {  

            return score;

         }

     // Override toString method - display name of class and instance fields in square bracket

         @Override

          public String toString()

         {

             return "BowlingGame[score = " + getScore() + "]";

         }

         private int score;

}

/*BowlingGameViewer.java*/

import javax.swing.*;

public class BowlingGameViewer

{

    public static void main(String[] args)

    {

       JFrame frame = new BowlingGameFrame();

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setVisible(true);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote