Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

java - Calling a method that only prints out a "loan statement"

I am doing an assignment for class and we just started making our own methods and what I thought seemed easy enough has become extremely frustration and hoping you can help me wrap my head around it.

First things first and the assignment I am trying to complete is this: make a modular program to calculate monthly payments, seems easy but the few restrictions on this question is as follows

The main method should:

Ask the user for

  • the loan amount
  • the annual interest rate ( as a decimal, 7.5% is 0.075 )
  • the number of months

And

  • call a method to calculate and return the monthly interest rate (annual rate/12)
  • call a method to calculate and return the monthly payment
  • call a method to print a loan statement showing the amount borrowed, the annual interest rate, the number of months, and the monthly payment.

I have gotten to the end of just printing out the loan statement but cant for the life of me the proper way to call it, and make it show up once I run the program :/ so if you can help me understand how its done I would greatly appreciate it.

(I realize that there are probably other mistakes in my code but for right now I would rather just focus on what I need to get done) import java.util.Scanner; public class LoanPayment {

/**
 * The main method declares the variables in program while getting the user
 * info of amount loaned, interest rate of the loan, and the loans duration.
 * 
 * The main method also calls other methods to calculate monthly interest 
 * monthly payments and the output of the loan statement
 */  
public static void main(String[] args) 
{
   // declare variables
    double interest; // interest attributed to the loan
    double mInterest; // loans interest  divided by 12
    int time; // how long the loan was taken out for
    double principle; // the amount borrowed
    double mPayment; // how much is to be paid each month
    double loan;


   // initate new scanner class
    Scanner keyboard = new Scanner(System.in);

   // get user input/information
    System.out.println("Hi, Please enter the loan amount here:");
    principle = keyboard.nextDouble();

    System.out.println("Thanks, now what is the annual interest rate in decimal notation" + 
            "(example: 7.5% is 0.075:");
    interest = keyboard.nextDouble();

    System.out.println("now please put in the number of months the loan was taken out for");
    time = keyboard.nextInt();

   // call method to calculate and return monthly interest rate
    mInterest = calcMInterest( interest );

   // call method to calculate and return the monthly payment
    mPayment = calcMPayment (mInterest, principle, time);

    // call method to print loan statement  


} // end main ()
/******************************************************************************/

// this class calculates and returns the monthly interest on the loan
public static double calcMInterest(  double interest )
{ 
    double mInterest;

    mInterest = (interest / 12);

    return mInterest; 

} // end calcMInterest
/******************************************************************************/

// this class calculates and returns the monthly payment
public static double calcMPayment (double mInterest, double principle, int time)
{
    double mPayment;
    mPayment = (mInterest * principle) / (1-(1+ Math.pow(mInterest,-time)));

    return mPayment;
} // end calcMPayment
/******************************************************************************/

// this class prints a loan statement showing the amount borrowed
// and the amount borrowed, the annual interest rate, the number of months
// and the monthly payment
public static void loanStatement(double principle, double interest, int time, double mPayment)
{
   System.out.println(" principle is" + principle);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If // call method to print loan statement is all you have left to do, then this is what you need on the line below it:

loanStatement(principle, interest, time, mPayment);

And it should work fine.

Your other methods have non-void return types, so you put someVariable = yourMethod(yourArguments) in order to accept the return value. However, loanStatement has a void return type. You don't need to do this. You can call it simply as I showed above and it will execute the code in the method.

Though, my personal preference would be to change loanStatement to a String return type and put the print statement in main and print the return of loanStatement. Methods that return Strings almost as easily and are more flexible for future use (for example, if you wanted to allow your program to also write to file, you need two loanStatement methods, or to completely rework loanStatement).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...