Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Assignment IT145 USING NetBeans IDE 8.2 1. Develop an application with a

ID: 3817796 • Letter: P

Question

Project Assignment IT145 USING NetBeans IDE 8.2

1. Develop an application with at least three classes (one of these will contain the main method; the other two will have constructors). (15pts)

2. Within the classes your application must contain the following logic structures:

a) One of the constructors must be overloaded (different method signatures). (10pts)

b) At least one void method. (10pts)

c) At least one methods that returns a value. (10pts)

d) One repetition statement (While, Do or For loop). (10pts)

e) One decision structure (If/Else or Switch/Case). (10pts)

3. Your application must use GUI elements from Chapter 14 including at least two of the following buttons (with handlers), labels, textfields, checkboxes or a button group. (10pts)

4. You must include at least one use of JOptionPane for interactive dialog with users. (10pts)

5. All good programming techniques applied such as comments and white space. (10pts)

6. Compiles error free and is free of any errors (5pts).

Explanation / Answer

import javax.swing.JOptionPane;

/**
*
* @author Sam
*/
public class TestGuiClass extends javax.swing.JFrame {

    /**
     * Creates new form TestGuiClass
     */
    public TestGuiClass() {
        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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setText("--ENTER A DATE--");

        jLabel1.setText("NO DATE ENTERED");

        jLabel2.setText("Enter a valid date in ddmmyyyy format or dd/mm/yyyy format");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 40, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(43, 43, 43))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(55, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        DateChecker dc;
        try { //try to catch if the date entered is valid format
            try { //this try checks if the enter string can be converted to long
                long longDate = Long.parseLong(jTextField1.getText());
                dc = new DateChecker(longDate);
            } catch (Exception e) {
                dc = new DateChecker(jTextField1.getText());
            }

            jLabel1.setText(dc.toString());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "The date you entered doesnt seem to be valid!");
        }
    }                                      

    // Variables declaration - do not modify                   
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                 
}

class DateChecker { //class to convert date to format dd MMM yyyy

    int day, month, year;

    public DateChecker(String date) { //initialize from string
        setDay(date);
        setMonth(date);
        setYear(date);
    }

    public DateChecker(long date) { //inititalize from long
        year = (int) (date % 10000);
        date /= 10000;
        month = (int) (date % 100);
        day = (int) (date / 100);
    }

    private void setDay(String date) { //set day from string
        day = Integer.parseInt(date.substring(0, date.indexOf('/')));
        if (day > 31 || day < 1)
            throw new IllegalArgumentException();
    }

    private void setMonth(String date) { //set month from string
        month = Integer.parseInt(date.substring(date.indexOf('/') + 1, date.lastIndexOf('/')));
        if (month > 12 || month < 1)
            throw new IllegalArgumentException();
    }

    private void setYear(String date) { //set year from string
        year = Integer.parseInt(date.substring(date.lastIndexOf('/') + 1));
        if (year > 2050 || year < 1500)
            throw new IllegalArgumentException();
    }

    @Override
    public String toString() { //to convert into string format
        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        return day + " " + months[month - 1] + " " + year;
    }
}

class TestGuiDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestGuiClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestGuiClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestGuiClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestGuiClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestGuiClass().setVisible(true);
            }
        });
    }
}

Aaah... It took more time to think of a problem than to code the solution. This program takes an input from the user which is a date in format dd/mm/yyyy or in format ddmmyyyy e.g. 15/04/2017 and 15042017. It then converts the date to format dd MMM yyyy format. e.g. 15 Apr 2017.

This code is just for demonstration and has few points of failure and not suitable for commercial use. But am surre this will serve your problem. I have also added comments where needed. If you are still having problem the code, please get back to me. :)
I shall try my best to help you