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
  • 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 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...

  • 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 : i have 15 min to submit the solution please help me   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...

  • 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 LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • write the program with the language NODEJS need help Programming challenge, screenshot your work and output.  ...

    write the program with the language NODEJS need help Programming challenge, screenshot your work and output.   Description A positive integer is a palindrome if its decimal representation (without leading zeros, is a palindromic string (a string that reads the same forwards and backwards). For example, the number 5, 77, 363, 4884, 11111, 12121 and 349943 are palindromes A range of integers is interesting if it contains an even number of palindromes. The range [L, R], with L <= R, is...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...

    Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue. The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can...

  • Hallo! I have a problem in my code, I need help please in java code. My...

    Hallo! I have a problem in my code, I need help please in java code. My task is : Write a program that can read a text from a file and do frequency analysis on the letters that occur . To store information (frequency) you must use an array. Tip! The place where you collect the frequency of eg the letter A is the equivalent of the ASCII code of "A" ie 65. The array should be 127 elements. For...

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