Question

Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

Objective: GUI Layout manager

Download one of the sample GUI layout program.

Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel.

The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge sort, the text file will be merged sorted and its time will be presented.

The code I provided needs to be updated (needs):

ability to click the button to choose the sort option

to sort the text file after clicked:

             merge uses merge sort

             selection uses selection sort

             insertion uses insertion sort

             quick uses quick sort

all the sorts are provided, so you just need to call the specific sort after the button clicked.(also slection sort uses linked list while the rest can use arrays)

The code i provided only creates the buttons but does nothing else.

Hopefully that helps explain a bit more of what i need.

This is the GUI I created( need to add the ability to click):

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

public class BorderSort
{
//-----------------------------------------------------------------
// Creates several bordered panels and displays them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Border Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
panel.setLayout (new GridLayout (0, 2, 5, 10));
panel.setBorder (BorderFactory.createEmptyBorder (8, 8, 8, 8));

JPanel p1 = new JPanel();
p1.setBorder (BorderFactory.createLineBorder (Color.red, 3));
p1.add (new JLabel ("Quick Sort"));
panel.add (p1);

JPanel p2 = new JPanel();
p2.setBorder (BorderFactory.createLineBorder (Color.red, 3));
p2.add (new JLabel ("Insertion Sort"));
panel.add (p2);

JPanel p3 = new JPanel();
p3.setBorder (BorderFactory.createLineBorder (Color.red, 3));
p3.add (new JLabel ("Merge Sort"));
panel.add (p3);

JPanel p4 = new JPanel();
p4.setBorder (BorderFactory.createLineBorder (Color.red, 3));
p4.add (new JLabel ("Selection Sort"));
panel.add (p4);
frame.getContentPane().add (panel);
frame.pack();
frame.setVisible(true);
}
}

Given Code:

//********************************************************************
// SelectionSortS.java   
//
//********************************************************************

import java.util.Comparator;

public class SelectionSortS
{
private SortNode list;
  
//-----------------------------------------------------------------
// Creates an initially empty linked list.
//-----------------------------------------------------------------
public SelectionSortS()
{
list = null;
}

//-----------------------------------------------------------------
// Adds an integer to the linked list
//-----------------------------------------------------------------
public void add(String value)
{
SortNode node = new SortNode(value);
SortNode current = null;

if (list == null)
list = node;
else
{
current = list;
while (current.next != null)
current = current.next;
current.next = node;
}
}
  
//-----------------------------------------------------------------
// Sorts the linked list using the selection sort.
//-----------------------------------------------------------------
public void sort()
{
SortNode current = list;
SortNode min = list;
SortNode swapPos = list;
  
  
if (list == null)
return;

while (swapPos.next != null)
{
while (current.next != null) // find min value
{
current = current.next;
if (current.value.compareTo(min.value) <0)
min = current;
}

// Swap the values
if (min != swapPos) // a swap was found
{
String temp = min.value;
min.value = swapPos.value;
swapPos.value = temp;
}
swapPos = swapPos.next;
current = swapPos;
min = current;
}
}
  
//-----------------------------------------------------------------
// Returns a listing of the contents of the linked list.
//-----------------------------------------------------------------
public String toString()
{
String report = "";
SortNode current = list;
  
if (current != null)
{
report += String.valueOf(current.value) + " ";
while (current.next != null)
{
current = current.next;
report += String.valueOf(current.value) + " ";
}
}

return report;
}
  
//*****************************************************************
// An inner class that represents a node containing String.
// The public variables are only visible in the outer class.
//*****************************************************************
private class SortNode
{
public String value;
public SortNode next;

//--------------------------------------------------------------
// Sets up the node.
//--------------------------------------------------------------
public SortNode (String current)
{
value = current;
next = null;
}
}
}

/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/

// Sorting main function for testing correctness of sort algorithm.
// To use: [+/-]
// + means increasing values, - means decreasing value and no
// parameter means random values;
// where controls the number of objects to be sorted;
// and controls the threshold parameter for certain sorts, e.g.,
// cutoff point for quicksort sublists.

import java.io.*;

public class QuickSort {

static int THRESHOLD = 0;

static int ARRAYSIZE = 0;

static > void Sort(E[] A) {
qsort(A, 0, A.length-1);
}

static int MAXSTACKSIZE = 1000;
static >

void qsort(E[] A, int i, int j) { // Quicksort
int pivotindex = findpivot(A, i, j); // Pick a pivot
DSutil.swap(A, pivotindex, j); // Stick pivot at end
// k will be the first position in the right subarray
int k = partition(A, i-1, j, A[j]);
DSutil.swap(A, k, j); // Put pivot in place
if ((k-i) > 1) qsort(A, i, k-1); // Sort left partition
if ((j-k) > 1) qsort(A, k+1, j); // Sort right partition
}

static >
int partition(E[] A, int l, int r, E pivot) {
do { // Move bounds inward until they meet
while (A[++l].compareTo(pivot)<0);
while ((r!=0) && (A[--r].compareTo(pivot)>0));
DSutil.swap(A, l, r); // Swap out-of-place values
} while (l < r); // Stop when they cross
DSutil.swap(A, l, r); // Reverse last, wasted swap
return l; // Return first position in right partition
}

static >
int findpivot(E[] A, int i, int j)
{ return (i+j)/2; }


}

/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/

// Sorting main function for testing correctness of sort algorithm.
// To use: [+/-]
// + means increasing values, - means decreasing value and no
// parameter means random values;
// where controls the number of objects to be sorted;
// and controls the threshold parameter for certain sorts, e.g.,
// cutoff point for quicksort sublists.

import java.io.*;

public class MergeSort {

static int THRESHOLD = 0;

static int ARRAYSIZE = 0;

@SuppressWarnings("unchecked") // Generic array allocation
static > void Sort(E[] A) {
E[] temp = (E[])new Comparable[A.length];
mergesort(A, temp, 0, A.length-1);
}

static >
void mergesort(E[] A, E[] temp, int l, int r) {
int mid = (l+r)/2; // Select midpoint
if (l == r) return; // List has one element
mergesort(A, temp, l, mid); // Mergesort first half
mergesort(A, temp, mid+1, r); // Mergesort second half
for (int i=l; i<=r; i++) // Copy subarray to temp
temp[i] = A[i];
// Do the merge operation back to A
int i1 = l; int i2 = mid + 1;
for (int curr=l; curr<=r; curr++) {
if (i1 == mid+1) // Left sublist exhausted
A[curr] = temp[i2++];
else if (i2 > r) // Right sublist exhausted
A[curr] = temp[i1++];
else if (temp[i1].compareTo(temp[i2])<0) // Get smaller
A[curr] = temp[i1++];
else A[curr] = temp[i2++];
}
}

}

/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/

// Sorting main function for testing correctness of sort algorithm.
// To use: [+/-]
// + means increasing values, - means decreasing value and no
// parameter means random values;
// where controls the number of objects to be sorted;
// and controls the threshold parameter for certain sorts, e.g.,
// cutoff point for quicksort sublists.

import java.io.*;

public class InsertionSort {

static int THRESHOLD = 0;

static int ARRAYSIZE = 0;
static >
void Sort(E[] A, int l) {
for (int i=1; i<l; i++)="" insert="" i'th="" record="" for="" (int="" j="i;" (j="">0)="" &&="" (a[j].compareto(a[j-1])<0);="" j--)DSutil.swap(A, j, j-1);
}


}

/** Source code example for "A Practical Introduction to Data
Structures and Algorithm Analysis, 3rd Edition (Java)"
by Clifford A. Shaffer
Copyright 2008-2011 by Clifford A. Shaffer
*/

import java.util.*;
import java.math.*;

/** A bunch of utility functions. */
class DSutil {

/** Swap two Objects in an array
@param A The array
@param p1 Index of one Object in A
@param p2 Index of another Object A
*/
public static void swap(E[] A, int p1, int p2) {
E temp = A[p1];
   A[p1] = A[p2];
   A[p2] = temp;
}

/** Randomly permute the Objects in an array.
@param A The array
*/

// int version
// Randomly permute the values of array "A"
static void permute(int[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element

public static void swap(int[] A, int p1, int p2) {
int temp = A[p1];
   A[p1] = A[p2];
   A[p2] = temp;
}

/** Randomly permute the values in array A */
static void permute(E[] A) {
for (int i = A.length; i > 0; i--) // for each i
swap(A, i-1, DSutil.random(i)); // swap A[i-1] with
} // a random element

/** Initialize the random variable */
static private Random value = new Random(); // Hold the Random class object

/** Create a random number function from the standard Java Random
class. Turn it into a uniformly distributed value within the
range 0 to n-1 by taking the value mod n.
@param n The upper bound for the range.
@return A value in the range 0 to n-1.
*/
static int random(int n) {
   return Math.abs(value.nextInt()) % n;
}

}

Text File (part):

HO09C1068A,HONDA,FIT,Front bumper license plate,2015,7.00
HO11F1213B,HONDA,ODYSSEY,RT Front bumper molding,2014,7.00
KI17B1042A,KIA,FORTE SDN,LT Front bumper cover retainer,2015,7.00
KI17B1043A,KIA,FORTE KOUP,RT Front bumper cover retainer,2015,7.00
SU04D1042D,SUBARU,FORESTER,LT Rear bumper cover retainer,2015,7.00
SU04D1043D,SUBARU,FORESTER,RT Rear bumper cover retainer,2015,7.00
HO11F1213A,HONDA,ODYSSEY,RT Front bumper molding,2014,8.00
HO11F2598A,HONDA,ODYSSEY,LT Front fog lamp cover,2014,9.00
HO11F2599A,HONDA,ODYSSEY,RT Front fog lamp cover,2014,9.00
HY03G2599A,HYUNDAI,ELANTRA SEDAN,RT Front fog lamp cover,2013,9.00
HY03G2598A,HYUNDAI,ELANTRA SEDAN,LT Front fog lamp cover,2013,9.00
SU04D1042C,SUBARU,FORESTER,LT Rear bumper cover retainer,2015,9.00
SU04D1043C,SUBARU,FORESTER,RT Rear bumper cover retainer,2015,9.00
VW11E2513B,VOLKSWAGEN,GOLF,RT Fog Lamp Ring Bezel,2014,9.00
DG06C1042A,DODGE,AVENGER,LT Front bumper cover support,2010,10.00
SZ10C2599A,SUZUKI,GRAND VITARA,RT Front fog lamp cover,2013,11.00
SZ10C2599B,SUZUKI,GRAND VITARA,RT Front fog lamp cover,2013,11.00
SZ10C2598B,SUZUKI,GRAND VITARA,LT Front fog lamp cover,2013,11.00
SZ10C2598A,SUZUKI,GRAND VITARA,LT Front fog lamp cover,2013,12.00

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

The issue is there about the basic design principle. You should use JButton if you want them to be used for clicking [JLabel

JButton jbSelection = new JButton ("Selection Sort");

p4.add (jbSelection);

jbSelection.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

SelectionSortS.sort(yourItem);

}

} );

String []content =new String[100]; //Max 100 lines to read

int i = 0;

Scanner sc =new Scanner (new File(“filename.txt”));

while (sc.hasNextLine()) {

     content[i] = sc.nextLine();

     i++;

}

Add a comment
Know the answer?
Add Answer to:
Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...
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
  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

    Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...

  • JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of...

    JAVA Submit:    HashSet.java    with the following methods added: HashSet.java // Implements a set of objects using a hash table. // The hash table uses separate chaining to resolve collisions. // Original from buildingjavaprograms.com supplements public class HashSet<E> { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry<E>[] elementData; private int size; // Constructs an empty set. @SuppressWarnings("unchecked") public HashSet() { elementData = new HashEntry[10]; size = 0; } // ADD METHODS HERE for exercise solutions: // Adds the...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

  • Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Desc...

    Need Help finishing up MyCalendar Program: Here are the methods: Modifier and Type Method Description boolean add​(Event evt) Add an event to the calendar Event get​(int i) Fetch the ith Event added to the calendar Event get​(java.lang.String name) Fetch the first Event in the calendar whose eventName is equal to the given name java.util.ArrayList<Event> list() The list of all Events in the order that they were inserted into the calendar int size() The number of events in the calendar java.util.ArrayList<Event>...

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