Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with a UML diagram for my program? public class P3GUI extends JFrame { //Make class variables for various Swing objects private

Can someone help me with a UML diagram for my program?

public class P3GUI extends JFrame { //Make class variables for various Swing objects private final JLabel originalLabel = new JLabel("Original List: "); private final JTextField originalField = new JTextField("", 20); private final JLabel sortedLabel = new JLabel("Sorted List: "); private final JTextField sortedField = new JTextField("", 20); private final JButton sortButton = new JButton("Perform Sort"); private final ButtonGroup sortGroup = new ButtonGroup(); private final JRadioButton ascending = new JRadioButton("Ascending"); private final JRadioButton descending = new JRadioButton("Descending"); private final ButtonGroup numericGroup = new ButtonGroup(); private final JRadioButton integer = new JRadioButton("Integer"); private final JRadioButton fraction = new JRadioButton("Fraction");   public P3GUI(String title, int width, int height){ super(title); setFrame(width, height);  //Create panels for swing objects to be placed JPanel inputPanel = new JPanel(); JPanel resultPanel = new JPanel(); JPanel buttonPanel = new JPanel(); JPanel sortPanel = new JPanel(); JPanel numericPanel = new JPanel();  //Set groups borders and titles and add buttons to them sortPanel.setBorder(BorderFactory.createTitledBorder("Sort Order")); sortGroup.add(ascending); sortGroup.add(descending); numericPanel.setBorder(BorderFactory.createTitledBorder("Numeric Type")); numericGroup.add(integer); numericGroup.add(fraction);  //Add swing objects to panels inputPanel.add(originalLabel); inputPanel.add(originalField); resultPanel.add(sortedLabel); resultPanel.add(sortedField); buttonPanel.add(sortButton); sortPanel.add(ascending); sortPanel.add(descending); numericPanel.add(integer); numericPanel.add(fraction);   //Add panels to main frame with layouts JPanel top = new JPanel(); add(top, BorderLayout.NORTH); top.setLayout(new GridLayout(3,1)); top.add(inputPanel); top.add(resultPanel); top.add(buttonPanel);  JPanel bot = new JPanel(); add(bot, BorderLayout.SOUTH); bot.add(sortPanel); bot.add(numericPanel);  //Adjust text fields ablility to be edited originalField.setEditable(true); sortedField.setEditable(false); ascending.setSelected(false); descending.setSelected(false); integer.setSelected(false); fraction.setSelected(false);  //Add an action listener and handler to the construct tree button sortButton.addActionListener((ActionEvent e) -> { try{ String input = originalField.getText(); Sort sortedInput = new Sort(input); if(ascending.isSelected() && integer.isSelected() && input.indexOf('/') == -1){ sortedField.setText(sortedInput.ascend()); }else if(descending.isSelected() && integer.isSelected() && input.indexOf('/') == -1){ sortedField.setText(sortedInput.descend()); }else if(ascending.isSelected() && fraction.isSelected() && input.indexOf('/') >= 0){ sortedField.setText(sortedInput.ascend()); }else if (descending.isSelected() && fraction.isSelected() && input.indexOf('/') >= 0){ sortedField.setText(sortedInput.descend()); } }catch(NumberFormatException x){ JFrame parent1 = new JFrame(); JOptionPane.showMessageDialog(parent1, "Non numberic input"); }catch(NoSuchElementException p){ JFrame parent1 = new JFrame(); JOptionPane.showMessageDialog(parent1, "Non numberic input"); }catch(ClassCastException j){ JFrame parent1 = new JFrame(); JOptionPane.showMessageDialog(parent1, "Enter all integers or all fractions"); } } ); }  //Method to set the frame to visible public void display(){ setVisible(true); }  //Method to set the size of the frame private void setFrame(int width, int height){ setSize(width, height); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }  //Main method public static void main(String[] args) { P3GUI p1 = new P3GUI("Binary Search Tree Sort", 420, 220); p1.display(); }  } //Sort Class public class Sort {   private String token;   private BinaryTree tree = new BinaryTree();     public Sort(String input){     //Tokenize the string containing the expression     StringTokenizer tokens = new StringTokenizer(input);         //While there are more tokens, Get the next token     while(tokens.hasMoreTokens()){            token = tokens.nextToken();       try{       if(Integer.parseInt(token)/1 == Integer.parseInt(token)){       tree.addNode(Integer.parseInt(token));       }       }catch(NumberFormatException e){         StringTokenizer tester = new StringTokenizer(token);                while(tester.hasMoreTokens()){         StringTokenizer tester2 = new StringTokenizer(tester.nextToken(), "/", false);           while(tester2.hasMoreTokens()){             int numerator = Integer.parseInt(tester2.nextToken());             int denominator = Integer.parseInt(tester2.nextToken());             Fraction fractionNode = new Fraction(numerator, denominator);             tree.addNode(fractionNode);           }         }       }      }   }     public String ascend(){     ByteArrayOutputStream baos = new ByteArrayOutputStream();     PrintStream ps = new PrintStream(baos);     PrintStream old = System.out;     System.setOut(ps);     tree.inOrder(tree.root);     System.out.flush();     System.setOut(old);     return baos.toString();   }      public String descend(){     ByteArrayOutputStream baos = new ByteArrayOutputStream();     PrintStream ps = new PrintStream(baos);     PrintStream old = System.out;     System.setOut(ps);     tree.inOrder(tree.root);     System.out.flush();     System.setOut(old);     String x = baos.toString();     StringTokenizer tokens = new StringTokenizer(x);     Stack stackStrings = new Stack();     String tokenD,finalString = "";     while(tokens.hasMoreTokens()){       tokenD = tokens.nextToken();       stackStrings.push(tokenD);     }     while(!stackStrings.isEmpty()){       String y = stackStrings.pop();       finalString = finalString + y + " ";     }     return finalString;   }     public static void main(String[] args){     Sort p1 = new Sort("2/2 6/3 3/4");      }   } //Fraction class public class Fraction implements Comparable<Fraction>{     private int numerator;   private int denominator;     public Fraction(int numerator, int denominator){        this.numerator = numerator;     this.denominator = denominator;   }   @Override   public int compareTo(Fraction other) {     if(denominator<0 && numerator>0){       numerator = numerator * (-1);       denominator = denominator * (-1);     }     if(other.denominator<0 && other.numerator>0){       other.numerator = other.numerator * (-1);       other.denominator = other.denominator * (-1);     }     if(denominator < 0 && numerator < 0){       denominator = denominator * (-1);       numerator = numerator * (-1);     }     if(other.denominator < 0 && other.numerator < 0){       other.denominator = other.denominator * (-1);       other.numerator = other.numerator * (-1);     }     if(denominator == 0 || other.denominator == 0){       JFrame parent1 = new JFrame();       JOptionPane.showMessageDialog(parent1, "Invalid fraction with 0 as denominator : Restart program");       System.exit(numerator);     }     int crossProduct = numerator * other.denominator - denominator * other.numerator;     if(crossProduct >= 0){       return 1;     }else{       return -1;     }      }     public String toString(){     return numerator + "/" + denominator;   } } //Binary Tree public class BinaryTree<T extends Comparable<T>> {     Node root;     public void addNode(T key){     Node newNode = new Node(key);         if(root == null){       root = newNode;     } else {       Node focusNode = root;       Node parent;       while(true){         parent = focusNode;         if(key.compareTo((T)focusNode.key) == -1         || key.compareTo((T)focusNode.key) == 0){           focusNode = focusNode.leftChild;           if(focusNode == null){             parent.leftChild = newNode;             return;           }         }         else{           focusNode = focusNode.rightChild;           if(focusNode == null){             parent.rightChild = newNode;             return;           }         }       }     }   }       public void inOrder(Node focusNode){        if(focusNode != null){       inOrder(focusNode.leftChild);       System.out.print(focusNode + " ");       inOrder(focusNode.rightChild);          }      }          } class Node<T>{     T key;     Node leftChild;   Node rightChild;     Node(T key){     this.key = key;   }     public String toString(){     return "" + key;   } 

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions

Question

=+a. The golf courses were far less crowded.

Answered: 1 week ago