Call and write methods with parameters and return


Objectives include:

Call and write methods with parameters and return values.
Use the String and Scanner classes.
Use if statements to validate input and control assignments.
Hand-in Requirements

All projects and laboratories will be submitted electronically through Blackboard.
Zip up your entire project directory to submit as the source.
(Right click on the project folder and follow 7-Zip > Add to "project2.zip".)
The project folder should include the following two files:

LoanPaymentCal.java
LoanPaymentCalOutput.txt
Sample Output

Here is an example of what your output should look like for a loan amount of $300000 for 2 years.
Project 2 written by Steve Robbins

Enter the loan amount and loan duration in years, for example
amount 3000 years 2
[DrJava Input Box]
Loan amount: $300000.00
Loan period: 2 years
Loan rate: 3.5%
Monthly payment: $12960.82
Month Balance Payment Remaining
1 300875.00 12960.82 287914.18
2 288753.93 12960.82 275793.12
3 276597.51 12960.82 263636.70
4 264405.64 12960.82 251444.82
5 252178.20 12960.82 239217.38
6 239915.10 12960.82 226954.28
7 227616.23 12960.82 214655.42
8 215281.50 12960.82 202320.68
9 202910.78 12960.82 189949.97
10 190503.99 12960.82 177543.17
11 178061.00 12960.82 165100.19
12 165581.73 12960.82 152620.91
13 153066.06 12960.82 140105.24
14 140513.88 12960.82 127553.06
15 127925.09 12960.82 114964.28
16 115299.59 12960.82 102338.77
17 102637.26 12960.82 89676.44
18 89938.00 12960.82 76977.18
19 77201.70 12960.82 64240.88
20 64428.25 12960.82 51467.44
21 51617.55 12960.82 38656.73
22 38769.48 12960.82 25808.67
23 25883.94 12960.82 12923.12
24 12960.82 12960.82 0.00
Total payment amount: $311059.60
Tasks

Create a directory called project2. Write a program called LoanPaymentCal and save it in project2. The program will print
Project 2 written by YOURNAME
Prompt the user for an amount and a number of years. See below for details.
Print the amount and number of years.
Call a method loanRate that takes an input loan amount as a parameter and returns an interest rate. If a jumbo loan (a loan amount is greater than or equal to $350,000), use 4.0%. For a large loan (not jumbo but greater than or equal to $100,000), use 3.5%. Otherwise, the interest rate is 3.0%. No print statements should be included in this method. The main method will print the loan rate.
Call a method loanMonthlyPayment that will take three parameters: the loan amount, rate, and number of years. It will return a monthly payment amount. This method should not print anything. The main method should print the monthly payment.
Call a method loanTotalPayment that will take 4 parameters: the loan amount, monthly payment amount, rate and the number of years. It will return the total payment amount after printing a table like the one in the sample output. The main method should then print the total payment amount.
Details

The Scanner

You will need to use Scanner to obtain input from the keyboard. You should declare a Scanner variable named console at the beginning of your main method. Only the main method will input from the keyboard.
Input Format

Inputting and verifying the input data is worth 5 points (25%). When writing you program, assume that two numbers will be entered, a double value giving the loan amount and an integer giving the loan duration in years. Use the console.nextDouble() and console.nextInt() to read in the values. This will give you 1 point out of 5 for this part of the assignment. Get the rest of the assignment working before coming back to this and improve the input handling of your program.
To get the maximum of 5 points on this part of the assignment, your program must accept input as 4 tokens and verify that the input is in an appropriate format.
The first token will be either "amount" or "years" (without the quotes). If the first token is "amount", the next is the amount of the loan and should be read in using console.nextDouble(). If the first token is "years", the next token is the number of years and should be read in with console.nextInt(). If the first token was "amount", the third token should be "years" and the number of years is read in as the fourth token using console.nextInt(). If the first token was "years", the third token should be "amount" and the amount should be read in with console.nextDouble(). Any other tokens are considered invalid input.
If invalid input is detected, the program should print an appropriate message, explaining how the input is invalid. At that point it should exit the program. You can exit the program by calling return.
You can get up to 4 points by following the above description. To obtain full credit you need to also validate the numeric input before you call console.nextInt() or console.nextDouble(). You can do this using the following Scanner methods:
boolean hasNextInt(): returns true if the next token is a valid int.
boolean hasNextDouble(): returns true if the next token is a valid double.
A typical use of these might look like this:
if (!console.hasNextInt()) {

System.exit(0);
}
int value = console.nextInt();
Outputting two decimal places

Use the method twoPlaces described in Project 1 to produce numbers with 2 decimal places. Use of this is not optional in this assignment.
Lining up columns

Do not use tabs to line up the columns. Instead, write a method called padOnLeft which takes 2 parameters, a String, s and an int, n. If the length of s is at least n, return s. Otherwise, return a string of length n obtained by putting the appropriate number of blanks at the front of s. You can create this string with a for loop.
Use this to create the columns of your table with the decimal points in each column lined up.
Calculating monthly payment

Given a loan amount, b, an monthly interest rate, r, and a loan period in months, n, you can compute monthly payment, p, using the following formula:
p = b * r
1 - (1+r)-n
Calculating loan balance

When you take out a loan, you are charged interest monthly based on an annual interest rate. If the annual rate is r, the monthly rate is r/12. The annual rate is usually represented as a fraction, a number between 0 and 1, but the interest rate will be entered as a percentage. If the annual interest rate is 15%, the annual rate will be 15/100 = .15.
If the balance at the beginning of the month is b, the balance at the end of the month before a payment is made is b + b*r/12. After the payment is made, the balance is reduced by the payment.
Test the program with the inputs shown in the sample output above and make sure your program produces the correct values. You can use tabs to line up the columsn while debugging, but in the final program you should use the padOnLeft method described above.
When you think your program is working correctly, test the program with an amount of $10000 for 2 years. Save the output to a file called LoanPaymentCalOutput.txt in the project2 directory.
Rubric

For the main method data input and output. See the details of the assignment.
If your program has a method loanRate() that takes a parameter and returns a rate correctly.
If your program has a method loanMonthlyPayment() that takes parameters and returns a monthly payment amount correctly.
If your program has a method loanTotalPayment() that takes parameters and returns a total payment amount correctly.
If the method loanTotalPayment() prints month, monthly principle, payment and remaining information per month correctly including with two decimal places and in appropriate columns.
One point for each of the following:
If the main method of your program prints "Project 2 written by [...]" and calls the three other methods.
If your submission was a Zip file named project2.zip containing two files called LoanPaymentCal.java and LoanPaymentCalOutput.txt.
If your program contains a comment that describes what the program does and contains a descriptive comment for each method.
If your program is indented properly.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Call and write methods with parameters and return
Reference No:- TGS0767033

Expected delivery within 24 Hours