423,867 Members | 3,449 Online
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

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

 Expert 100+ P: 2,364 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: Expand|Select|Wrap|Line Numbers      public double getPayment( ) {         double mPayment;           mPayment = (loanAmount * interestRate)                            /                          (1 - Math.pow(1/(1 + interestRate),                                           numPayments ) );         return mPayment;     }     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: Expand|Select|Wrap|Line Numbers what is 1 in:   (1 - Math.pow   what is 1 in:   pow(1/ what is 1 in:   (1 + interestRate     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 Feb 12 '08 #1
14 Replies

 Expert 100+ P: 1,216 Aren't you just asking how to solve an equation for a certain variable? That's not particularly a Java question is it? Feb 12 '08 #2

 Expert 100+ P: 2,364 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: Expand|Select|Wrap|Line Numbers       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...     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 Feb 13 '08 #3

 Expert 100+ P: 1,216 Expand|Select|Wrap|Line Numbers       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...     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. Feb 13 '08 #4

 Expert 100+ P: 2,364 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: Expand|Select|Wrap|Line Numbers   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(amount);         setRate  (rate  );         setPeriod(period);     }           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(double 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;     }     }     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 Feb 13 '08 #5

 Expert 100+ P: 1,216 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. Feb 13 '08 #6

 Expert 100+ P: 2,364 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... Expand|Select|Wrap|Line Numbers   Can you help me doctor up an if statement?  I got below error:       at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)     at java.lang.Double.parseDouble(Unknown Source)     at loanPackage.LoanCalculator.getInput(LoanCalculator.java:70)     at loanPackage.LoanCalculator.start(LoanCalculator.java:33)     at loanPackage.LoanCalculator.main(LoanCalculator.java:20)     Here is the if statement: Expand|Select|Wrap|Line Numbers   //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;       }     What I am trying to do now is load a weekly amount according to getcommLevel( ) , getRate will hold the interestRate keyed in by user: Expand|Select|Wrap|Line Numbers        //Returns the interest rate.     public double getRate( ) {         return interestRate * 100.0 * HOURS_WORKED;     }     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! Feb 14 '08 #7

 Expert 100+ P: 1,216 Expand|Select|Wrap|Line Numbers //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;     }   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? Feb 14 '08 #8

 Expert 100+ P: 2,364 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: Expand|Select|Wrap|Line Numbers //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;     }     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.FloatingDecimal.readJavaFormatString(Unkn own Source) at java.lang.Double.parseDouble(Unknown Source) at loanPackage.LoanCalculator.getInput(LoanCalculator .java:75) at loanPackage.LoanCalculator.start(LoanCalculator.ja va:35) at loanPackage.LoanCalculator.main(LoanCalculator.jav a:21) it is defined as double< I am not sure that's even an issue. What do you see? Dököll Feb 17 '08 #9

 Expert 100+ P: 1,216 I hope you realize you are reporting two different, unrelated problems. First, you forgot to return a value when the rate is above 15: Expand|Select|Wrap|Line Numbers 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;     } } Second, you have some parsing error in another part of your code, line 75. Feb 17 '08 #10

 Expert 100+ P: 2,364 I hope you realize you are reporting two different, unrelated problems. First, you forgot to return a value when the rate is above 15: Expand|Select|Wrap|Line Numbers 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;     } } 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: Expand|Select|Wrap|Line Numbers //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;     }     }     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 Feb 19 '08 #11

 Expert 100+ P: 1,216 Works like a charm BigDaddyLH! Thanks much:-) Now can tell me why below wouldn't have worked: Expand|Select|Wrap|Line Numbers //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;     }     }     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: Expand|Select|Wrap|Line Numbers     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;         }     }   Or you use the local variable to support a single exit point from the method: Expand|Select|Wrap|Line Numbers     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;     }   This style is sometimes easier to work with in some debuggers, easier to add debugging print statements and sometime easier to understand program flow. Feb 19 '08 #12

 Expert 100+ P: 2,364 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: Expand|Select|Wrap|Line Numbers     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;         }     }   Or you use the local variable to support a single exit point from the method: Expand|Select|Wrap|Line Numbers     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;     }   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: Expand|Select|Wrap|Line Numbers   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_STANDARD = 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_STANDARD = 1.5;         //Constructor: Initiating block...        public MyJava_Burgers(double amount, double rate, double payHour) {         setAmount(amount);         setRate  (rate);         setHours  (payHour);     }       //Returns the sales amount.     public double getAmount( ) {         return salesAmount;     }       //Returns all hours worked.     public double getHours() {                 return HOURS_WORKED_STANDARD;     }         //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_STANDARD;                     } else if (getHours() < 40) {                 return HOURS_WORKED_STANDARD;         } else if (getHours() > 40) {             return HOURS_WORKED_STANDARD;         } 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 getOvertimePayRate() {         return WAGE_RATE * OT_WORKED_STANDARD;     }       This is where it got confusing, so thanks on that. I switched it up a little bit... Expand|Select|Wrap|Line Numbers       //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 getOvetimePayment() {         double overtimePayment;         overtimePayment = get_OT_Hours() * getOvertimePayRate();                          return overtimePayment;     }            //Returns total weekly payment     public double getFinalPayment() {         double totalPayment;         totalPayment = getWeeklyPay() + get_commPayment() + getOvetimePayment();         return totalPayment;     }       //Sets the sales amount of this sales.     public void setAmount(double amount) {         salesAmount = amount;     }       //Sets the sales amount in hours.     public void setHours(double payHour) {         HOURS_WORKED_STANDARD = payHour;     }       //Sets commission rate on sales.     public void setRate(double dayRate) {         commissionRate = dayRate / 100.0 / HOURS_WORKED_STANDARD;     }   }     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:-) Feb 21 '08 #13

 Expert 100+ P: 2,364 Expand|Select|Wrap|Line Numbers   package burgersPackage;   /**  * @Lab 3  * @version 20080514  * @author ...  * @Professor BenFoldsFive  * @param args  */   import javax.swing.*; import java.text.*;   class burgersCalculator {    // This object does the actual sales computation   private MyJava_Burgers myjava_burgers;     public static void main(String[] arg) {         burgersCalculator calculator = new burgersCalculator();       calculator.start();     }     public burgersCalculator() {     }     //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.println("This application calculates all sales amount per commission");       System.out.println("The application computes results into weekly pay and includes ovetime pay ");       System.out.println("We hope to gather Overtime payment + Commission + Regular pay in the end...");       System.out.println("Let us begin!");       System.out.println("\n");   }   //Displays the input values and weekly and total earnings.   private void displayOutput() {         DecimalFormat df = new DecimalFormat("0.00");         System.out.println("(1) Sales Amount is \$ " + myjava_burgers.getAmount());       System.out.println("(2) Sales Commission Rate is "                           + myjava_burgers.getRate() + "%");        System.out.println("(3) Number of Overtime Hours "                       + myjava_burgers.get_OT_Hours( ) + " hours");       System.out.println("(4) Regular Hours  " + df.format(myjava_burgers.getRegHours()) + " hours");       System.out.println("(5) All Hours Worked Number is " + df.format(myjava_burgers.loadHours()) + " hours");       System.out.println("(6) Overtime Pay Amount \$ "               + myjava_burgers.getOvetimePayment( ));       System.out.println("(7) Sales Commission Payment is \$ " + df.format(myjava_burgers.get_commPayment( )));       System.out.println("(8) Regular Weekly Payment is \$ " + df.format(myjava_burgers.getWeeklyPay( )));       System.out.println("    Total Weekly Payment is \$ " + df.format(myjava_burgers.getFinalPayment()));                                   }     // Gets three input values--sales amount, interest rate, and   private void getInput() {       String inputStr;       double salesAmount, commissionRate, HOURS_WORKED_STANDARD;         inputStr           = JOptionPane.showInputDialog(null,                                   "Sales Amount (Dollars + Cents):");       salesAmount         = Double.parseDouble(inputStr);                 inputStr           = JOptionPane.showInputDialog(null,       "Daily Commission Rate (e.g., 9.5):");       commissionRate = Double.parseDouble(inputStr);         inputStr           = JOptionPane.showInputDialog(null,                                   "Hours worked - # of hours:");       HOURS_WORKED_STANDARD = Double.parseDouble(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.showMessageDialog(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_STANDARD);     }     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! Feb 21 '08 #14

 Expert 100+ P: 2,364 I figured this part out. I was not calling the input dialog after data did not take: Expand|Select|Wrap|Line Numbers        while (commissionRate < 5) {       JOptionPane.showMessageDialog(null,      "Invalid entry, please enter number greater or equal to 5");          inputStr           = JOptionPane.showInputDialog(null,       "Daily Commission Rate (e.g., 9.5):");       commissionRate = Double.parseDouble(inputStr);   }     See yoo soon! Dököll Feb 21 '08 #15