Link to home
Start Free TrialLog in
Avatar of ejuste
ejuste

asked on

Mortgage Calculator

I'm having problem with this java code, can someone take a look at it and help me find what is wrong?


import java.io.*;
import java.util.Date;
import java.text.DecimalFormat;

public class Mortgage
{
public static void main(String[] args);
{
Date currentDate = new Date();
//Date constructor
DecimalFormat decimalPlaces=new DecimalFormat("0.00");

//declaring variables
final double PRINCIPAL = 200000;
final double INTEREST = .0575;
final double TERM = 12*30;

//declaring variables

final double MONTHLY = (((PRINCIPLE*(INTEREST/12))/(1-Math (1+(INTEREST/12))-(TERM)))

//displaying variables
System.out.println("\t\t" + currentDate);
System.out.println("\t\tPrinciple or Loan Amount: " + PRINCIPLE);
System.out.println("\t\tInterest Rate: " + INTEREST);
System.out.println("\t\tThe Term of Loan (in months): " + TERM);
System.out.println("\t\tThe Monthly Payment is: " + MONTHLY);
System.out.println(decimalPlaces.format(MONTHLY));
}
}
Avatar of dbkruger
dbkruger

You have to define the problem. If it doesn't compile, post the error. If it gives the wrong answer, state what's wrong.
Avatar of ejuste

ASKER

My problem is it does not compile. This is the error message  I got.

A:\Mortgage Calculator.java:21: ')' expected
^
1 error

Tool completed with exit code 1
ASKER CERTIFIED SOLUTION
Avatar of ricjava
ricjava

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A few other problems as well...

Your spelling of PRINCIPAL is inconsistent... the definition is spelled PRINCIPAL and it's usage is spelled PRINCIPLE
There is no method called Math...
You're calculations don't work...

The following calculations should clean this up...

final double MONTHLY = (PRINCIPAL * (INTEREST/12)) + (PRINCIPAL / TERM);
Avatar of ejuste

ASKER

Thank you guys for your comments they help a lot.
I have a new problem that I would like someone to help me debug.

This the code, and this is the error message I got:

A:\Mortgage Calculator.java:16: class MortgageCalculator is public, should be declared in a file named MortgageCalculator.java
public class MortgageCalculator
       ^
1 error

Tool completed with exit code 1
-------------------------------------------------------------------------------

/*
Modify the mortgage program to display the mortgage payment amount.
Then, list the loan balance and interest paid for each payment over
the term of the loan.  The list would scroll off the screen, but use loops to display
a partial list, hesitate, and then display more of the list.  Do not use a graphical
user interface.  Insert comments in the program to document the program.
*/




import java.io.*;
import java.util.Date;
import java.text.DecimalFormat;

public class MortgageCalculator
{
      public static void main(String[] args)
      {
            Date currentDate = new Date();

            //Date constructor

            DecimalFormat decimalPlaces=new DecimalFormat("0.00");

            //declaring variables

            final double PRINCIPAL = 200000;
            final double INTEREST = .0575;
            final double TERM = 12*30;

            //declaring variables

            final double MONTHLY = (PRINCIPAL*(INTEREST/12))/(1-Math.pow (1+INTEREST/12,-TERM));

            //displaying variables

            System.out.println("\t\t" + currentDate);
            System.out.println("\t\tPrinciple or Loan Amount: " + PRINCIPAL);
            System.out.println("\t\tInterest Rate: " + INTEREST);
            System.out.println("\t\tThe Term of Loan (in months): " + TERM);
            System.out.println("\t\tThe Monthly Payment is: " + MONTHLY);
            System.out.println(decimalPlaces.format(MONTHLY));

            double balance = PRINCIPAL;

            // calculate monthly interest and principal
            double monthlyInterest = (balance * (INTEREST/12));
            double monthlyPrincipal = (MONTHLY - monthlyInterest);

            // format column headers for results to be displayed

            System.out.println("\n\nPayment\tPrincipal\t\tInterest\t\tBalance");
            System.out.println("---------\t---------\t---------\t---------");

            int lines = 0;

            for(int i=0; i<TERM; i++)
            {
                  // information to display
                  System.out.println((i+1) + "\t\t" + decimalPlaces.format(monthlyPrincipal) +
                        "\t\t" + decimalPlaces.format(monthlyInterest) +
                        "\t\t" + decimalPlaces.format(balance));

                  // calculate interest and principal payments
                         monthlyInterest = (balance * (INTEREST/12));
                         monthlyPrincipal = (MONTHLY - monthlyInterest);

                  //reduce balance
                  balance -= monthlyPrincipal;

                  // pause every 25 lines
                    if(lines == 25)
                    {
                        lines = 0;

                        try
                        {
                              Thread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        {

                        }
                  }
                  else
                  {
                        lines++;
                  }

                }


      }

}



It looks like the name of the Java source file for this class is "Mortgage Calculator.java" instead of "MortgageCalculator.java" (note the space).

The name of the Java file must match exactly (including case) the name of the class contained within it.
Avatar of ejuste

ASKER

Thanks Dean,

I tried to make the correction that you suggested, but this is the compile errors I have;

A:\Mortgage Calculator.java:24: '{' expected
public class Mortgage Calculator.java
                      ^
A:\Mortgage Calculator.java:105: '}' expected
^
2 errors

Tool completed with exit code 1
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial