Where do I startNeed help figuring out how to start coding java
22 Replies - 1797 Views - Last Post: 10 January 2011 - 07:26 AM
#1
Where do I start
Posted 08 January 2011 - 06:09 PM
I need some help getting started creating a Java program for a class I am taking.
Here is what I need to do:
Create A program written in Java (without a graphical user interface) that will calculate and display the monthly payment amount to fully amortize a $200,000.00 loan over a 30 year term at 5.75‰ interest
I don't even know where to begin with this.
I am using JDK 6 Update 23 and we are required to use Text Pad for the class to write our coding.
Thanks in advance for any help.
IKE
Replies To: Where do I start
#2
Re: Where do I start
Posted 08 January 2011 - 06:37 PM
public class Name {
public static void main(String[] args) {
}
}
Here "Name" would be the name of your program's main class. So for your example perhaps it will be called "Mortgage". Whatever you call it, make sure you save the file with the exact same name. So you will save the Mortgage class in a file called "Mortgage.java"
Once you have this, open up a command prompt window and navigate to where you saved the file. Type javac Mortgage.java and hit enter. This will compile the program. If there are no errors, type java Mortgage to run the program. Notice I didn't put ".java" on the end of this command. Leave it off.
Each time you make a change, you will have to recompile it using javac and rerun it using java.
Then I suggest you look up how you figure out mortgages. Also do a search on our site for Mortgage programs because we do a TON of these programs in java. I think pbl and I alone have like 50 of them. hehe
#3
Re: Where do I start
Posted 08 January 2011 - 07:01 PM
I did do a search for "java mortgage" and came up with no results.
Also thanks to the text pad editor, I can just press "ctrl 1" to compile and "ctrl 2" to run program.
It also gives error explanations and such.
I will do another search for mortgage coding under java, and see if I find something.
IKE
#4
Re: Where do I start
Posted 08 January 2011 - 07:38 PM
I am not suer if this is correct or not, it does compile with no errors, but it gives me this error when I run it.
**********************************************
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
at Mortgage.main<mortgage.java:30>
*********************************************
import java.text.DecimalFormat;
public class Mortgage
{
public static void main(String args[]) throws Exception
{
// declare and construct variables
int loanAmt = 200000; // principal loan amount
int[] loanTerm = {360}; // loan term for 30 years
int loanYears; // indicates the loan term in years
double[] intRate = {5.75}; // interest rate 30 years
double newIntRate; // displays interest rate calculation
double monthlyPay = 0; // displays monthly payment calculation
DecimalFormat money = new DecimalFormat("$0.00"); // displays mortgage in decimal format
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
// displays messages to indicate purpose of program and conditions in the console window
System.out.println();
System.out.println("Welcome to the Mortgage Payment Calculator");
System.out.println();
System.out.println("This program will calculate the mortgage payments for a $" + loanAmt);
System.out.println("with the following loan terms and interest rates: 30 years @ 5.75%");
System.out.println();
System.out.println("The results are as follows:");
int i;
for (i = 0; i <= 2; i++)
{
intRate[i] = (intRate[i] /12 * .01);
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
// displays results for loan in the console window
System.out.println();
System.out.println("The mortgage payment for a $" + loanAmt + " loan for " + loanTerm[i] + " years at");
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
}
}
}
Thanks again for the help!!
IKE
#5
Re: Where do I start
Posted 09 January 2011 - 03:23 AM
int[] arrayOfInts = new int[10];
which would have 10 items at indexes 0 through 9.
This post has been edited by GregBrannon: 09 January 2011 - 03:29 AM
#6
Re: Where do I start
Posted 09 January 2011 - 07:38 AM
Thanks for the reply.
I am not sure what you are telling me. I don't have any array that is listing more than one item.
On line 9 it only has 1 loan term listing of 360 months which equals 30 years, and
on line 11 there is only 1 interest ammount lising of 5.57 percent.
I tried to change the { } on each to [ ], but when it compiled it gave me errors as follows:
*****************************************************
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:9: illegal start of expression
int[] loanTerm = [360]; // loan term for 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:9: not a statement
int[] loanTerm = [360]; // loan term for 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:9: ';' expected
int[] loanTerm = [360]; // loan term for 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:11: illegal start of expression
double[] intRate = [5.75]; // interest rate 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:11: not a statement
double[] intRate = [5.75]; // interest rate 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:11: ';' expected
double[] intRate = [5.75]; // interest rate 30 years
*************************************************
If I replace the [ ] with { } I don't get the errors. (any ideas)
Also it doesn't show the correct number of years when it runs, it shows 360 instead of 30 years.
Thannks in advance for the assistance.
#7
Re: Where do I start
Posted 09 January 2011 - 07:48 AM
Here is sun's tutorial about arrays:
http://download.orac...lts/arrays.html
What are you trying to do in those lines:
int[] loanTerm = [360]; // loan term for 30 years double[] intRate = [5.75]; // interest rate 30 years
are you trying to create a new array of 360 values? or you try to asssign the value 360 to an index in the array?
Try reading the tutorail I provided. If you still have any problem, feel free to post again
#8
Re: Where do I start
Posted 09 January 2011 - 07:53 AM
But here is what I am trying to do in general.
I am trying to get the java program co calculate the monthly payment ammount on a 30 year mortgage of $200,000 with a 5.75% interest rate.
I already know the payment will need to be 1167.15 a month, I just need to figure out how to make the java work and get the correct result.
I don't need it to show any more than 1 loan term (360 months),
1 interest rate (5.75%), and 1 initial loan amount ($200,000). If I don't need to use arrays, that is fine with me, I can do it some other way.
Is there a simpler way to make this work, without using the arrays?
Thanks in advance again.
IKE
This post has been edited by coyboss: 09 January 2011 - 07:57 AM
#9
Re: Where do I start
Posted 09 January 2011 - 08:08 AM
double interstRate = 5.75;
Anyway, before you start coding, prepare the formula.
consider exactly what data, variables you need.
Then, when you have all information, translate the formula into code.
You can use this site:
http://www.fonerbooks.com/interest.htm
#10
Re: Where do I start
Posted 09 January 2011 - 08:31 AM
int loanAmt = 200000; // principal loan amount
int[] loanTerm = {360}; // loan term for 30 years
int loanYears; // indicates the loan term in years
From the code to make it work right?
Thanks in advance.
IKE
#11
Re: Where do I start
Posted 09 January 2011 - 08:40 AM
What I meant is that these variables should'nt be arrays.
Did you check the link I provided? It contains the formula, and an explanation about it.
what you have to do in the code, is declare all the necessary variables, assign values to them, and pass their value to the formula.
The result from that formula is what you are looking for.
#12
Re: Where do I start
Posted 09 January 2011 - 09:22 AM
Let me clarify something here.
I don't understand the first thing about coding or Java.
I am newer than new to this all.
I tried to use the formula form the link you privided, but you have to be a MATH MAJOR to understand it.
I got something WAY out in space from what I know the correct payment to be.
From what I understand the numbers above are the correct figures, but I don't understand how to make them show up right.
#13
Re: Where do I start
Posted 09 January 2011 - 10:26 AM
Loan ammount is $200000
Interest rate 5.57% / 12 = .0048
Number of payment is 30 * 12 for 360
The total of the loan payment will be $420174
Now how do I plug these into the java so I can get the results I need of
1167.15 monthly payment
What do I need to replace with what figures?
Thanks in advance.
IKE
#14
Re: Where do I start
Posted 09 January 2011 - 12:40 PM
OK I tried the code they way I understand from your explanations
Here is my code
import java.text.DecimalFormat;
public class Mortgage
{
public static void main(String args[]) throws Exception
{
// declare and construct variables
double loanAmt = {200000}; // principal loan amount
double loanTerm = {360}; // loan term for 30 years
double loanYears = {30}; // indicates the loan term in years
double intRate = {5.75}; // interest rate 30 years
double newIntRate; // displays interest rate calculation
double monthlyPay = 0; // displays monthly payment calculation
DecimalFormat money = new DecimalFormat("$0.00"); // displays mortgage in decimal format
DecimalFormat money2 = new DecimalFormat("0.00%"); // displays interest rate in decimal format
// displays messages to indicate purpose of program and conditions in the console window
System.out.println();
System.out.println("Welcome to the Mortgage Payment Calculator");
System.out.println();
System.out.println("This program will calculate the mortgage payments for a $" + loanAmt);
System.out.println("with the following loan terms and interest rates: 30 years @ 5.75%");
System.out.println();
System.out.println("The results are as follows:");
int i;
for (i = 0; i <= 2; i++)
{
intRate[i] = (intRate[i] /12 * .01);
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
// displays results for loan in the console window
System.out.println();
System.out.println("The mortgage payment for a $" + loanAmt + " loan for " + loanTerm[i] + " years at");
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))));
}
}
}
But when I compile it I get the following errors.
****************************************************
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:8: illegal initializer for double
double loanAmt = {200000}; // principal loan amount
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:9: illegal initializer for double
double loanTerm = {360}; // loan term for 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:10: illegal initializer for double
double loanYears = {30}; // indicates the loan term in years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:11: illegal initializer for double
double intRate = {5.75}; // interest rate 30 years
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:30: array required, but double found
intRate[i] = (intRate[i] /12 * .01);
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:30: array required, but double found
intRate[i] = (intRate[i] /12 * .01);
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:31: array required, but double found
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:31: array required, but double found
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:31: operator + cannot be applied to int,<any>
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:31: array required, but double found
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:31: pow(double,double) in java.lang.Math cannot be applied to (<nulltype>,int)
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:35: array required, but double found
System.out.println("The mortgage payment for a $" + loanAmt + " loan for " + loanTerm[i] + " years at");
^
C:\Users\owner\Documents\java Parctice\Wk2 assignment\Mortgage.java:36: array required, but double found
System.out.println("a " + (money2.format(intRate[i]) + " interest rate = " + (money.format(monthlyPay))))
***************************************
So now what am I doing wrong?
I am so confused on all this, and don't understand how to figure it all out.
I understand that the // is not supposed to have any effect on the coding or how Java interprets the coding. So what is causing the issues with the code?
IKE
This post has been edited by coyboss: 09 January 2011 - 12:42 PM
#15
Re: Where do I start
Posted 09 January 2011 - 05:31 PM
double loanAmt = 200000; // principal loan amount double loanTerm = 360; // loan term for 30 years double loanYears = 30; // indicates the loan term in years double intRate = 5.75; // interest rate 30 years
Also, don't refer to their indices, so,
intRate[i] should be intRate.
Now to the formula.
M = P [ i(1 + i)^n ] / [ (1 + i)^n - 1]
M is the monthly paymrnt you look for.
P in your case is loanAmt
i = 0.0575/12
n = 12 * 30
just assign values in the formula.
For more info, go to the link I provided earlier.
Hope it helps
This post has been edited by japanir: 09 January 2011 - 05:32 PM
|
|

New Topic/Question
Reply



MultiQuote




|