My code for a calculator that calculates the number of hours a person has spent sleeping from
Fantastic news! We've Found the answer you've been seeking!
Question:
My code for a calculator that calculates the number of hours a person has spent sleeping from the time they was born until today is not working. This is assuming that everyone sleeps 8 hours every night, there are 30 days in each month, and there are 365 days in each year. Can someone help me with the code?
package sleep; /** * * @author sprince */ public class Calc extends javax.swing.JFrame { /** * Creates new form Calc */ public Calc() { 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() { titleLbl = new javax.swing.JLabel(); enterBirthLbl = new javax.swing.JLabel(); enterTodayLbl = new javax.swing.JLabel(); birthDayField = new javax.swing.JTextField(); bDate = new javax.swing.JLabel(); bMonth = new javax.swing.JLabel(); bYear = new javax.swing.JLabel(); toDate = new javax.swing.JLabel(); toMonth = new javax.swing.JLabel(); toYear = new javax.swing.JLabel(); birthMonthField = new javax.swing.JTextField(); birthYearField = new javax.swing.JTextField(); toDayField = new javax.swing.JTextField(); toYearField = new javax.swing.JTextField(); toMonthField = new javax.swing.JTextField(); exitBtn = new javax.swing.JButton(); calcBtn = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); outputArea = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); titleLbl.setFont(new java.awt.Font("Sitka Small", 2, 24)); // NOI18N titleLbl.setText("Sleep Calculator"); enterBirthLbl.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N enterBirthLbl.setText("Enter Date of Birth"); enterTodayLbl.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N enterTodayLbl.setText("Enter Today's Date"); birthDayField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N birthDayField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { birthDayFieldActionPerformed(evt); } }); bDate.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N bDate.setText("Date"); bMonth.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N bMonth.setText("Month"); bYear.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N bYear.setText("Year"); toDate.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N toDate.setText("Date"); toMonth.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N toMonth.setText("Month"); toYear.setFont(new java.awt.Font("Sitka Small", 0, 12)); // NOI18N toYear.setText("Year"); birthMonthField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N birthMonthField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { birthMonthFieldActionPerformed(evt); } }); birthYearField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N birthYearField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { birthYearFieldActionPerformed(evt); } }); toDayField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N toDayField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toDayFieldActionPerformed(evt); } }); toYearField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N toYearField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toYearFieldActionPerformed(evt); } }); toMonthField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N toMonthField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { toMonthFieldActionPerformed(evt); } }); exitBtn.setFont(new java.awt.Font("Sitka Small", 0, 14)); // NOI18N exitBtn.setText("Exit"); exitBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitBtnActionPerformed(evt); } }); calcBtn.setFont(new java.awt.Font("Sitka Small", 0, 14)); // NOI18N calcBtn.setText("Calculate"); calcBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { calcBtnActionPerformed(evt); } }); outputArea.setColumns(20); outputArea.setRows(5); jScrollPane1.setViewportView(outputArea); 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(104, 104, 104) .addComponent(titleLbl) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(55, 55, 55) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(bDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(birthDayField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(enterBirthLbl) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(bYear, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(bMonth, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(birthMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(birthYearField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(calcBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(enterTodayLbl) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(toDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(14, 14, 14)) .addComponent(toYear, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(toMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(toDayField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(toYearField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(toMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addComponent(exitBtn, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(56, 56, 56)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(titleLbl) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(enterTodayLbl) .addComponent(enterBirthLbl)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(bDate) .addComponent(birthDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(4, 4, 4) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(bMonth) .addComponent(birthMonthField, 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(birthYearField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(bYear))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(toDayField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(toDate)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(toMonth) .addComponent(toMonthField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(6, 6, 6) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(toYear) .addComponent(toYearField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(exitBtn) .addComponent(calcBtn)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// private void birthDayFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void birthMonthFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String[] months; months[ 0 ] = null; months[ January ] = "1"; months[ February ] = "2"; months[ March ] = "3"; months[ April ] = "4"; months[ May ] = "5"; months[ June ] = "6"; months[ July ] = "7"; months[ August ] = "8"; months[ September ] = "9"; months[ October ] = "10"; months[ November ] = "11"; months[ December ] = "12"; } private void birthYearFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void toDayFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void toYearFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void toMonthFieldActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String[] months; months[ 0 ] = null; months[ January ] = "1"; months[ February ] = "2"; months[ March ] = "3"; months[ April ] = "4"; months[ May ] = "5"; months[ June ] = "6"; months[ July ] = "7"; months[ August ] = "8"; months[ September ] = "9"; months[ October ] = "10"; months[ November ] = "11"; months[ December ] = "12"; } private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void calcBtnActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: enterBirthLbl = (months - 1) *30 + bDate (365 - enterBirthLbl); enterTodayLbl = (months - 1) *30 + toDayField; calcBtn = (toYearField - birthYearField)*365 (enterBirthLbl + enterTodayLbl + calcBtn)/8; } int bDate=Integer.parseInt(birthDayField.getText()); int bMonth=Integer.parseInt(birthMonthField.getText()); int bYear=Integer.parseInt(birthYearField.getText()); outputArea.append("You have slept a total of" + calcBtn + "in your life so far"); } /** * @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(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Calc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Calc.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 Calc().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel bDate; private javax.swing.JLabel bMonth; private javax.swing.JLabel bYear; private javax.swing.JTextField birthDayField; private javax.swing.JTextField birthMonthField; private javax.swing.JTextField birthYearField; private javax.swing.JButton calcBtn; private javax.swing.JLabel enterBirthLbl; private javax.swing.JLabel enterTodayLbl; private javax.swing.JButton exitBtn; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea outputArea; private javax.swing.JLabel titleLbl; private javax.swing.JLabel toDate; private javax.swing.JTextField toDayField; private javax.swing.JLabel toMonth; private javax.swing.JTextField toMonthField; private javax.swing.JLabel toYear; private javax.swing.JTextField toYearField; // End of variables declaration }
Related Book For
Understanding Business Ethics
ISBN: 9781506303239
3rd Edition
Authors: Peter A. Stanwick, Sarah D. Stanwick
Posted Date: