Answer to Question #8876 in Java | JSP | JSF for Wilfred

Question #8876
Write an application containing three parallel arrays that hold 10 elements each. The first array holds four digit student ID numbers, the second holds first names and the third holds the students grade point averages. Use dialog boxes to accept a student ID number and display the student's first name and grade point average. If a match is to found, display an error message that includes the invalid ID numbers and allow the user to search for a new ID number. This is for an assignment that is due monday,would appreciate it if you can help me before the due date.
1
Expert's answer
2012-05-08T08:51:30-0400
/*
& * To change this template, choose Tools | Templates
& * and open the template in the editor.
& */
package javaapplication3;

/**
& *
& *
& */
public class BoxJFrame extends javax.swing.JFrame {

/**
& * Creates new form BoxJFrame
& */
private int[] studentsID = {1234, 1111, 2222, 3333, 1234, 1235, 1222, 7777, 4567, 9876};

private String[] studentsName ={"Alan", "Cris", "Joe", "Suzzy", "Stan", "Eminem", "Josh", "Ann", "Katy", "Ralph"};

private double[] studentsGP = {2.8, 4.1, 3.5, 4.0, 3.8, 3.75, 3.0, 4.2, 3.9, 3.6};

public BoxJFrame() {
& 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">//GEN-BEGIN:initComponents
private void initComponents() {

& jScrollPane1 = new javax.swing.JScrollPane();
& jTextPane1 = new javax.swing.JTextPane();
& jLabel1 = new javax.swing.JLabel();
& jScrollPane2 = new javax.swing.JScrollPane();
& jTextPane2 = new javax.swing.JTextPane();
& jButton1 = new javax.swing.JButton();
& jLabel2 = new javax.swing.JLabel();
& jScrollPane3 = new javax.swing.JScrollPane();
& jTextArea1 = new javax.swing.JTextArea();

& jScrollPane1.setViewportView(jTextPane1);

& setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

& jLabel1.setText("Input student ID");

& jScrollPane2.setViewportView(jTextPane2);

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

& jLabel2.setText("Result");

& jTextArea1.setColumns(20);
& jTextArea1.setRows(5);
& jScrollPane3.setViewportView(jTextArea1);

& javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
& getContentPane().setLayout(layout);
& layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
& .addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42)
.addComponent(jButton1))
& .addGroup(layout.createSequentialGroup()
.addGap(226, 226, 226)
.addComponent(jLabel2)))
.addContainerGap(43, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(101, 101, 101))
& );
& layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
& .addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
& .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(51, 51, 51)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(110, Short.MAX_VALUE))
& );

& pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
& // TODO add your handling code here:
& boolean flag = false;
& try{
int num = Integer.parseInt(jTextPane2.getText());
for(int i=0; i<10; i++){
if(studentsID[i]==num){
& flag = true;
& jTextArea1.setText("ID: " + num + "\n"
+ "Name: " + studentsName[i] + "\n"
+ "GPA: " + studentsGP[i]);
& break;
}
}
if(!flag)
& jTextArea1.setText("Wrong ID! Try again!");
& }
& catch(Exception e){
jTextArea1.setText("Error input!");
& }
&
}//GEN-LAST:event_jButton1ActionPerformed

/**
& * @param args the command line arguments
& */
public static void main(String args[]) {
& /*
* Set the Nimbus look and feel
*/
& //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
& /*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* https://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
& 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(BoxJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
& } catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BoxJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
& } catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BoxJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
& } catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BoxJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
& }
& //</editor-fold>

& /*
* Create and display the form
*/
& java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new BoxJFrame().setVisible(true);
}
& });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
// End of variables declaration//GEN-END:variables
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS