Java programing

  • Thread starter milefile
  • 4 comments
  • 597 views
10,832
Does anyone here know Java?

I'm writing a simple Java program for a class. It's a mortgage calculator. The program takes two pieces of input from the user: the monthly payment and the mortage balance. The interest rate is a constant set to 7.49. The program is supposed to output the amount of the payment that goes to interest and the amount that goes to principal (actual debt). This seems easy enough. But I can't seem to figure out why the program needs the loan balance. It is not necessary to to calculate the output, I think. If it were, it seems to me the program would have to be more complicated and include at least one function. I know this is not the case, though, because we haven't even started functions yet. So I did the program and it works okay, but I can't help thinking that there must be a reason why it asks for the loan balance. Can anyone think of a way to use it in this program?

/**
This program calculates the principal
and interest paid on a mortgage based
on the term and balance input by the
user.
*/

public class assignment03
{
//Sets constant value for INTEREST


public static final double INTEREST = 7.49/100;

public static void main(String[] args)
{
//variable declaration

double interest = 0, principal = 0, interestPaid = 0,
monthlyPayment = 0, balance = 0;

//User input here

System.out.println ("Please enter your monthly payment.");
monthlyPayment = SavitchIn.readLineInt();

System.out.println ("Please enter you loan balance.");
balance = SavitchIn.readLineInt();


/**calculates interest and principal based on user input
stored in vaiables*/


interestPaid = monthlyPayment * INTEREST;
principal = monthlyPayment - interestPaid;

//outputs principal and interest paid

System.out.println ("The amount of your payment that goes ");
System.out.println ("toward principal is " + principal);

System.out.println ("The amount of your payment that goes ");
System.out.println ("toward interest is " + interestPaid);

//keeps program open

System.out.println("Press enter key to end program.");
String junk;
junk = SavitchIn.readLine();


}
}
 
Yeah so this is how it comes out then:


/**
This program calculates the interest and principal paid
on a mortgage based on two pieces of inpur from the
user: monthly payment and loan balance.
*/

public class assignment03

{
/**
Sets the interest rate to a constant amount and
performs operations to make it usable in the
program.
*/

public static final double INTERESTRATE = (7.49/12)/100;

public static void main(String[] args)

{

//variable declarations

double principal = 0, interestPaid = 0, payment = 0, balance = 0;

//user prompt for input

System.out.println("Please enter your monthly paymnent.");
payment = SavitchIn.readLineDouble();

System.out.println("Please enter your loan balance.");
balance = SavitchIn.readLineDouble();

/**
calculates interest and principal paid using user input
and constant INTERESTRATE.
*/

interestPaid = balance * INTERESTRATE;
principal = payment - interestPaid;
balance = balance - principal;

//output to user

System.out.println("For your first month you pay $"
+ interestPaid + " in interest ");
System.out.println("and $"+ principal + " toward your principal.");
System.out.println("Your new balance will be $" + balance);

System.out.println("Press Enter key to end program.");
String junk;
junk = SavitchIn.readLine();




}
}
 

Latest Posts

Back