Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Easy calculate coding needed.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Easy calculate coding needed.

    Please post the code you are having problems with and ask your questions about the posted code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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");
    btnCalculate.addActionListener(new java.awt.event.ActionListener() {
    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));

    jTabbedPane1.addTab("Exercise 1", Panel1);

    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");
    btnCalculate2.addActionListener(new java.awt.event.ActionListener() {
    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.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(25, 25, 25)
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING, false)
    .addComponent(lblDiscount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
    .addComponent(lblCommission, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(lblTotal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(lblSalesTax, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
    .addGap(162, 162, 162)
    .addComponent(btnCalculate2)))
    .addGap(105, 105, 105))
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(31, 31, 31)
    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(lblDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(lblCommission, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(lblSalesTax, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(113, 113, 113)
    .addComponent(btnCalculate2)))
    .addContainerGap(115, Short.MAX_VALUE))
    );

    jTabbedPane1.addTab("Exercise 2", jPanel2);

    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.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addGap(36, 36, 36)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 90, Short.MAX_VALUE)
    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(101, 101, 101))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addGap(20, 20, 20)
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(28, 28, 28)
    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 418, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(btnExit)
    .addComponent(jLabel1))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );

    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){
    // TODO add your handling code here:
    [/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
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Easy calculate coding needed.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Easy calculate coding needed.

    <        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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  2. Help needed with java coding!!
    By Bentino in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2012, 08:09 PM
  3. calculate sum and product
    By swampfox in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 27th, 2011, 03:55 PM
  4. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  5. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM