Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Is it possible to convert the string below into a double so that the code below does the exact same thing but without using DecimalFormat?

Is it possible to convert the string below into a double so that the code below does the exact same thing but without using DecimalFormat?

 private void studentAvgBtnActionPerformed(java.awt.event.ActionEvent evt) { String firstName = firstInput.getText(); String lastName = lastInput.getText(); String testOne = oneField.getText(); String testTwo = twoField.getText(); String testThree = threeField.getText(); String testFour = fourField.getText(); DecimalFormat dec = new DecimalFormat("0.00"); String avg = dec.format((Integer.parseInt(testOne)+Integer.parseInt(testTwo)+Integer.parseInt(testThree)+Integer.parseInt(testFour)) / 4.00); studentGrades.get(studentGrades.size()-1).add(avg); outputArea.setText("First NametLast NametTest 1tTest 2tTest 3tTest 4tStudent Average "); for(int row = 0; row  tempRow = studentGrades.get(row); outputArea.append(tempRow.get(0)+"t"); outputArea.append(tempRow.get(1)+"t"); outputArea.append(tempRow.get(2)+"t"); outputArea.append(tempRow.get(3)+"t"); outputArea.append(tempRow.get(4)+"t"); outputArea.append(tempRow.get(5)+"t"); outputArea.append(tempRow.get(6)+"t"); } }

Full code below:

/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ package pkg2d.assignment; import java.util.ArrayList; import javax.swing.JOptionPane; import java.text.DecimalFormat; /** * * @author sprince */ public class StudentList extends javax.swing.JFrame { ArrayList studentGrades = new ArrayList(); /** * Creates new form StudentList */ public StudentList() { 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") //  private void initComponents() { jSpinner1 = new javax.swing.JSpinner(); jScrollPane1 = new javax.swing.JScrollPane(); outputArea = new javax.swing.JTextArea(); titleLb = new javax.swing.JLabel(); firstNameLb = new javax.swing.JLabel(); lastNameLb = new javax.swing.JLabel(); testOneLb = new javax.swing.JLabel(); testTwoLb = new javax.swing.JLabel(); testThreeLb = new javax.swing.JLabel(); testFourLb = new javax.swing.JLabel(); firstInput = new javax.swing.JTextField(); lastInput = new javax.swing.JTextField(); oneField = new javax.swing.JTextField(); twoField = new javax.swing.JTextField(); threeField = new javax.swing.JTextField(); fourField = new javax.swing.JTextField(); addBtn = new javax.swing.JButton(); listBtn = new javax.swing.JButton(); studentAvgBtn = new javax.swing.JButton(); courseAvgBtn = new javax.swing.JButton(); exitBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); outputArea.setColumns(20); outputArea.setRows(5); jScrollPane1.setViewportView(outputArea); titleLb.setBackground(new java.awt.Color(240, 240, 250)); titleLb.setFont(new java.awt.Font("Consolas", 3, 36)); // NOI18N titleLb.setText("Student Grades"); firstNameLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N firstNameLb.setText("First Name:"); lastNameLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N lastNameLb.setText("Last Name:"); testOneLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N testOneLb.setText("Test 1:"); testTwoLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N testTwoLb.setText("Test 2:"); testThreeLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N testThreeLb.setText("Test 3:"); testFourLb.setFont(new java.awt.Font("Consolas", 2, 24)); // NOI18N testFourLb.setText("Test 4:"); firstInput.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N lastInput.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N oneField.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N twoField.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N threeField.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N fourField.setFont(new java.awt.Font("Consolas", 0, 18)); // NOI18N addBtn.setFont(new java.awt.Font("Consolas", 2, 18)); // NOI18N addBtn.setText("Add"); addBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addBtnActionPerformed(evt); } }); listBtn.setFont(new java.awt.Font("Consolas", 2, 18)); // NOI18N listBtn.setText("List"); listBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { listBtnActionPerformed(evt); } }); studentAvgBtn.setFont(new java.awt.Font("Consolas", 2, 18)); // NOI18N studentAvgBtn.setText("Student Average"); studentAvgBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { studentAvgBtnActionPerformed(evt); } }); courseAvgBtn.setFont(new java.awt.Font("Consolas", 2, 18)); // NOI18N courseAvgBtn.setText("Course Average"); courseAvgBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { courseAvgBtnActionPerformed(evt); } }); exitBtn.setFont(new java.awt.Font("Consolas", 2, 18)); // NOI18N exitBtn.setText("Exit"); exitBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitBtnActionPerformed(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() .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(testOneLb) .addComponent(testTwoLb) .addComponent(testThreeLb) .addComponent(testFourLb)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(273, 273, 273) .addComponent(exitBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(271, 271, 271) .addComponent(courseAvgBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(oneField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(lastNameLb) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lastInput, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(firstNameLb) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(firstInput, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(twoField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(threeField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fourField, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(37, 37, 37) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(studentAvgBtn) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(listBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE) .addComponent(addBtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addComponent(jScrollPane1)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 165, Short.MAX_VALUE) .addComponent(titleLb) .addGap(165, 165, 165)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(15, 15, 15) .addComponent(titleLb) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(firstNameLb) .addComponent(firstInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(addBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lastNameLb) .addComponent(lastInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(listBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(testOneLb) .addComponent(oneField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(studentAvgBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(testTwoLb) .addComponent(twoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(courseAvgBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(testThreeLb) .addComponent(threeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(testFourLb) .addComponent(fourField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(exitBtn)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }//  private void courseAvgBtnActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //Will need to add begining of code outputArea.setText("First NametLast NametTest 1tTest 2tTest 3tTest 4tStudent AveragetCourse Average "); for(int row = 0; row  tempRow = studentGrades.get(row); outputArea.append(tempRow.get(0)+"t"); outputArea.append(tempRow.get(1)+"t"); outputArea.append(tempRow.get(2)+"t"); outputArea.append(tempRow.get(3)+"t"); outputArea.append(tempRow.get(4)+"t"); outputArea.append(tempRow.get(5)+"t"); outputArea.append(tempRow.get(6)+"t"); outputArea.append(tempRow.get(7)+"t"); } } private void addBtnActionPerformed(java.awt.event.ActionEvent evt) { String firstName = firstInput.getText(); String lastName = lastInput.getText(); String testOne = oneField.getText(); String testTwo = twoField.getText(); String testThree = threeField.getText(); String testFour = fourField.getText(); if (firstName.equals("")){ JOptionPane.showMessageDialog(rootPane, "No First Name Entered");} else if (lastName.equals("")){ JOptionPane.showMessageDialog(rootPane, "No Last Name Entered");} else if (testOne.equals("")){ JOptionPane.showMessageDialog(rootPane, "No First Test Grade Entered");} else if (testTwo.equals("")){ JOptionPane.showMessageDialog(rootPane, "No Second Test Grade Entered");} else if (testThree.equals("")){ JOptionPane.showMessageDialog(rootPane, "No Third Test Grade Entered");} else if (testFour.equals("")){ JOptionPane.showMessageDialog(rootPane, "No Fourth Test Grade Entered");} else{ ArrayList tempRow = new ArrayList(); tempRow.add(firstName); tempRow.add(lastName); tempRow.add(testOne); tempRow.add(testTwo); tempRow.add(testThree); tempRow.add(testFour); studentGrades.add(tempRow); outputArea.append("Added Successfully!");} } private void listBtnActionPerformed(java.awt.event.ActionEvent evt) { outputArea.setText("First NametLast NametTest 1tTest 2tTest 3tTest 4 "); for(int row = 0; row  tempRow = studentGrades.get(row); outputArea.append(tempRow.get(0)+"t"); outputArea.append(tempRow.get(1)+"t"); outputArea.append(tempRow.get(2)+"t"); outputArea.append(tempRow.get(3)+"t"); outputArea.append(tempRow.get(4)+"t"); outputArea.append(tempRow.get(5)+"t"); } } private void studentAvgBtnActionPerformed(java.awt.event.ActionEvent evt) { String firstName = firstInput.getText(); String lastName = lastInput.getText(); String testOne = oneField.getText(); String testTwo = twoField.getText(); String testThree = threeField.getText(); String testFour = fourField.getText(); DecimalFormat dec = new DecimalFormat("0.00"); String avg = dec.format((Integer.parseInt(testOne)+Integer.parseInt(testTwo)+Integer.parseInt(testThree)+Integer.parseInt(testFour)) / 4.00); studentGrades.get(studentGrades.size()-1).add(avg); outputArea.setText("First NametLast NametTest 1tTest 2tTest 3tTest 4tStudent Average "); for(int row = 0; row  tempRow = studentGrades.get(row); outputArea.append(tempRow.get(0)+"t"); outputArea.append(tempRow.get(1)+"t"); outputArea.append(tempRow.get(2)+"t"); outputArea.append(tempRow.get(3)+"t"); outputArea.append(tempRow.get(4)+"t"); outputArea.append(tempRow.get(5)+"t"); outputArea.append(tempRow.get(6)+"t"); } } private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://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(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(StudentList.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 StudentList().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton addBtn; private javax.swing.JButton courseAvgBtn; private javax.swing.JButton exitBtn; private javax.swing.JTextField firstInput; private javax.swing.JLabel firstNameLb; private javax.swing.JTextField fourField; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSpinner jSpinner1; private javax.swing.JTextField lastInput; private javax.swing.JLabel lastNameLb; private javax.swing.JButton listBtn; private javax.swing.JTextField oneField; private javax.swing.JTextArea outputArea; private javax.swing.JButton studentAvgBtn; private javax.swing.JLabel testFourLb; private javax.swing.JLabel testOneLb; private javax.swing.JLabel testThreeLb; private javax.swing.JLabel testTwoLb; private javax.swing.JTextField threeField; private javax.swing.JLabel titleLb; private javax.swing.JTextField twoField; // End of variables declaration }

Step by Step Solution

3.45 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

Yes its possible to achieve the same functionality without using DecimalFormat Heres the modified co... 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Elements Of Chemical Reaction Engineering

Authors: H. Fogler

6th Edition

013548622X, 978-0135486221

More Books

Students also viewed these Programming questions

Question

What are the ethical issues involved in NOAC's proposal?

Answered: 1 week ago

Question

Define a traverse in Surveying?

Answered: 1 week ago