Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to separate class into multiple classes 3

Status
Not open for further replies.

LBryant777

IS-IT--Management
Mar 24, 2004
23
US
I started this out as one class, but I need to separate it out into different classes with constructors, getters and setters. I've gotten this far and now I'm lost. Plus now I get an error:

java.lang.Error: Unresolved compilation problem:
Syntax error on token "principal", ".", "[" expected

at school.MortgageCalculatorMain.main(MortgageCalculatorMain.java:30)
Exception in thread "main"


Here is the code for the different files:

MortgageCalculatorMain:
Code:
import java.io.*;
import java.text.NumberFormat;

public class MortgageCalculatorMain {
	
	public static void main(String[] args) throws IOException {//add throws Exception for keyboard input
		final double principal = 200000;
		final double interestRate[] = {5.35,5.5,5.75};
		final double termInMonths[] = {84,180,360};
		NumberFormat currency = NumberFormat.getCurrencyInstance(); //will format payment in currency form
	
//		MortgageCalculator value = new MortgageCalculator();
//				value.doCalculation();	
		for (int i = 1; i <= termInMonths.length; i++){ //prints out number of payments to be made
			MortgageCalculator value = new MortgageCalculator();
			MortgageCalculator.doCalculation(double principal, double interestRate[i], double termInMonths[i]);
			}
		}		
}

Code from MortgageCalculator:
Code:
import java.io.*;
import java.text.NumberFormat;

public class MortgageCalculator {
	
	 //declare and initialize the variables

 

	 //formula that calculates monthly payment
	 public MortgageCalculator
	 {
	 	public static void doCalculation(double principal, double InterestRate, double termInMonths)
	
		double PeriodicRate, MonthlyIntPymt, MonthlyPrincipalPymt, Balance, LoanAmount, payment = 0;
		
		
	PeriodicRate = interestRate/(100*12);
	
	 LoanAmount = principal+(principal*(interestRate/100)); //total amount to be repaid
	 payment = principal*(PeriodicRate / (1 - Math.pow(1 + PeriodicRate, -termInMonths))); //Mortgage Payment Amount
	 MonthlyIntPymt = principal*PeriodicRate; //calculates initial monthly interest payment
	 MonthlyPrincipalPymt = payment-MonthlyIntPymt; //calculates initial payment against principal loan
 
	 //print out results to screen
 
	 System.out.println("Principal: " + currency.format(principal));
	 System.out.println("Interest Rate: " + interestRate+"%");
	 System.out.println("Mortgage Term: " + Math.round(termInMonths/12) + " years");
	 System.out.println("Payment: " + currency.format(payment) + "\n\n\n");
	 
 
 	  int pymtNumber; 
	  int counter = 0;
 
	 System.out.println("Payment #" + "\t" + "Interest" + "\t" + "Principal" + "\t" + "Balance" + "\n");

	   System.out.print(pymtNumber + "\t\t\t" + currency.format(MonthlyIntPymt)); //Interest payment
	   System.out.print("\t\t" + currency.format(MonthlyPrincipalPymt)); //Payment against principal
	   System.out.println("\t\t\t" + currency.format(principal-MonthlyPrincipalPymt)); //New Loan Balance
		
	  //next 3 formulas recalculate variables for next loop
	  principal = principal-MonthlyPrincipalPymt;
	  MonthlyIntPymt = principal*PeriodicRate;
	  MonthlyPrincipalPymt = payment-MonthlyIntPymt;
		
	  //for screen printing purposes
		  counter++;
		  if (counter==50)// pause on every 50 lines
		  {
		  System.out.println("\nPress the RETURN key to continue...\n");
		  byte[] buffer = new byte[50];
		  System.in.read(buffer);
		  counter = 0;
		  }
	 	}
	}

Can someone help me? I really have a desire to learn and internalize this for the longhaul and not just for right now...thanks in advance!
 
// I set the counter==1 to pauge page, readChar class is modified from a programmer code in tektips forum
import java.io.*;
import java.text.NumberFormat;

public class MortgageCalculatorMain {

public static void main(String[] args) throws IOException {//add throws Exception for keyboard input
final double principal = 200000;
final double interestRate[] = {5.35,5.5,5.75};
final double termInMonths[] = {84,180,360};
MortgageCalculator value[] = new MortgageCalculator[3];
NumberFormat currency = NumberFormat.getCurrencyInstance(); //will format payment in currency form

// MortgageCalculator value = new MortgageCalculator();
// value.doCalculation();
for (int i = 0; i < termInMonths.length; i++){ //prints out number of payments to be made
value = new MortgageCalculator(principal,interestRate,termInMonths);
}
}
}
//*******************
import java.text.NumberFormat;
public class MortgageCalculator
{

//declare and initialize the variables



//formula that calculates monthly payment
public MortgageCalculator(double principal, double interestRate, double termInMonths)
{

double PeriodicRate, MonthlyIntPymt, MonthlyPrincipalPymt, Balance, LoanAmount, payment = 0;

NumberFormat currency = NumberFormat.getCurrencyInstance();
PeriodicRate = interestRate/(100*12);

LoanAmount = principal+(principal*(interestRate/100)); //total amount to be repaid
payment = principal*(PeriodicRate / (1 - Math.pow(1 + PeriodicRate, -termInMonths))); //Mortgage Payment Amount
MonthlyIntPymt = principal*PeriodicRate; //calculates initial monthly interest payment
MonthlyPrincipalPymt = payment-MonthlyIntPymt; //calculates initial payment against principal loan

//print out results to screen

System.out.println("Principal: " + currency.format(principal));
System.out.println("Interest Rate: " + interestRate+"%");
System.out.println("Mortgage Term: " + Math.round(termInMonths/12) + " years");
System.out.println("Payment: " + currency.format(payment) + "\n\n\n");


int pymtNumber=0;
int counter = 0;

System.out.println("Payment #" + "\t" + "Interest" + "\t" + "Principal" + "\t" + "Balance" + "\n");

System.out.print(pymtNumber + "\t\t\t" + currency.format(MonthlyIntPymt)); //Interest payment
System.out.print("\t\t" + currency.format(MonthlyPrincipalPymt)); //Payment against principal
System.out.println("\t\t\t" + currency.format(principal-MonthlyPrincipalPymt)); //New Loan Balance

//next 3 formulas recalculate variables for next loop
principal = principal-MonthlyPrincipalPymt;
MonthlyIntPymt = principal*PeriodicRate;
MonthlyPrincipalPymt = payment-MonthlyIntPymt;

//for screen printing purposes
counter++;
if (counter==1)// pause on every 50 lines
{
System.out.println("\nPress the RETURN key to continue...\n");
char c = 0;
readChar readCharObj = new readChar();
while(c!=10)
{
c = readCharObj.getch();
}
counter = 0;
}
}
}
//*****************
// readChar.java
import java.io.IOException;
class readChar
{
public char getch()
{
try
{
return (char) System.in.read();
}
catch (IOException e)
{
System.out.println("error de io "+e);
}
catch(Exception e)
{
}
return 0;
}
}
 
First, replace
Code:
MortgageCalculator.doCalculation(double principal, double interestRate[i], double termInMonths[i]);
with
Code:
MortgageCalculator.doCalculation(principal, interestRate[i] , termInMonths[i]);
Second, you probably want to replace it with :
Code:
value.doCalculation(principal, interestRate[i] , termInMonths[i]);
but to be able to do that, you must change the method "doCalculation" to a non-static. So replace
Code:
public static void doCalculation(...)
with
Code:
public void doCalculation(...)
And probably you want the method to return a value...
 
Ok, I applied the changes that hologram gave me (haven't gotten to prosper's yet...) and now I get a error message of:

java.lang.Error: Unresolved compilation problem:
The method doCalculation(double, double, double) is undefined for the type MortgageCalculator

at school.MortgageCalculatorMain.main(MortgageCalculatorMain.java:28)
Exception in thread "main"


The error seems to be still pointing to these lines in the corresponding files:

MortgageCalculatorMain:
Code:
			MortgageCalculator.doCalculation(principal, interestRate[i], termInMonths[i]);

MortgageCalculator (the culprit is in bold):
Code:
public class MortgageCalculator {
	
	 //formula that calculates monthly payment
	 public MortgageCalculator() {

[b]	 	public static void doCalculation(double principal, double interestRate, double termInMonths)
	{
[/b]

Maybe I'm missing the mark because I don't understand methods and constructors all that well, but isn't the bold line in the second set of code correctly built as a constructor? And does the class MortgageCalculator need to have values in it? I felt like I was doing well with understanding, but the resource that we have for the class is inadequate (per the instructor), so much of what I am learning is from reading the forums. But trying to apply it into something that was working when it was all in the main class is really confusing me. Let me also say that, when everything was under the main, I only had to deal with a static set of variables; now I need it to calculate several different terms and rates, but with the same principal. Thanks for being patient with me -
 
public class MortgageCalculator() {// constructor return nothing, void is accounted as return thing
public void doCalculation(double principal, double interestRate, double termInMonths)
// do not use static method because you call this method by an object called value
...
}

for (int i = 0; i < termInMonths.length; i++)
{ //prints out number of payments to be made
MortgageCalculator value = new MortgageCalculator();
value.doCalculation(double principal, double interestRate, double termInMonths);

}
 
Please correct the line to this
value.doCalculation(principal, interestRate,termInMonths);
 
In your code :
Code:
public class MortgageCalculator {
    
     //formula that calculates monthly payment
     public MortgageCalculator() {

         public static void doCalculation(double principal, double interestRate, double termInMonths)
    {
...
you define a method "doCalculate" inside a constructor, which is not allowed. Change it to (for example) or remove the constructor if he does nothing :
Code:
public class MortgageCalculator {     
     //formula that calculates monthly payment
     public MortgageCalculator() { [COLOR=red]}[/color] // end of constructor


         public static void doCalculation(double principal, double interestRate, double termInMonths)
    {
...
And remove a } at the end to make the { } match.


 
I've gotten much closer - thanks to your help and some more reading to understand the differences in constructors and methods. Now I've hit another point - the amortization table gets to the first line and gives this output:

Payment # Interest Principal Balance

java.lang.ArrayIndexOutOfBoundsException: 3
at school.MortgageCalculatorMain.main(MortgageCalculatorMain.java:25)
Exception in thread "main"


My revised code is as follows:

MortgageCalculatorMain:
Code:
public class MortgageCalculatorMain {

	public static void main(String[] args)
		throws Exception { //add throws Exception for keyboard input
		final double principal = 200000;
		final double interestRate[] = { 5.35, 5.5, 5.75 };
		final double termInMonths[] = { 84, 180, 360 };

		for (int i = 0;	i <= termInMonths.length; i++) { //prints out number of payments to be made
			MortgageCalculator value = new MortgageCalculator();
			value.doCalculation(principal, interestRate[i], termInMonths[i]);
		}
	}
}

MortgageCalculator:
Code:
import java.io.*;
import java.text.NumberFormat;

public class MortgageCalculator {

	//formula that calculates monthly payment
	public MortgageCalculator doCalculation(
		double principal,
		double interestRate,
		double termInMonths)
		throws Exception {

		double PeriodicRate,
			MonthlyIntPymt,
			MonthlyPrincipalPymt,
			Balance,
			LoanAmount,
			payment = 0;
		NumberFormat currency = NumberFormat.getCurrencyInstance();
		//will format payment in currency form

		PeriodicRate = interestRate / (100 * 12);

		LoanAmount = principal + (principal * (interestRate / 100));
		//total amount to be repaid
		payment =
			principal
				* (PeriodicRate
					/ (1 - Math.pow(1 + PeriodicRate, -termInMonths)));
		//Mortgage Payment Amount
		MonthlyIntPymt = principal * PeriodicRate;
		//calculates initial monthly interest payment
		MonthlyPrincipalPymt = payment - MonthlyIntPymt;
		//calculates initial payment against principal loan

		//print out results to screen

		System.out.println("Principal: " + currency.format(principal));
		System.out.println("Interest Rate: " + interestRate + "%");
		System.out.println(
			"Mortgage Term: " + Math.round(termInMonths / 12) + " years");
		System.out.println("Payment: " + currency.format(payment) + "\n\n\n");

		System.out.println(
			"Payment #"
				+ "\t"
				+ "Interest"
				+ "\t"
				+ "Principal"
				+ "\t"
				+ "Balance"
				+ "\n");
		
		for (int pymtNumber = 1; pymtNumber <= 0; pymtNumber++){ //prints out number of payments to be made

		System.out.print(
			pymtNumber + "\t\t\t" + currency.format(MonthlyIntPymt));
		//Interest payment
		System.out.print("\t\t" + currency.format(MonthlyPrincipalPymt));
		//Payment against principal
		System.out.println(
			"\t\t\t" + currency.format(principal - MonthlyPrincipalPymt));
		//New Loan Balance

		//next 3 formulas recalculate variables for next loop
		principal = principal - MonthlyPrincipalPymt;
		MonthlyIntPymt = principal * PeriodicRate;
		MonthlyPrincipalPymt = payment - MonthlyIntPymt;

		//for screen printing purposes
		int counter = 0;
		counter++;
		if (counter == 50) // pause on every 50 lines
			{
			System.out.println("\nPress the RETURN key to continue...\n");
			byte[] buffer = new byte[50];
			System.in.read(buffer);
			counter = 0;
			}
		}
		return null;
	}
}

I think I am really close - I just need to understand what is wrong with one of my loops that causes an exception to occur. The table did work when I just had a single principal, rate and term, so I know it's not the formula. It's when the arrays were added that the problem started. I've appreciated your help to this point!
 
Change :

for (int i = 0; i <= termInMonths.length; i++) {

to :

for (int i = 0; i < termInMonths.length; i++) {

Remember that arrays in java start at subscript 0, not 1, so that an array of size 3 will hold subscripts of 0,1,2 - so a subscript of 3 (ie array.length) is illegal.
 
You guys are the best! It is functioning properly - now I have to get the pause that was working before to work again. I tried to give all of you stars for helping me out. Prosper, for some reason it won't let me give you one, but I will try again later!

Not imperative, but if you could help me figure out why the output won't wait for user input to continue, I would be eternally grateful. But I will still keep plugging along and will post again if I come up with the answer. Thanks to all again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top