2 errors I can't understand
I am in a Java Programming course and the first assignment was to generate a mortgage calculating java application that will calculate a $200,000 for a 30 year term at 5.75% interest and calculate it into what the monthly interest payment would be. This is what I have so far...
Code Java:
/**
* @(#)MortgageCalculator.java
*
* MortgageCalculator application
*
* @author Brock Overberg
* @version 1.00 2010/12/19
*/
public class Main {
public static void main(String[] args) {
// Declare, initialize and calculate variables
long amount = 200000.00; // Total amount of loan
int term = 360; // Term of loan in months
int interestRate = 5.75; // Interest rate
long amount * int interestRate = totalInterestRate; // Multiply total amount of loan by the interest rate
totalInterestRate / 360; // Total interest rate divided by 360 months in 30 years
System.out.println("The monthly interest payment will be" = "totalInterestRate" / "360"); // Display the result
}
}
I get 2 errors as follows...
C:\Users\Brock\Documents\JCreator Pro\MyProjects\MortgageCalculator\src\MortgageCalc ulator.java:18: ';' expected
long amount * int interestRate = totalInterestRate; // Multiply total amount of loan by the interest rate
^
C:\Users\Brock\Documents\JCreator Pro\MyProjects\MortgageCalculator\src\MortgageCalc ulator.java:19: not a statement
totalInterestRate / 360; // Total interest rate divided by 360 months in 30 years
^
2 errors
Process completed.
Can anyone help?
Re: 2 errors I can't understand
line 18 is in the wrong order: variable = x + y;
your variable is undeclared with no type. Choose a type int or double? Which is for money?
5.75 percent is actually .0575 as a decimal number.
-john :o
Re: 2 errors I can't understand
New revised version...
Code Java:
/**
* @(#)MortgageCalculator.java
*
* MortgageCalculator application
*
* @author Brock Overberg
* @version 1.00 2010/12/19
*/
public class MortgageCalculator.java {
public static void main(String[] args) {
// Declare, initialize and calculate variables
Double totalAmount = 200000.00; // Total amount of loan
Double termInMonths = 360; // Term of loan in months
Double interestRate = 0.0575; // Interest rate
System.out.println("Monthly Interest Rate" = "200000 * 0.0575 / 360"); // Display the result
}
}
Re: 2 errors I can't understand
Here is the proper working code:
Code Java:
public class MortgageCalculator {
public static void main(String[] args) {
// Declare, initialize and calculate variables
double totalAmount = 200000.0; // Total amount of loan
double termInMonths = 360.0; // Term of loan in months
double interestRate = 5.75; // Interest rate
// Display the result
System.out.println("Monthly Interest Rate: "+ ((totalAmount * interestRate) / termInMonths));
}
}
Hope that helps,
Goldest
Re: 2 errors I can't understand
Hey Brock,
There are some things for you,
Quote:
public class MortgageCalculator.java {}
When we declare a java class we shouldn't mention the ".java" extension inside the source code.
The rule is that the name of your source file and your only public class should match. It means that if your source file is Hello.java, then your public class name should be Hello.
Quote:
Double totalAmount = 200000.00; // Total amount of loan
Double termInMonths = 360; // Term of loan in months
Double interestRate = 0.0575; // Interest rate
Plus, you are using Wrapper classes above, which means "Double". They shouldn't be used unless you need to use primitives as objects, which is not the case here. So you can go with simple primitives "double" declarations.
Quote:
System.out.println("Monthly Interest Rate" = "200000 * 0.0575 / 360");
Now finally coming to your print statement, the concatenating operator in java is '+' and not '=', so you must use '+' whenever you are trying to display your result on console.
Secondly, you are using hard coded values for calculations. If hard coded values were meant to be used then whats the point of declaring the variables? Keep this in mind, not to go for hard coded values when we have the same initiated inside variables.
I hope that things are much clear to you now!
Goldest
Re: 2 errors I can't understand
I don't know whether to ask this here or not, but because I see its relevant, I am going to ask it anyways. I come from c and am very very new to java however in c we were always taught to refrain from using double for money, instead use int for value then other int for pennies, and i found out that it was much more effective as a system to deal with money. Is it okay in java to use doubles for monetary calculations? Will it be pain in the [bleep] when the problem gets complex?
Re: 2 errors I can't understand
Doubles are subject to the same problem in Java or in C/C++.
It all has to deal with the way floating point numbers are represented.
When you use an integer/long value, you're guaranteed to keep track of every last digit. Floats and doubles are meant to keep track "rough estimates" of decimal values. The consist of a mantissa and an exponent (similar to scientific notation representation of numbers).
For example, say there are a billion people and they each have to pay $5 to a pool of money. Now you're goal is to write a program which will add $5 to the pool every time someone contributes.
For the sake of simplicity, this could be modeled as:
Code Java:
float poolF = 0;
long poolI = 0;
int individualContribution = 5;
for (int i = 0; i < 1000000000; ++i)
{
poolF += individualContribution;
poolI += individualContribution;
}
System.out.printf("float pool amount = $%.2f\n", poolF);
System.out.println("actual (integer) pool amount = $" + poolI);
At a certain point, the floating point representation of 5 dollars becomes so insignificant compared to the current pool amount that the floating point value will just add 0 instead of the 5 dollars. Integers/longs do not suffer from this problem (they will suffer from other problems due to their outer bounds, but for a 32/64-bit system these values are far beyond any monetary values used today)
This leads to the following output:
float pool amount = $134217728.00
actual (integer) pool amount = $5000000000
Using doubles in this above example would remedy this particular case (in fact it would fix most conceivable cases), but at some point it could still run into the same problems.
Long story short:
1. Using floats/doubles is ok for most cases (the vast majority of cases) in both C/C++ and Java
2. Any cases it would not be ok in one means it will not be ok in the other
3. The syntax of C/C++ and Java are similar enough in this respect that you could almost copy the C/C++ code verbatim and it would work in Java for this type of problem (there are a few sticky points such as pointers and references you'd have to remedy, usually this means the Java code would be easier rather than harder).
Re: 2 errors I can't understand
Helloworld922, what you have wrote is perfect example of the way the data type should be taught in any languages and is very insightful, however one thing I want to point out is that, it seems though the comparison you did was mainly between float and a double, or did I misunderstood(I am not trying to undermine your input)? If I didn't, then would the same comparision be done between a double and an integer when it comes to a large digit number, specially if the other end of the spectrum(i.e. double) has a large precision(i.e. decimal places)?
I repeat again. I am not being smartass or trolling, am just trying to understand this. However as you have already stated the java and c/c++ are very similar and I concur with that, saying so, I must also say that, was merely trying to see the common programing techniques used in java[if there are any, different than others].
Thank you :)
Re: 2 errors I can't understand
I'm simply trying to define the scale at which you should be worried about using floats vs. doubles and ints vs. longs.
For all practical monetary values, you probably won't see these problems with doubles, but they could pop up with floats (albeit, rather large monetary values).
Take the previous code and run it with doubles (I would advise against trying to run this, it will take a while):
Code Java:
double poolF = 0;
long poolI = 0;
int individualContribution = 5;
for (long i = 0; i < 100000000000L; ++i) // even increased the number of contributors by 100x
{
poolF += individualContribution;
poolI += individualContribution;
}
System.out.printf("double pool amount = $%.2f\n", poolF);
System.out.println("actual (integer) pool amount = $" + poolI);
Even after 100 billion contributors, the double result has no error. Also, keep in mind that even with floats the code was able to get up to 1 billion contributors, a rather large amount in itself.
double pool amount = $500000000000.00
actual (integer) pool amount = $500000000000
Longs and integers will eventually experience "wrapping error". This happens when the mathematical operation takes the result physically out of the range of longs and integers (2^63-1 for longs, 2^31-1 for integers). In monetary terms, it's possible to overload an integer (~2 trillion), however like with doubles it's nearly impossible to conceive a practical case where longs would be overloaded (~9 sextillion).