Welcome to Dream.In.Code
Become a Java Expert!

Join 149,614 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




Java Errors

 
Reply to this topicStart new topic

Java Errors, Need Help seeign why my program will not compile

dogma992
19 Aug, 2007 - 09:07 AM
Post #1

New D.I.C Head
*

Joined: 19 Aug, 2007
Posts: 11


My Contributions
MortgageCalculator.java:19: pow(double,double) in java.lang.Math cannot be applied to (double)
payment=principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0) -periods) ) )); ^

MortgageCalculator.java:19: possible loss of precision
found : double
required: int
payment=principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0) -periods) ) )); ^

I am receiving the following errors for my attached program any advice would be greatly appreciated. Thanks


Attached File(s)
Attached File  MortgageCalculator.txt ( 727bytes ) Number of downloads: 28
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Java Errors
19 Aug, 2007 - 09:28 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
QUOTE(dogma992 @ 19 Aug, 2007 - 01:07 PM) *

MortgageCalculator.java:19: possible loss of precision
found : double
required: int
payment=principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0) -periods) ) )); ^


Your variable payment is a int, and you made it return a decimal value. The variable will be reducted to a int, causing a loss of precision. Change the payment variable to a double.

User is offlineProfile CardPM
+Quote Post

dogma992
RE: Java Errors
19 Aug, 2007 - 09:33 AM
Post #3

New D.I.C Head
*

Joined: 19 Aug, 2007
Posts: 11


My Contributions
QUOTE(alpha02 @ 19 Aug, 2007 - 10:28 AM) *

QUOTE(dogma992 @ 19 Aug, 2007 - 01:07 PM) *

MortgageCalculator.java:19: possible loss of precision
found : double
required: int
payment=principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0) -periods) ) )); ^


Your variable payment is a int, and you made it return a decimal value. The variable will be reducted to a int, causing a loss of precision. Change the payment variable to a double.



I just changed it and I still receive the same error, any ideas? Thanks


MortgageCalculator.java:19: pow(double,double) in java.lang.Math cannot be applied to (double)
payment=principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0) -periods) ) ));
^
1 error

User is offlineProfile CardPM
+Quote Post

alpha02
RE: Java Errors
19 Aug, 2007 - 09:38 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
Math.pow needs TWO double as the arguments, and you just put one. It works as follow:
CODE

double r = Math.pow(10.0, 2.0); //100
double s = Math.pow(2.0, 3.0); //8
double t = Math.pow(4.0, 3.0); //64


Hope this helps, this should solve your problem smile.gif
User is offlineProfile CardPM
+Quote Post

dogma992
RE: Java Errors
19 Aug, 2007 - 09:56 AM
Post #5

New D.I.C Head
*

Joined: 19 Aug, 2007
Posts: 11


My Contributions
QUOTE(alpha02 @ 19 Aug, 2007 - 10:38 AM) *

Math.pow needs TWO double as the arguments, and you just put one. It works as follow:
CODE

double r = Math.pow(10.0, 2.0); //100
double s = Math.pow(2.0, 3.0); //8
double t = Math.pow(4.0, 3.0); //64


Hope this helps, this should solve your problem smile.gif




So what would I have to change to correct this issue? Sorry for all of the questions but I am brand new to Java. Thanks for all of your help


Steve
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Java Errors
19 Aug, 2007 - 11:40 AM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi Dogma,

You had a few things going on here that needed fixing. The error wasn't helping you much in diagnosing everything. Here is a working version for you...

CODE

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MortgageCalculator
{
public static void main (String[] args)
  {
  
   //variables
   DecimalFormat decimalPlaces=new DecimalFormat("0.00");
   double periods,principle,interest,payment;
   int amount;
  

   interest=.0575;
   principle=200000.0;
   periods=30.0;
    
   payment = principle * ((interest / 12.0) / (1.0 - Math.pow( (1.0 + (interest / 12.0)), -periods) ) );
   //Output
   System.out.println("Principle="+principle);
   System.out.println("interest rate="+interest*100);
   System.out.println("Payments="+periods);
   System.out.println("Payment="+payment);
   System.out.println(decimalPlaces.format(payment));

  }
}


Here are the changes I made... principle and periods were defined as double, so you should make sure you put a decimal into them. Next, you were using an integer (1) when dealing with calculations in double. So just make them 1.0.

Next payment was defined as an integer, it should be double because the calculation at the end will be double. So I moved it to the double definition list.

Then in your formula, you were not defining the "second" parameter in pow()... the exponent. So I put in the comma before -periods to make it your power. This made it to the power of negative 30.

Also notice that I moved a parenthesis or two to make the correct order of operations to compensate for the new comma I put in.

After running this code I got a total of $7,173.24 as a mortgage payment which will result in paying $215,197.20 over the 30 months. That is over 15,000 in interest! Which sounds about right for a 200,000 principle over almost 3 years of payments.

Hope this all makes sense!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:27AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month