Question
METHODS Not sure if you all noticed or not but in our previous 2 labs we had to code the same logic in multiple places.
METHODS
Not sure if you all noticed or not but in our previous 2 labs we had to code the same logic in multiple places. This makes it a long program in our main() method, more confusing to read, difficult to change if we need to change something in the logic in multiple places, and not re-usable. Let's instead modularize our bankAccount program by putting some of the duplicate logic into a module (method) that we can then call from our main() method.
Name and submit bankLoanMethod.java
Highlighted in a subsection of our flowchart are duplicate code, yellow = determining interest rate, blue = displaying formatted output of Loan Account information, let's turn each of these into a module and then refer to the updated flowcharts. Also, let's move our Selection structure to a module that determines Interest Rate
UPDATED FLOWCHART AFTER MODULARIZING OUR LOGIC, all the below is in our main() module, where within main() we make a call to methods getInterestRate(), calcMonthlyPayment() and displayAccount() (refer to section 6.3 Calling a Method)
we moved our Selection Structure to our Module getInterestRate() module
my code my last lab
import java.util.Scanner; import java.text.DecimalFormat;
public class bankLoanLoop {
public static void main (String[] args) { //infinite loop while(true){ double L, MonthlyPayment, TotalAmountOfLoan, TotalInterest, InterestRate, rand; int i, n, acctNo; String firstName, lastName, loanType; DecimalFormat df = new DecimalFormat("##.##%"); Scanner input = new Scanner(System.in); System.out.println(" Enter customer's first name: "); firstName = input.nextLine(); // using equals mehtod to check if the entered firstName was "STOP" if(firstName.equals("STOP")){ //if firstName entered was STOP then break the loop break; } System.out.println("Enter customer's last name: "); lastName = input.nextLine(); System.out.println("Enter loan type: "); loanType = input.nextLine(); System.out.println("Enter Loan Amount: "); L = input.nextDouble(); System.out.println("Enter Length of Loan in years: "); i = input.nextInt(); n = 12; acctNo = (int) (Math.random()*1000+1); if(loanType.equalsIgnoreCase("Variable")) { /ew logic if(L>=100) { rand = (Math.random()*10+5); InterestRate=(Math.max(rand, (L/(i*12*10))))/100; MonthlyPayment=L * ((InterestRate) * Math.pow(( 1 + InterestRate), i * n))/((Math.pow(( 1 + InterestRate), i *n))-1); System.out.println(firstName + " " + lastName); System.out.println("Acct No: " + acctNo); System.out.println(loanType + " loan for " + i + " years at an interest rate of " + df.format(InterestRate)); System.out.printf("Loan Amount: $%.2f " , L); System.out.printf("Monthly Payments wull be: $%.2f " , MonthlyPayment); } else { System.out.println("No account opened for " + firstName + " " + lastName + ", requires minimum $100.00 loan amount for variable loan."); } } else { if (L main |getinterestRate | calcMonthlyPayment | DisplayAccount we move our calculation to calcMonthlyPayment() module main | getinterestRate and finally our displayAccount() Module When Defining your methods (section 6.2, Figures 6.1) the method headers (signatures) should be as follows - you will code the logic within each module: public static double getinterestRate(String LoanType, double LoanAmt, int years) public static double calcMonthlyPayment(double LoanAmt, double interest, int n, int years) public static void displayAccount(String first_name, String last_name, int AccountNo, String LoanType, double LoanAmt, int time, double interestRate, double MonthlyPayment), "*hint: YOU CAN CALL the calcMonthlyPayment() module FROM displayAccount() *+* see Figure 6.4 for example of memory "Call Stack" of methods. the updated displayAccount() module would look like: @Overloaded public static void displayAccount(String first_name, String last_name, int AccountNo, String LoanType, double LoanAmt, int time, double interestRate, int n). main | getinterestRate | calcMonthlyPayment DisplayAccount PUT Loantypef PUT "Legn Ameuset" PUT "Timo of Louz in years " if PUT "Intarostinas" + intarostiatof updated flowchart would look liks main |qeinterestRaie | cakklonthlyPayment| DisplayAceount| BCT"LoasBropmes Ended' End
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started