Java Homework, need help ..

Discussion in 'Mac Programming' started by letsdiscussit, Apr 23, 2008.

  1. letsdiscussit macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #1
    Great thanks for the help so far. I created a GUI interface, what's left is the implementation of the sorting code. Can someone help? TX

    Code:
    /*
     * GUI.java
     *
     * Created on April 24, 2008, 6:43 PM
     */
    
    package learn;
    
    /**
     *
     * @author  unknown
     */
    public class GUI extends javax.swing.JFrame {
        
        /** Creates new form GUI */
        public GUI() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            Label1 = new javax.swing.JLabel();
            Label2 = new javax.swing.JLabel();
            NumberField = new javax.swing.JTextField();
            NumberText = new javax.swing.JLabel();
            SortButton = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            sortBox = new javax.swing.JTextPane();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setAlwaysOnTop(true);
            setBackground(new java.awt.Color(200, 200, 100));
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    
            Label1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
            Label1.setText("Java Programming");
    
            Label2.setText("Assignment 3");
    
            NumberField.setText("Enter number here");
            NumberField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    NumberFieldActionPerformed(evt);
                }
            });
    
            NumberText.setText("Enter random number:");
    
            SortButton.setText("Sort");
            SortButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    SortButtonActionPerformed(evt);
                }
            });
    
            jScrollPane1.setViewportView(sortBox);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(146, 146, 146)
                            .add(Label2))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(NumberText))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(SortButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                                .add(NumberField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(111, 111, 111)
                            .add(Label1)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(20, 20, 20)
                    .add(Label1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(Label2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(46, 46, 46)
                    .add(NumberText)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(NumberField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(SortButton)
                    .add(18, 18, 18)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void NumberFieldActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
    }
    
       [B] private void SortButtonActionPerformed(java.awt.event.ActionEvent evt) {
    
      int tempFahr = (int)((Double.parseDouble(NumberField.getText())));
    
    
      
        sortBox.setText(tempFahr + " ascending");  [/B]      // TODO add your handling code here:
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new GUI().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify
        private javax.swing.JLabel Label1;
        private javax.swing.JLabel Label2;
        private javax.swing.JTextField NumberField;
        private javax.swing.JLabel NumberText;
        private javax.swing.JButton SortButton;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextPane sortBox;
        // End of variables declaration
        
    }
    
    
    Code:
     public void sort(int b[]) {
            int temp;
            for (int i = 0; i < b.length - 1; i++) {
                for (int j = i + 1; j < b.length; j++) {
                    if (b[i] > b[j]) {
                        temp = b[i];
                        b[i] = b[j];
                        b[j] = temp;
                    }
                }
    
            }
        }
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    Put the numbers that you get from the screen into an array and call the sort routine.

    You could modify your paint method to take an array parameter to generate the output.
     
  3. elppa macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
  4. letsdiscussit thread starter macrumors newbie

    letsdiscussit

    Joined:
    Jun 26, 2007
    #4
    Thanks Elppa, helped me a lot. I updated my code in the first post
     

Share This Page