Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darine32
    New Member
    • Jul 2006
    • 3

    #1

    Help

    I have a program I am writing for class. Now I have the program virtually done I am having some issues on the calculation. Any guidance would be appreciated:

    public class Commission
    {
    public static void main(String args[])
    {
    int salesp[];
    salesp= new int [15];
    int salespw[]={500,1000,350, 625,5000,200,28 0,230,290,376,6 10,2000,350,100 ,150};
    double commision=.09;
    double total;
    {

    System.out.prin tf( "%5s%8s\n", "Salesper", "Sales per Week" );

    {
    for(int counter=0;count er < salespw.length; counter++)
    System.out.prin tf("%5d%8d\n", counter, salespw[ counter ]);
    }

    {
    for ( int counter = 0; counter < salespw.length; counter++ )
    total += salespw[ counter ]*commission;
    System.out.prin tln("%5d%8d\n", 'Your commission is: ',commission);
    }
    }

    }

    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    First, you should use [ code ] (without spaces) because it preserves the whitespace, making it easier to read.

    Second, the 12th entry is "20 00" instead of "2000"

    Also, your brackets may be misplaced. It should still work because you only have one instruction nested below the for loop. It should really be
    Code:
    for (condition)
    {
      stmt1;
      stmt2;
    }
    What result are you getting? What do you think it should be? How far are you off? I think the result should be 1085.49. You should add the total up first, then multiply by the commision rate once you have summed all the numbers. It has to do with infinitely repeating fractions, and losing accuracy.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Additionally you create the total using the operator += but you never initialise total to 0 first, swhen you output the calculation result you are outputing commission but not total.

      Comment

      • darine32
        New Member
        • Jul 2006
        • 3

        #4
        Originally posted by D_C
        First, you should use [ code ] (without spaces) because it preserves the whitespace, making it easier to read.

        Second, the 12th entry is "20 00" instead of "2000"

        Also, your brackets may be misplaced. It should still work because you only have one instruction nested below the for loop. It should really be
        Code:
        for (condition)
        {
          stmt1;
          stmt2;
        }
        What result are you getting? What do you think it should be? How far are you off? I think the result should be 1085.49. You should add the total up first, then multiply by the commision rate once you have summed all the numbers. It has to do with infinitely repeating fractions, and losing accuracy.

        The result I am trying to get is the commision and I am trying to do a ratio of the number of comissions ranging from 200 - 1000 dollars. I forgot to set total to 0 seems to be my ultimate mistake programming in Java

        Comment

        Working...