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

Why does my toString output this for the Find action? Records.java package Proje

ID: 3582803 • Letter: W

Question

Why does my toString output this for the Find action?

Records.java

package Project4_CMIS242;

import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Records extends javax.swing.JFrame {
  
   // Creating variables
   Student students;
    private javax.swing.JTextField id;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField major;
    private javax.swing.JTextField name;
    @SuppressWarnings("rawtypes")
   private javax.swing.JComboBox operation;
    private javax.swing.JButton process;
    private javax.swing.JFrame frame1;
    private javax.swing.JPanel panel1;
  
    /**
   *
   */
   private static final long serialVersionUID = 1L;
   // hash map record for database
     HashMap<Integer,Student> record=new HashMap<Integer,Student>();
  
    /**
     * Creates new form StudentsRecord
     */
    public Records() {
        initComponents();
    }
  

    @SuppressWarnings({ "unchecked", "rawtypes" })
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        major = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        operation = new javax.swing.JComboBox();
        process = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      
        setTitle("Project 4");
      
        jLabel1.setText("Id: ");

        jLabel2.setText("Name: ");

        jLabel3.setText("Major: ");

        jLabel4.setText("Choose Selection: ");
      


        operation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Insert", "Delete", "Find", "Update" }));

        process.setText("Process Request");
        process.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                processActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(id)
                            .addComponent(name)
                            .addComponent(major)
                            .addComponent(operation, 0, 119, Short.MAX_VALUE))
                        .addGap(48, 48, 48))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(process)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(major, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(process)
                .addContainerGap(27, Short.MAX_VALUE))
        );

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

    private void processActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processActionPerformed
          try
          {
            
              int sid=Integer.parseInt(id.getText());//geting id from text field
              String sName=name.getText();       //getting name from text field
              String sMajor=major.getText();   //gettinn major form text field
            
              if(operation.getSelectedIndex()==0) // index 0 is for insertion
              {
                  if(record.containsKey(sid)) //if reccord already exists then show an error
                  {
                      JOptionPane.showMessageDialog(this,"This ID is already in use.","Error",0);
                  }
                  else
                  {
                     record.put(sid,new Student(sName, sMajor));
                     JOptionPane.showMessageDialog(this,"The record was inserted successfully.","Success",1);
                  }
              }
              else if(operation.getSelectedIndex()==1) // index 1 is for deletion
              {
                   if(record.containsKey(sid)) //if record exist then delete it
                   {
                         record.remove(sid);
                         JOptionPane.showMessageDialog(this,"The record was deleted successfully.","Success",1);
                   }
                   else // else show error
                   {
                         JOptionPane.showMessageDialog(this,"No record exists for this ID","Error",0);
                   }
                
              }
              else if(operation.getSelectedIndex()==2) //index 2 is for find
              {
                  if(record.containsKey(sid)) // if exist show details
                   {
                       
                      
                       Student m = (Student)record.get(id);
                         JOptionPane.showMessageDialog(this,sid,"Success", 1);
                         JOptionPane.showMessageDialog(this,sName,"Success", 1);
                         JOptionPane.showMessageDialog(this,sMajor,"Success", 1);
                         JOptionPane.showMessageDialog(frame1,toString(),"Success", 1);
                   }
                   else
                   {
                         JOptionPane.showMessageDialog(this,"No record exists for this ID","Error",0);
                   }
              }
              else if(operation.getSelectedIndex()==3) //index 3 is for update
              {
                   if(record.containsKey(sid))
                   {
                         Student s=record.get(sid);
                         JFrame frame = new JFrame("Input Dialog : ");
                         final String grades[]={"A","B","C","D","F"};
                         //showing grade input dialog box
                         String sGrade=(String)JOptionPane.showInputDialog(frame,"Choose Grade: ","A",JOptionPane.QUESTION_MESSAGE,null,grades,grades[0]);
                         if(sGrade==null)
                         {
                            JOptionPane.showMessageDialog(this,"Update failed.","Error",0);
                         }
                         else
                         {
                              String credits[]={"1","2","3","4","6"};
                              //credit input dialog box
                              String credit=(String)JOptionPane.showInputDialog(frame,"Choose credits","3",JOptionPane.QUESTION_MESSAGE,null,credits,credits[0]);
                             if(credit==null)
                             {
                                JOptionPane.showMessageDialog(this,"Update failed.","Error",0);
                             }
                             else
                             {
                                 int sCredit=Integer.parseInt(credit);
                                  s.courseCompleted(sGrade.charAt(0), sCredit);
                                  record.replace(sid, s);
                                  JOptionPane.showMessageDialog(this,"The record was updated successfully.","Success",1);
                             }
                             
                         }
                      
                   }
                   else
                   {
                         JOptionPane.showMessageDialog(this,"No record exists for this ID.","Error",0);
                   }
              }
            
          }catch(Exception e)
          {
              JOptionPane.showMessageDialog(this,"The ID must be an integer.","Error",0);
          }
    }//GEN-LAST:event_processActionPerformed


   /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
     
        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(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
      
        catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
      
        catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
      
        catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Records.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 Records().setVisible(true);
            }
        });
    }


}

Student.java

package Project4_CMIS242;


   public class Student {
     
      
        private String name;
        private String major;
        private int totalCredit;
        public double qualityPoint;
      
      

        public Student(String name, String major) {
          
            this.name = name;
            this.major = major;
            this.totalCredit = 0;
            this.qualityPoint = 0;
        }
      
        public void courseCompleted(char grade,int credit) {
          
            this.totalCredit = credit;
            int value=0;
            if(grade=='A') {
                value=10;
            }
          
            else if (grade=='B') {
                value=8;
            }
          
            else if(grade=='C') {
                value=6;
            }
          
            else if(grade=='D') {
                value=4;
            }
          
            else if(grade=='F') {
                value=2;
            }
          
            qualityPoint = value * credit;
         
        }
      
        public double getGPA(double qualityPoint) {
           return this.qualityPoint = qualityPoint;
        }
      
      
      
        public String toString()
        {
          
            double gpa = 0.0;
            if(totalCredit==0)
            {
                gpa= 4.0;
                qualityPoint = gpa;
            }
          
           String str = "Name: " + this.name + "Major: " + this.major + "GPA: " + this.qualityPoint;
          
           return str;
        }
   }

Explanation / Answer

import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Records extends javax.swing.JFrame {
  
// Creating variables
Student students;
private javax.swing.JTextField id;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField major;
private javax.swing.JTextField name;
@SuppressWarnings("rawtypes")
private javax.swing.JComboBox operation;
private javax.swing.JButton process;
private javax.swing.JFrame frame1;
private javax.swing.JPanel panel1;
  
/**
*
*/
private static final long serialVersionUID = 1L;
// hash map record for database
HashMap<Integer,Student> record=new HashMap<Integer,Student>();
  
/**
* Creates new form StudentsRecord
*/
public Records() {
initComponents();
}
  
@SuppressWarnings({ "unchecked", "rawtypes" })
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
id = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
name = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
major = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
operation = new javax.swing.JComboBox();
process = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
setTitle("Project 4");
  
jLabel1.setText("Id: ");
jLabel2.setText("Name: ");
jLabel3.setText("Major: ");
jLabel4.setText("Choose Selection: ");
  

operation.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Insert", "Delete", "Find", "Update" }));
process.setText("Process Request");
process.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
processActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(id)
.addComponent(name)
.addComponent(major)
.addComponent(operation, 0, 119, Short.MAX_VALUE))
.addGap(48, 48, 48))
.addGroup(layout.createSequentialGroup()
.addComponent(process)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(major, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(process)
.addContainerGap(27, Short.MAX_VALUE))
);
pack();
  
}// </editor-fold>//GEN-END:initComponents
private void processActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processActionPerformed
try
{
  
int sid=Integer.parseInt(id.getText());//geting id from text field
String sName=name.getText(); //getting name from text field
String sMajor=major.getText(); //gettinn major form text field
  
if(operation.getSelectedIndex()==0) // index 0 is for insertion
{
if(record.containsKey(sid)) //if reccord already exists then show an error
{
JOptionPane.showMessageDialog(this,"This ID is already in use.","Error",0);
}
else
{
record.put(sid,new Student(sName, sMajor));
JOptionPane.showMessageDialog(this,"The record was inserted successfully.","Success",1);
}
}
else if(operation.getSelectedIndex()==1) // index 1 is for deletion
{
if(record.containsKey(sid)) //if record exist then delete it
{
record.remove(sid);
JOptionPane.showMessageDialog(this,"The record was deleted successfully.","Success",1);
}
else // else show error
{
JOptionPane.showMessageDialog(this,"No record exists for this ID","Error",0);
}
  
}
else if(operation.getSelectedIndex()==2) //index 2 is for find
{
if(record.containsKey(sid)) // if exist show details
{
Student m = (Student)record.get(Integer.parseInt(id.getText()));
JOptionPane.showMessageDialog(frame1,m.toString(),"Success", 1);

}
else
{
JOptionPane.showMessageDialog(this,"No record exists for this ID","Error",0);
}
}
else if(operation.getSelectedIndex()==3) //index 3 is for update
{
if(record.containsKey(sid))
{
Student s=record.get(sid);
JFrame frame = new JFrame("Input Dialog : ");
final String grades[]={"A","B","C","D","F"};
//showing grade input dialog box
String sGrade=(String)JOptionPane.showInputDialog(frame,"Choose Grade: ","A",JOptionPane.QUESTION_MESSAGE,null,grades,grades[0]);
if(sGrade==null)
{
JOptionPane.showMessageDialog(this,"Update failed.","Error",0);
}
else
{
String credits[]={"1","2","3","4","6"};
//credit input dialog box
String credit=(String)JOptionPane.showInputDialog(frame,"Choose credits","3",JOptionPane.QUESTION_MESSAGE,null,credits,credits[0]);
if(credit==null)
{
JOptionPane.showMessageDialog(this,"Update failed.","Error",0);
}
else
{
int sCredit=Integer.parseInt(credit);
s.courseCompleted(sGrade.charAt(0), sCredit);
record.remove(sid);
record.put(sid, s);
JOptionPane.showMessageDialog(this,"The record was updated successfully.","Success",1);
}

}
  
}
else
{
JOptionPane.showMessageDialog(this,"No record exists for this ID.","Error",0);
}
}
  
}catch(Exception e)
{
JOptionPane.showMessageDialog(this,"The ID must be an integer.","Error",0);
}
}//GEN-LAST:event_processActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

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(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
  
catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
  
catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Records.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
  
catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Records.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 Records().setVisible(true);
}
});
}

}

---------------------------------------------------------------------------------------------------------------------------------------------------

public class Student {

  
private String name;
private String major;
private int totalCredit;
public double qualityPoint;
  
  
public Student(String name, String major) {
  
this.name = name;
this.major = major;
this.totalCredit = 0;
this.qualityPoint = 0;
}
  
public void courseCompleted(char grade,int credit) {
  
this.totalCredit = credit;
int value=0;
if(grade=='A') {
value=10;
}
  
else if (grade=='B') {
value=8;
}
  
else if(grade=='C') {
value=6;
}
  
else if(grade=='D') {
value=4;
}
  
else if(grade=='F') {
value=2;
}
  
qualityPoint = value * credit;

}
  
public double getGPA(double qualityPoint) {
return this.qualityPoint = qualityPoint;
}
  
  
  
public String toString()
{
  
double gpa = 0.0;
if(totalCredit==0)
{
gpa= 4.0;
qualityPoint = gpa;
}
  
String str = "Name: " + this.name + " Major: " + this.major + " GPA: " + this.qualityPoint;
  
return str;
}
}

[NOTE: The changed areas have been marked in bold. please check it out]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote