Is this mathematically possible? Java code for interest rate...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    Is this mathematically possible? Java code for interest rate...

    Greetings and salutations!

    Hope it's a good week thus far...

    Got myself thinking and wanted to see what can be achieved with a certain bit of code:

    [CODE=JAVA]

    public double getPayment( ) {
    double mPayment;

    mPayment = (loanAmount * interestRate)
    /
    (1 - Math.pow(1/(1 + interestRate),
    numPayments ) );
    return mPayment;
    }

    [/CODE]

    This calculates interest rate for a certain loan amount, per mothly payments. Question is can we turn around and doctor this up to calculate weekly payments as in instead of using it as interest rate, it is used as commission rate whereby the end result is a weekly reward on one's check for a certain amount of sales.

    This is homework so I am not expecting the year book answer on this one, just a simple direction will do, surely can read between the lines;-)

    I am almost satisfied with it thus far, and in due time I'll nail it right. I have since removed the code above and wanted to reinstate it only if in fact it is valuable, otherwise, will keep coding...

    Perhaps this may me be of aid if you can tell me, here is an excerpt of the code above:

    [CODE=JAVA]
    what is 1 in:

    (1 - Math.pow

    what is 1 in:

    pow(1/

    what is 1 in:


    (1 + interestRate

    [/CODE]

    I think if I can at least figure that out, I'm on my way. What are you thoughts? Can this be done?

    Thanks for your help;-)

    Dököll
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Aren't you just asking how to solve an equation for a certain variable? That's not particularly a Java question is it?

    Comment

    • Dököll
      Recognized Expert Top Contributor
      • Nov 2006
      • 2379

      #3
      Originally posted by BigDaddyLH
      Aren't you just asking how to solve an equation for a certain variable? That's not particularly a Java question is it?
      Hey BigDaddyLH!

      Thank you for coming to aid. It is a Java question, above is part of the code that caculates interest rate with monthly payments. I actually have more calculations I may need help with including:

      [CODE=JAVA]

      public void setRate(double annualRate) {
      interestRate = annualRate / 100.0 / MONTHS_IN_YEAR;
      }

      //above sets the inerets rate, in my code it's the commission rate...

      [/CODE]

      For something like that I would need to know what the 100.0 represents. I have already converted MONTHS_IN_YEAR to WORK_HOURS, annualRate, I believe is dayRate or something like that, seems to be working.

      :-) I don't know math, I can also be way off...

      Book's version of code and mine is too much to post here, will do if you wish...

      I am hoping I can salvage some of the mathematic in the book's version to complete quicker or effortlessly.

      If you are okay with chunks of the code, I keep posting.

      Thanks again BigDaddyLH, for responding to this...

      Dököll
      Last edited by Dököll; Feb 13 '08, 03:05 AM. Reason: added remark...

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Dököll
        [CODE=JAVA]

        public void setRate(double annualRate) {
        interestRate = annualRate / 100.0 / MONTHS_IN_YEAR;
        }

        //above sets the inerets rate, in my code it's the commission rate...

        [/CODE]

        For something like that I would need to know what the 100.0 represents.
        I don't know if I'd call that a "Java question", but anyway, isn't 100.0 just converting from percentage to fraction? From 15 (percent) to 0.15? I can't imagine it's anything more than that.

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          #5
          Originally posted by BigDaddyLH
          I don't know if I'd call that a "Java question", but anyway, isn't 100.0 just converting from percentage to fraction? From 15 (percent) to 0.15? I can't imagine it's anything more than that.
          I was coming over to report I figured that part out, read the book a bit more, assume we have this:

          [CODE=JAVA]

          package loanPackage;


          class Loan {


          private final int MONTHS_IN_YEAR = 12;
          private double loanAmount;
          private double interestRate;
          private int numPayments;


          public Loan(double amount, double rate, int period) {
          setAmount(amoun t);
          setRate (rate );
          setPeriod(perio d);
          }


          public double getAmount( ) {
          return loanAmount;
          }


          public int getPeriod( ) {
          return numPayments / MONTHS_IN_YEAR;
          }


          public double getRate( ) {
          return interestRate * 100.0 * MONTHS_IN_YEAR;
          }


          public double get_mPayment( ) {
          double mPayment;

          mPayment = (loanAmount * interestRate)
          /
          (1 - Math.pow(1/(1 + interestRate),
          numPayments ) );
          return mPayment;
          }

          public double getFinalPayment ( ) {
          double totalPayment;

          totalPayment = getFinalPayment ( ) * numPayments;

          return totalPayment;
          }

          public void setAmount(doubl e amount) {
          loanAmount = amount;
          }

          public void setRate(double dayRate) {
          interestRate = dayRate / 100.0 / MONTHS_IN_YEAR;
          }

          public void setPeriod(int periodInDays) {
          numPayments = periodInDays * MONTHS_IN_YEAR;
          }


          }

          [/CODE]

          I modified the code to make it fit what I need for the long run. This is actual code to calculate monthly payments on a loan and gives out the full amount to be paid for a certain amount of years.

          Question is do you see any way this code can be used to calculate commission per hour worked and total amount of weekly pay for 40 hours:

          (1) Hours replaces years
          (2) Monthly payments replace daily commission pay

          and so on... My professor will say I am making too much of this:-)

          If you feel that logically it cannot be achieved, I'll stop now and run with my idea, same code but without all the math. Much appreciated assistance...

          Dököll

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            I guess I'm just confused about what you want to do. What does weekly pay have to do with interest payments? In any case, this is not a Java question.

            Comment

            • Dököll
              Recognized Expert Top Contributor
              • Nov 2006
              • 2379

              #7
              Originally posted by BigDaddyLH
              I guess I'm just confused about what you want to do. What does weekly pay have to do with interest payments? In any case, this is not a Java question.
              You're toying with me:-)

              I was turning the interest payments into commission payments, in fact, I have already completed my version of the code, it needs more work but I am almost there. Here's a better one for ya...

              [CODE=JAVA]

              Can you help me doctor up an if statement? I got below error:

              at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown Source)
              at java.lang.Doubl e.parseDouble(U nknown Source)
              at loanPackage.Loa nCalculator.get Input(LoanCalcu lator.java:70)
              at loanPackage.Loa nCalculator.sta rt(LoanCalculat or.java:33)
              at loanPackage.Loa nCalculator.mai n(LoanCalculato r.java:20)

              [/CODE]

              Here is the if statement:

              [CODE=JAVA]

              //Returns commission level for a certain percentage...

              public double getcommLevel( ) {
              double commmLevel;
              if (getRate( ) <= 5) {
              return commmLevel;
              } else if (getRate( ) <=10) {
              return commmLevel;
              } else if (getRate( ) <= 15) {
              return commmLevel;

              }

              [/CODE]

              What I am trying to do now is load a weekly amount according to getcommLevel( ) , getRate will hold the interestRate keyed in by user:

              [CODE=JAVA]

              //Returns the interest rate.
              public double getRate( ) {
              return interestRate * 100.0 * HOURS_WORKED;
              }

              [/CODE]

              What happens is commmLevel is underlined in red (through Eclipse) on each line in the code, seems to indicate it is undefined but as you can see double commmLevel; is there and decimals are being passed through.

              What do you think may be happening?

              Thanks!

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by Dököll
                [CODE=JAVA]
                //Returns commission level for a certain percentage...
                public double getcommLevel( ) {
                double commmLevel;
                if (getRate( ) <= 5) {
                return commmLevel;
                } else if (getRate( ) <=10) {
                return commmLevel;
                } else if (getRate( ) <= 15) {
                return commmLevel;
                }
                [/CODE]
                The error should be obvious -- commmLevel is never given a value. For example, if getRate() is less than 5 what value will this method return? Undefined! What if the rate is between 5 and 10? Undefined! What if between 10 and 15? Undefined! What if over 15 (you forgot that case)? Control path doesn't even return a value!

                You need to take a tutorial on the basics of programming because this code isn't properly thought out. Is this an introductory course you are taking?

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  #9
                  Originally posted by BigDaddyLH
                  The error should be obvious -- commmLevel is never given a value. For example, if getRate() is less than 5 what value will this method return? Undefined! What if the rate is between 5 and 10? Undefined! What if between 10 and 15? Undefined! What if over 15 (you forgot that case)? Control path doesn't even return a value!

                  You need to take a tutorial on the basics of programming because this code isn't properly thought out. Is this an introductory course you are taking?
                  Wrong code, and excuse the late response:

                  [CODE=JAVA]
                  //Returns commission level for a certain percentage...
                  public double getcommLevel( ) {
                  double commLevel;
                  if (getRate( ) <= 5) {
                  return commLevel= 99.99;
                  } else if (getRate( ) <=10) {
                  return commLevel= 299.99;
                  } else if (getRate( ) <= 15) {
                  return commLevel= 399.99;
                  }

                  [/CODE]

                  What I get with that is an underline part in my code, namely public double getcommLevel( ) { , this is keeping the code firing and giving the value as result.

                  the error says:

                  at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown Source)
                  at java.lang.Doubl e.parseDouble(U nknown Source)
                  at loanPackage.Loa nCalculator.get Input(LoanCalcu lator.java:75)
                  at loanPackage.Loa nCalculator.sta rt(LoanCalculat or.java:35)
                  at loanPackage.Loa nCalculator.mai n(LoanCalculato r.java:21)

                  it is defined as double< I am not sure that's even an issue.

                  What do you see?

                  Dököll

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    I hope you realize you are reporting two different, unrelated problems. First, you forgot to return a value when the rate is above 15:
                    [CODE=Java]public double getcommLevel() {
                    if (getRate() <= 5) {
                    return 99.99;
                    } else if (getRate() <=10) {
                    return 299.99;
                    } else if (getRate() <= 15) {
                    return 399.99;
                    } else {
                    return 499.99;
                    }
                    }[/CODE]
                    Second, you have some parsing error in another part of your code, line 75.

                    Comment

                    • Dököll
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 2379

                      #11
                      Originally posted by BigDaddyLH
                      I hope you realize you are reporting two different, unrelated problems. First, you forgot to return a value when the rate is above 15:
                      [CODE=Java]public double getcommLevel() {
                      if (getRate() <= 5) {
                      return 99.99;
                      } else if (getRate() <=10) {
                      return 299.99;
                      } else if (getRate() <= 15) {
                      return 399.99;
                      } else {
                      return 499.99;
                      }
                      }[/CODE]
                      Second, you have some parsing error in another part of your code, line 75.
                      Works like a charm BigDaddyLH! Thanks much:-) Now can tell me why below wouldn't have worked:

                      [CODE=JAVA]
                      //Returns commission level for a certain percentage...
                      public double getcommLevel( ) {
                      double commLevel;
                      if (getRate( ) <= 5) {
                      return commLevel= 99.99;
                      } else if (getRate( ) <=10) {
                      return commLevel= 299.99;
                      } else if (getRate( ) <= 15) {
                      return commLevel= 399.99;
                      } else {
                      return commLevel= 499.99;
                      }
                      }

                      [/CODE]

                      It got fuzzy the more I read into it. But commLevel seems to be legitimate.

                      Again, thanks, will post my reworking of the code mentioned above

                      Comment

                      • BigDaddyLH
                        Recognized Expert Top Contributor
                        • Dec 2007
                        • 1216

                        #12
                        Originally posted by Dököll
                        Works like a charm BigDaddyLH! Thanks much:-) Now can tell me why below wouldn't have worked:

                        [CODE=JAVA]
                        //Returns commission level for a certain percentage...
                        public double getcommLevel( ) {
                        double commLevel;
                        if (getRate( ) <= 5) {
                        return commLevel= 99.99;
                        } else if (getRate( ) <=10) {
                        return commLevel= 299.99;
                        } else if (getRate( ) <= 15) {
                        return commLevel= 399.99;
                        } else {
                        return commLevel= 499.99;
                        }
                        }

                        [/CODE]

                        It got fuzzy the more I read into it. But commLevel seems to be legitimate.

                        Again, thanks, will post my reworking of the code mentioned above
                        That code "works" but it is poorly written. The insertion of the local variable commLevel doesn't serve any purpose. I think the code falls between two stools. You could have either written it to directly return values without the superfluous local variable:
                        [CODE=Java]
                        public double getcommLevel2() {
                        if (getRate() <= 5) {
                        return 99.99;
                        } else if (getRate() <=10) {
                        return 299.99;
                        } else if (getRate() <= 15) {
                        return 399.99;
                        } else {
                        return 499.99;
                        }
                        }
                        [/CODE]
                        Or you use the local variable to support a single exit point from the method:
                        [CODE=Java]
                        public double getcommLevel3() {
                        double commLevel;
                        if (getRate() <= 5) {
                        commLevel= 99.99;
                        } else if (getRate() <=10) {
                        commLevel= 299.99;
                        } else if (getRate() <= 15) {
                        commLevel= 399.99;
                        } else {
                        commLevel= 499.99;
                        }
                        return commLevel;
                        }
                        [/CODE]
                        This style is sometimes easier to work with in some debuggers, easier to add debugging print statements and sometime easier to understand program flow.

                        Comment

                        • Dököll
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 2379

                          #13
                          Originally posted by BigDaddyLH
                          That code "works" but it is poorly written. The insertion of the local variable commLevel doesn't serve any purpose. I think the code falls between two stools. You could have either written it to directly return values without the superfluous local variable:
                          [CODE=Java]
                          public double getcommLevel2() {
                          if (getRate() <= 5) {
                          return 99.99;
                          } else if (getRate() <=10) {
                          return 299.99;
                          } else if (getRate() <= 15) {
                          return 399.99;
                          } else {
                          return 499.99;
                          }
                          }
                          [/CODE]
                          Or you use the local variable to support a single exit point from the method:
                          [CODE=Java]
                          public double getcommLevel3() {
                          double commLevel;
                          if (getRate() <= 5) {
                          commLevel= 99.99;
                          } else if (getRate() <=10) {
                          commLevel= 299.99;
                          } else if (getRate() <= 15) {
                          commLevel= 399.99;
                          } else {
                          commLevel= 499.99;
                          }
                          return commLevel;
                          }
                          [/CODE]
                          This style is sometimes easier to work with in some debuggers, easier to add debugging print statements and sometime easier to understand program flow.
                          I see...

                          Boy you make it sound easy... Just finished it up, still weird in some spots. My first true design in Java actually:

                          (1) everything on paper to look at it
                          (2) a silly pseudocode to aid in the purpose of the code

                          I am not sure how I got the math to work my way, but I had an example of what commission pay should result to, I've manually caculated overtime payments, and when added into the program if it did not match my results on paper, back to square one. So excting though truly. Please tell me what else you see that could have been simpler. I know I went to far on this one:

                          [CODE=JAVA]

                          package burgersPackage;

                          /**
                          * @Lab 3
                          * @version 20080514
                          * @author :-)
                          * @Professor Jane Doe
                          * @param args
                          */

                          class MyJava_Burgers {
                          private double salesAmount;
                          private double commissionRate;
                          private double HOURS_WORKED_ST ANDARD = 40;

                          private final double WAGE_RATE = 7.25;
                          private final double COMM_RATE_1 = 99.99;
                          private final double COMM_RATE_2 = 299.99;
                          private final double COMM_RATE_3 = 399.99;
                          private final double COMM_RATE_1_Low = 1.00;
                          private final double COMM_RATE_2_Low = 100.00;
                          private final double COMM_RATE_3_Low = 300.00;
                          private final double OT_WORKED_STAND ARD = 1.5;


                          //Constructor: Initiating block...
                          public MyJava_Burgers( double amount, double rate, double payHour) {
                          setAmount(amoun t);
                          setRate (rate);
                          setHours (payHour);
                          }

                          //Returns the sales amount.
                          public double getAmount( ) {
                          return salesAmount;
                          }

                          //Returns all hours worked.
                          public double getHours() {

                          return HOURS_WORKED_ST ANDARD;
                          }


                          //Returns Regular hours.
                          public double getRegHours() {

                          return getHours() - get_OT_Hours();
                          }

                          //Returns overtime hours.
                          public double get_OT_Hours() {
                          return loadHours() - 40;
                          }

                          //Stimulates hours: not sure what happenned but I lost my thinking here:-)
                          public double loadHours() {
                          if (getHours() == 40) {
                          return HOURS_WORKED_ST ANDARD;
                          } else if (getHours() < 40) {
                          return HOURS_WORKED_ST ANDARD;
                          } else if (getHours() > 40) {
                          return HOURS_WORKED_ST ANDARD;
                          } else {
                          return 0;
                          }
                          }

                          //Returns the commission rate.
                          public double getRate() {
                          return commissionRate * 100.0 * 40;
                          }

                          //Returns weekly regualr pay
                          public double getWeeklyPay() {
                          return WAGE_RATE * 40;
                          }


                          //Returns overtime pay
                          public double getOvertimePayR ate() {
                          return WAGE_RATE * OT_WORKED_STAND ARD;
                          }


                          [/CODE]

                          This is where it got confusing, so thanks on that. I switched it up a little bit...

                          [CODE=JAVA]

                          //Captures commission rate and computes Commission level pay
                          public double getcomLevel() {
                          if (getRate() == 5) {
                          return COMM_RATE_1;
                          } else if (getRate() < 5) {
                          return COMM_RATE_1_Low ;
                          } else if (getRate() == 10) {
                          return COMM_RATE_2;
                          } else if (getRate() == 15) {
                          return COMM_RATE_3;
                          } else if (getRate() <= 10) {
                          return COMM_RATE_2_Low ;
                          } else if (getRate() <= 15) {
                          return COMM_RATE_3_Low ;
                          } else {
                          return 0;
                          }
                          }



                          //Returns commission payment
                          public double get_commPayment () {
                          double commmPayment;
                          commmPayment = getRate() * salesAmount / 100.0;
                          return commmPayment;
                          }

                          //Returns overtime payment
                          public double getOvetimePayme nt() {
                          double overtimePayment ;
                          overtimePayment = get_OT_Hours() * getOvertimePayR ate();
                          return overtimePayment ;
                          }


                          //Returns total weekly payment
                          public double getFinalPayment () {
                          double totalPayment;
                          totalPayment = getWeeklyPay() + get_commPayment () + getOvetimePayme nt();
                          return totalPayment;
                          }

                          //Sets the sales amount of this sales.
                          public void setAmount(doubl e amount) {
                          salesAmount = amount;
                          }

                          //Sets the sales amount in hours.
                          public void setHours(double payHour) {
                          HOURS_WORKED_ST ANDARD = payHour;
                          }

                          //Sets commission rate on sales.
                          public void setRate(double dayRate) {
                          commissionRate = dayRate / 100.0 / HOURS_WORKED_ST ANDARD;
                          }

                          }

                          [/CODE]

                          This was the template. I adding it all here so you can run it and see... I think I'll just reply to this post to avoid problems:-)

                          Comment

                          • Dököll
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 2379

                            #14
                            [CODE=JAVA]

                            package burgersPackage;

                            /**
                            * @Lab 3
                            * @version 20080514
                            * @author ...
                            * @Professor BenFoldsFive
                            * @param args
                            */

                            import javax.swing.*;
                            import java.text.*;

                            class burgersCalculat or {
                            // This object does the actual sales computation
                            private MyJava_Burgers myjava_burgers;

                            public static void main(String[] arg) {

                            burgersCalculat or calculator = new burgersCalculat or();
                            calculator.star t();

                            }

                            public burgersCalculat or() {

                            }

                            //Top-level method that calls other private methods
                            public void start() {

                            describeProgram (); //tell what the program does
                            getInput(); //get three input values
                            displayOutput() ; //display the results
                            }
                            // Provides a brief explanation of the program to the user.
                            private void describeProgram () {
                            System.out.prin tln("This application calculates all sales amount per commission");
                            System.out.prin tln("The application computes results into weekly pay and includes ovetime pay ");
                            System.out.prin tln("We hope to gather Overtime payment + Commission + Regular pay in the end...");
                            System.out.prin tln("Let us begin!");
                            System.out.prin tln("\n");
                            }
                            //Displays the input values and weekly and total earnings.
                            private void displayOutput() {

                            DecimalFormat df = new DecimalFormat(" 0.00");

                            System.out.prin tln("(1) Sales Amount is $ " + myjava_burgers. getAmount());
                            System.out.prin tln("(2) Sales Commission Rate is "
                            + myjava_burgers. getRate() + "%");
                            System.out.prin tln("(3) Number of Overtime Hours "
                            + myjava_burgers. get_OT_Hours( ) + " hours");
                            System.out.prin tln("(4) Regular Hours " + df.format(myjav a_burgers.getRe gHours()) + " hours");
                            System.out.prin tln("(5) All Hours Worked Number is " + df.format(myjav a_burgers.loadH ours()) + " hours");
                            System.out.prin tln("(6) Overtime Pay Amount $ "
                            + myjava_burgers. getOvetimePayme nt( ));
                            System.out.prin tln("(7) Sales Commission Payment is $ " + df.format(myjav a_burgers.get_c ommPayment( )));
                            System.out.prin tln("(8) Regular Weekly Payment is $ " + df.format(myjav a_burgers.getWe eklyPay( )));
                            System.out.prin tln(" Total Weekly Payment is $ " + df.format(myjav a_burgers.getFi nalPayment()));

                            }

                            // Gets three input values--sales amount, interest rate, and
                            private void getInput() {
                            String inputStr;
                            double salesAmount, commissionRate, HOURS_WORKED_ST ANDARD;

                            inputStr = JOptionPane.sho wInputDialog(nu ll,
                            "Sales Amount (Dollars + Cents):");
                            salesAmount = Double.parseDou ble(inputStr);

                            inputStr = JOptionPane.sho wInputDialog(nu ll,
                            "Daily Commission Rate (e.g., 9.5):");
                            commissionRate = Double.parseDou ble(inputStr);

                            inputStr = JOptionPane.sho wInputDialog(nu ll,
                            "Hours worked - # of hours:");
                            HOURS_WORKED_ST ANDARD = Double.parseDou ble(inputStr);


                            //+++++++++++++++ +++++++++++++++
                            //DO NOT USE this while loop, it goes one for a while:-)
                            //This works but I get stuck there for some reason...

                            //while (commissionRate < 5) {
                            //JOptionPane.sho wMessageDialog( null,
                            //"Invalid entry, please enter number greater or equal to 5");
                            //}
                            //create a new sales with the input values
                            myjava_burgers = new MyJava_Burgers( salesAmount, commissionRate, HOURS_WORKED_ST ANDARD);

                            }

                            [/CODE] Let me know if you think this is Article material, if up to stuff, will pull it and add there, o at least ask someone, not sure if I can do that from here. Anyway, thanks much BigDaddyLH for everything. Let me know what you see need work.

                            In a bit!
                            Last edited by Dököll; Feb 21 '08, 05:22 AM. Reason: removed text...

                            Comment

                            • Dököll
                              Recognized Expert Top Contributor
                              • Nov 2006
                              • 2379

                              #15
                              I figured this part out. I was not calling the input dialog after data did not take:

                              [CODE=JAVA]

                              while (commissionRate < 5) {
                              JOptionPane.sho wMessageDialog( null,
                              "Invalid entry, please enter number greater or equal to 5");

                              inputStr = JOptionPane.sho wInputDialog(nu ll,
                              "Daily Commission Rate (e.g., 9.5):");
                              commissionRate = Double.parseDou ble(inputStr);
                              }

                              [/CODE]

                              See yoo soon!

                              Dököll

                              Comment

                              Working...