# Easy calculate coding needed.

• April 25th, 2012, 11:36 AM
mic2bless
Easy calculate coding needed.
This is the second part to my problem. i already have a MYutils to use .....i just need to know how to cod ethe calculate button. This is all on one form. If you have netbeans i have attatched what i have so far in a zip file. PLEASE HELP CODE THE Excercise 2 CALCULATE BUTTON. THANK YOU

. Implement a method called calcDiscount() that will accept the sales amount as a parameter and return the discount based on the following table values. Challenge option: Instead of using an if/else if selection structure, use parallel arrays with the amount and discount.
Amount
Discount
< \$100
0
100 - 199
2%
200 - 499
5%
500 - 999
7%
\$1000 or more
10%
4. Commissions are calculated based on the sales amount after any discount. Implement a method called calcCommission() that will accept the sales amount (after discount) as a parameter and return the commission based on the following table values. Challenge option: Instead of using an if/else if selection structure, use parallel arrays with the amount and discount.
Amount
Commission
< \$200
2%
200 - 499
5%
500 - 999
7%
\$1000 or more
10%
5. Implement a method called calcSalesTax() that will accept the sales amount (after discount) as a parameter and return the sales tax amount based on a 7.5% tax rate.

6. Implement a method called calcFinalPrice that will accept the sales amount, discount amount and tax amount as parameters and return the total price. [Total Price] = [Sales Amount] – [Discount] + [Sales Tax]. You will need to format the number to 2 decimal places and with a leading dollar sign (I.e., \$300.00). See the Formatting Numbers in Java handout.

7. Test your code with the following amounts. Verify the correct discount, commission, sales tax and final price.
Sales Amount
Discount
Commission
Sales Tax
Total Price
99
0
1.98
7.43
106.43
199
3.98
3.90
14.63
209.65
499
24.95
23.70
35.55
509.60
999
69.93
65.03
69.68
998.75
1000
100
63
67.5
967.50
• April 25th, 2012, 11:46 AM
Norm
Re: Easy calculate coding needed.
• April 26th, 2012, 11:42 PM
mic2bless
Re: Easy calculate coding needed.
here is what i have so far. i need to code the btncalculate2.....towards the end of the coding with i would guess an IF/ELSE.....for the problem above. to put in a sales amount and get the discount tax commision and so on....

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* Lab5Form.java
*
* Created on Mar 27, 2012, 8:02:27 PM
*/
package lab5;

import javax.swing.JLabel;
/**
*
* @author michael.lawrence
*/
public class Lab5Form extends javax.swing.JFrame {

/** Creates new form Lab5Form */
public Lab5Form() {
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() {

jTabbedPane1 = new javax.swing.JTabbedPane();
Panel1 = new javax.swing.JPanel();
txtAmount = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
lbl10 = new javax.swing.JLabel();
lbl5 = new javax.swing.JLabel();
lbl1 = new javax.swing.JLabel();
btnCalculate = new javax.swing.JButton();
lbl20 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
lblDiscount = new javax.swing.JLabel();
lblCommission = new javax.swing.JLabel();
lblSalesTax = new javax.swing.JLabel();
lblTotal = new javax.swing.JLabel();
btnCalculate2 = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

Panel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

txtAmount.setBorder(javax.swing.BorderFactory.crea teTitledBorder("Enter Amount"));
Panel1.add(txtAmount, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 44, 112, -1));
Panel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 116, -1, -1));

lbl10.setBorder(javax.swing.BorderFactory.createTi tledBorder("10's"));
Panel1.add(lbl10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 60, 40));

lbl5.setBorder(javax.swing.BorderFactory.createTit ledBorder("5's"));
Panel1.add(lbl5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, 60, 40));

lbl1.setBorder(javax.swing.BorderFactory.createTit ledBorder("1's"));
Panel1.add(lbl1, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 270, 50, -1));

btnCalculate.setText("Calculate");
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateActionPerformed(evt);
}
});
Panel1.add(btnCalculate, new org.netbeans.lib.awtextra.AbsoluteConstraints(201, 62, -1, -1));

lbl20.setBorder(javax.swing.BorderFactory.createTi tledBorder("20's"));
Panel1.add(lbl20, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 50, 40));

jTextField1.setBorder(javax.swing.BorderFactory.cr eateTitledBorder("Sales Amount"));

lblDiscount.setBorder(javax.swing.BorderFactory.cr eateTitledBorder("Discount"));

lblCommission.setBorder(javax.swing.BorderFactory. createTitledBorder("Commission"));

lblSalesTax.setBorder(javax.swing.BorderFactory.cr eateTitledBorder("Sales Tax"));

lblTotal.setBorder(javax.swing.BorderFactory.creat eTitledBorder("Total"));

btnCalculate2.setText("Calculate");
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculate2ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
);
jPanel2Layout.setVerticalGroup(
);

btnExit.setText("Exit");

jLabel1.setText("Michael Lawrence Lab 5 ex. 1");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
);
layout.setVerticalGroup(
);

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

private void btnCalculateActionPerformed(java.awt.event.ActionE vent evt) {
// local variables
int amount = 0;
int i = 0;
final int denoms[] = {20, 10, 5, 1};
int values[] = {0, 0, 0, 0};
JLabel labels[] = {lbl20, lbl10, lbl5, lbl1};

//input -get amount

amount = MyUtils.tryParseInt(txtAmount.getText(), 0);

// process - calculate 20 10 5 and 1's
for (i = 0; i < denoms.length; i++){
values [i] = amount / denoms[i];
amount = amount - (values[i] * denoms[i]);

//output - display the value
labels[i].setText(Integer.toString(values[i]));

} // end for

}

private void btnCalculate2ActionPerformed(java.awt.event.Action Event evt){
[/B]int String =

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Lab5Form().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel Panel1;
private javax.swing.JButton btnCalculate;
private javax.swing.JButton btnCalculate2;
private javax.swing.JButton btnExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel lbl1;
private javax.swing.JLabel lbl10;
private javax.swing.JLabel lbl20;
private javax.swing.JLabel lbl5;
private javax.swing.JLabel lblCommission;
private javax.swing.JLabel lblDiscount;
private javax.swing.JLabel lblSalesTax;
private javax.swing.JLabel lblTotal;
private javax.swing.JTextField txtAmount;
// End of variables declaration
}
• April 27th, 2012, 05:43 AM
Norm
Re: Easy calculate coding needed.

What does calculations do you need to do in the button's listener?
Can you describe the logic that you need to code to do the calc? Make a list of the steps the code needs to do.
• May 1st, 2012, 10:29 PM
mic2bless
Re: Easy calculate coding needed.
Code java:

```< private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) { // local variables int amount = 0; int i = 0; final int denoms[] = {20, 10, 5, 1}; int values[] = {0, 0, 0, 0}; JLabel labels[] = {lbl20, lbl10, lbl5, lbl1};   //input -get amount   amount = MyUtils.tryParseInt(txtAmount.getText(), 0);     // process - calculate 20 10 5 and 1's for (i = 0; i < denoms.length; i++){ values [i] = amount / denoms[i]; amount = amount - (values[i] * denoms[i]);     //output - display the value labels[i].setText(Integer.toString(values[i]));     } // end for       }   private void btnCalculate2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here:           }   /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() {   public void run() { new Lab5Form().setVisible(true); } >```

i need the button to to calculate out the discount, commision, sales tax, and total when i put in an amount and press the calculate button. the problem is in my first post. here is some of the code. I coded one calculate button for another excercise on there and now i need to do the 2nd excercise
• May 2nd, 2012, 06:18 AM
Norm
Re: Easy calculate coding needed.
Is the action listener method called when the button is pressed?
If it is then add the code to it to calculate the values you want:
discount = <formula to calculate discount>;
commission = <forumula to calculate commission>;
etc