Answered! Implement a Java GUI application that prompts the user for the list of integers delimited by commas and a…

Implement a Java GUI application that prompts the user for the list of integers delimited by commas and a number to search using binary search algorithm.   The below screen shot is expected

Binary Search Specify the list of numbers for binary-searching, delimited by commas: 12, 18, 23, 28, 31, 40, 52, 69, 73, 76, 77, 78, 78, 86, 95 Specify number to Search: 33 Search

The GUI layout includes:

A JLabel control to display the message: “Specify the list of numbers for binary-searching, delimited by commas”.

A JTextField control to accept the list of integer numbers delimited by commas.

A JLabel control to display the message: “Specify number to search”.

A JTextField control to accept an integer (key) to search for.

A JButton (labelled as “Search”) – Pressing this button will perform binary-searching for the specified key number.

A JLabel control to display the search result. This JLabel control must have a black border.

It’s recommended to use the binary search code (function) from the demo, “BinarySearchTest”, prepared for Session 8 for this code project with some modifications. Alternatively, you can use the binarySearch() method of the class Arrays.

If the search number is found, display the message to indicate that it’s found and its index in the list of numbers entered by the user. For example,

99 was found in position 14

If the search number cannot be found, display the message to indicate that it cannot be found. For example,

99 was not found

Expert Answer

 JAVA CODE

package binarySearch.GUI;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.BoxLayout;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import java.awt.GridLayout;

import java.awt.FlowLayout;

import java.awt.GridBagLayout;

import java.awt.GridBagConstraints;

import java.awt.Insets;

import javax.swing.JTextField;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.Color;

import javax.swing.border.EtchedBorder;

import javax.swing.border.LineBorder;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

public class BinarySearch extends JFrame {

private static JTextField tf_numbers;

private JTextField tf_num;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

BinarySearch frame = new BinarySearch();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public BinarySearch() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 264);

GridBagLayout gridBagLayout = new GridBagLayout();

gridBagLayout.columnWidths = new int[]{193, 38, 0, 0, 0, 0, 0};

gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};

gridBagLayout.columnWeights = new double[]{1.0, 1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};

gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};

getContentPane().setLayout(gridBagLayout);

JLabel lblNewLabel = new JLabel(“Specify the list of numbers for binary search, delimited by commas:”);

lblNewLabel.setFont(new Font(“Tahoma”, Font.PLAIN, 13));

GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();

gbc_lblNewLabel.anchor = GridBagConstraints.WEST;

gbc_lblNewLabel.gridwidth = 6;

gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);

gbc_lblNewLabel.gridx = 0;

gbc_lblNewLabel.gridy = 0;

getContentPane().add(lblNewLabel, gbc_lblNewLabel);

tf_numbers = new JTextField();

tf_numbers.addKeyListener(new KeyAdapter() {

@Override

public void keyTyped(KeyEvent arg0) { // input validation to accept only numbers, comma and space

char ch = arg0.getKeyChar();

if(ch==’ ‘ || ch==’,’|| isNum(ch))

{

}

else

arg0.consume();

}

});

GridBagConstraints gbc_tf_numbers = new GridBagConstraints();

gbc_tf_numbers.insets = new Insets(0, 0, 5, 0);

gbc_tf_numbers.gridwidth = 6;

gbc_tf_numbers.fill = GridBagConstraints.HORIZONTAL;

gbc_tf_numbers.gridx = 0;

gbc_tf_numbers.gridy = 1;

getContentPane().add(tf_numbers, gbc_tf_numbers);

tf_numbers.setColumns(10);

JLabel lblNewLabel_1 = new JLabel(“Specify number to search:”);

lblNewLabel_1.setFont(new Font(“Tahoma”, Font.PLAIN, 13));

GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();

gbc_lblNewLabel_1.fill = GridBagConstraints.HORIZONTAL;

gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);

gbc_lblNewLabel_1.gridx = 0;

gbc_lblNewLabel_1.gridy = 2;

getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);

tf_num = new JTextField();

GridBagConstraints gbc_tf_num = new GridBagConstraints();

gbc_tf_num.insets = new Insets(0, 0, 5, 0);

gbc_tf_num.fill = GridBagConstraints.HORIZONTAL;

gbc_tf_num.gridwidth = 5;

gbc_tf_num.gridx = 1;

gbc_tf_num.gridy = 2;

getContentPane().add(tf_num, gbc_tf_num);

tf_num.setColumns(10);

JButton search_btn = new JButton(“Search”);

GridBagConstraints gbc_search_btn = new GridBagConstraints();

gbc_search_btn.insets = new Insets(0, 0, 5, 0);

gbc_search_btn.gridx = 5;

gbc_search_btn.gridy = 3;

getContentPane().add(search_btn, gbc_search_btn);

tf_numbers.requestFocusInWindow();

JPanel panel = new JPanel();

panel.setBorder(new LineBorder(new Color(0, 0, 0)));

GridBagConstraints gbc_panel = new GridBagConstraints();

gbc_panel.gridwidth = 6;

gbc_panel.insets = new Insets(0, 0, 0, 5);

gbc_panel.fill = GridBagConstraints.BOTH;

gbc_panel.gridx = 0;

gbc_panel.gridy = 5;

getContentPane().add(panel, gbc_panel);

GridBagLayout gbl_panel = new GridBagLayout();

gbl_panel.columnWidths = new int[]{0, 0};

gbl_panel.rowHeights = new int[]{0, 0};

gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};

gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};

panel.setLayout(gbl_panel);

JLabel lbl_result = new JLabel(” “);

GridBagConstraints gbc_lbl_result = new GridBagConstraints();

gbc_lbl_result.fill = GridBagConstraints.VERTICAL;

gbc_lbl_result.anchor = GridBagConstraints.WEST;

gbc_lbl_result.gridx = 0;

gbc_lbl_result.gridy = 0;

panel.add(lbl_result, gbc_lbl_result);

search_btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

String st = tf_numbers.getText();

String[] numArray = st.split(“,”);

int len = numArray.length;

int[] array = new int[len];

for(int i=0; i<len; i++){

try{

array[i] = Integer.parseInt(numArray[i].trim());

System.out.println(“ANYUJ: “+array[i]);

}

catch(NumberFormatException ex)

{

}

}

int flag = 0;

int num = 0;

try{

num = Integer.parseInt(tf_num.getText());

flag = 0;

}

catch(NumberFormatException ex)

{

JOptionPane.showMessageDialog(null, “Please input a valid number”, “Error”, JOptionPane.ERROR_MESSAGE);

flag = 1;

}

if(flag == 0){

int found = binarySearch(array, num);

if(found == -1)

lbl_result.setText(String.valueOf(num)+” was not found”);

else

lbl_result.setText(String.valueOf(num)+” was found at postion “+String.valueOf(found));

}

}

});

}

private static boolean isNum(char chr)

{

return chr >= ‘0’ && chr <= ‘9’;

}

// Binary Search method

private static int binarySearch(int[] array, int num)

{

int first = 0;

int last = array.length-1;

 

 

while(first <= last)

{

int middle = first + (last – first) / 2;

if(array[middle] < num)

{

first = middle+1;

}

else if(array[middle] > num)

{

last = middle – 1;

}

else

{

return middle+1;

 

}

 

}

return -1;

}

}

DIRECTIONS: Open your java ide create a new java project with package name binarySearch.GUI and within that package create a new class named BinarySearch and then copy-paste the above code into the file and run.

OUTPUT

Still stressed from student homework?
Get quality assistance from academic writers!