Question

3) Using your Queue class write a GUI program that opens a file contains integers in the range 10 999] in.txt. The program stops reading if -1 is read (Hint: Use multi queues. Your program should use queues to make the output such that the first textfield output contains the integers in the range 0..9 but in their same order as in the input, the same for the second textfield but in the range 10..19, the third textfield but in the range 20.. 29 and so on up to the last line (range 990 999). If there is no integer in a range, skip it without producing an empty textfield for it. Exampe: Input txt: 57 25 59 51 22 28 53 51 59 -1 Open File input txt File is opened output.txt File created with the following Queue 1 25 22 28 Queue 2 57 59 51 53 51 59i need the sloution with java code please :)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE:

package deque;

import java.awt.FlowLayout;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.PriorityQueue;

import java.util.Scanner;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class GUIQueue {

   public static void main(String[] args) {

       JFrame frame = new JFrame("Queue");

      

       ArrayList<PriorityQueue<Integer>> queues = new ArrayList<>(100);

       for(int i=0; i<100; i++)

           queues.add(null);

       JButton openfile = new JButton("Open File");

       JPanel mainPanel = new JPanel();

       mainPanel.setLayout(new GridBagLayout());

       GridBagConstraints contraints = new GridBagConstraints();

       contraints.fill = GridBagConstraints.VERTICAL;

       contraints.gridx = 2;

       contraints.gridy = 0;

       mainPanel.add(openfile, contraints);

       contraints.gridy = 1;

       openfile.addActionListener(new ActionListener() {

          

           @Override

           public void actionPerformed(ActionEvent e) {

               try {

                   Scanner fileReader = new Scanner(new File("in.txt"));

                  

                   while(fileReader.hasNext())

                   {

                       int num = fileReader.nextInt();

                       int index = 0;

                      

                       if(num == -1)

                           break;

                       else if(num<10)

                       {

                           index = 0;

                       }

                       else

                       {

                           index = num/10;

                       }

                       //System.out.println("Index: " + index + " Number: " + num);

                       if(queues.get(index) == null)

                       {

                           PriorityQueue<Integer> queue = new PriorityQueue<>();

                           queue.add(num);

                           queues.set(index, queue);

                       }

                       else queues.get(index).add(num);

                   }

                   fileReader.close();

                   int counter = 1;

                   for(PriorityQueue<Integer> queue: queues)

                   {

                       if(queue != null)

                       {

                           JPanel panel = new JPanel(new FlowLayout());

                           String result = "";

                           while(!queue.isEmpty())

                           {

                               result += queue.peek() + " ";

                               queue.remove();

                           }

                           JTextField field = new JTextField(10);

                           field.setText(result);

                           panel.add(new JLabel("Queue " + counter));

                           panel.add(field);

                           mainPanel.add(panel, contraints);

                           contraints.gridy++;

                           counter++;

                          

                       }

                   }

                   mainPanel.updateUI();

                   mainPanel.repaint();

                  

           } catch (FileNotFoundException ef) {

               // TODO Auto-generated catch block

               ef.printStackTrace();

           }

           }

       });

      

      

      

       frame.setContentPane(mainPanel);

       frame.setSize(500, 500);

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setVisible(true);

   }

}

INPUT FILE:

D GUIQueue.java 目in.txt × M AssignRGBSlider.java 1 57 27 59 51 32 99 120 -1

OUTPUT:

Queue Open File Queue 127 Queue 2 32 Queue 351 57 59 Queue 4 99 Queue 5 120

Add a comment
Know the answer?
Add Answer to:
i need the sloution with java code please :) Using your Queue class write a GUI...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

  • please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in...

    please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in file that contains only positive numbers and outputs to the screen the sum of all numbers. Create your own exception class that describes the situation of a non-positive number. In your program, use this exception to handle this situation. Use other exception classes from the Java standard library to handle other I/O situations.

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Design a Java class named StringQueue for storing strings of first names into a queue. The...

    Design a Java class named StringQueue for storing strings of first names into a queue. The StringQueue.java class contains: * A String[] data field named names that stores the String values in the queue. * A private data field named size that stores the number of names in the queue. * A public static final data field named DEFAULT_CAPACITY = 10 for the array. * A private static data field named firstInitCtr that counts the number of names with the...

  • Please do in java as simply as possible with code available for copy and comments Write...

    Please do in java as simply as possible with code available for copy and comments Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. Throw an exception if the file does not exist. An error message should result. For example if file input is: This is a test File output should be 1. This is a test I...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E...

    (java) Please implement your own Dequeue class which has following methods •boolean add(E e)= void addLast(E e)  // two methods are the same. You could implement either one • •void addFirst(E e) • •E getFirst( ) = E peek( ) // two methods are the same. You could implement either one •E getLast( ) •E removeFirst() •E removeLast() Case 1 : Queue •Create a queue which is an instance of Dequeue. The queue should perform with given following input and print...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list or string or loops (while and for) or if,elif,else : You are given a sequence of n non-zero integers a1,a2,…,an. Determine if the given sequence contains a pair of adjacent elements of the same sign (both negative or both positive). Two elements are called adjacent if they stand next to each other in the sequence. Input The first line contains an integer n...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT