Object Oriented program, no output
Hello,
this is a homework assignment, but it has already been turned in. I can't figure out why it does not produce any output. The code compiles fine, but when I run it all I get is "press any key to continue." This tells me the program ran, but my output isn't.:confused:
I've tried moving things around, but if I move my println statements to main, I get errors about a non-static value in a static method, so I'm stumped.
Can anyone give me a shove in the right direction towards fixing this thing? I want to learn what I did wrong so I can fix it and move forward on my next assignment.
Thanks!
Here's the code:
Code :
import java.io.PrintStream;
public class MortgageCalc
{
private double loanAmount=0;
private int years=0;
private double interestRate=0;
public double monthPay=0;
public MortgageCalc (double loanAmount, int years, double interestRate)
{
this.loanAmount = loanAmount; // set the variables
this.years = years;
this.interestRate = interestRate;
}
public double calculations()
{
monthPay= ((loanAmount*(interestRate/12)) / (1 - 1 /Math.pow((1 + interestRate*12), years)));// calculation for monthly payment
return monthPay;
}
public void display()
{
System.out.println("Mortgage Payment Calculator"); // should print out the title, but doesn't
System.out.println("The amount of the loan $200,000"); // should print out the loan amount, but doesn't
System.out.println("The term of the loan is 30 years"); // should print out the loan term, but doesn't
System.out.println("The interest rate of the loan is 5.75%"); //should print out the loan rate, but doesn't
System.out.println(); // should leave a blank line, but doesn't
System.out.println("The monthly payment is: $" + monthPay ); //should print out the monthly payment, but doesn't
System.out.println();// should leave a blank line, but doesn't
}
public static void main(String[] args)
{
MortgageCalc mortgageCalc = new MortgageCalc(200000,30,0.0575);// create the values for the calculation
}
}
Re: Object Oriented program, no output
You didn't call the display method:
Code :
public static void main(String[] args)
{
MortgageCalc mortgageCalc = new MortgageCalc(200000,30,0.0575);
mortgageCalc.display();
}
Also, it's very important to realize that just because you want Java to do something, it won't. So, your display method will always display the text as is, without the properly calculated values.
Code :
public void display()
{
System.out.println("Mortgage Payment Calculator");
System.out.println("The amount of the loan $" + loanAmmount);
System.out.println("The term of the loan is " + years + " years");
System.out.println("The interest rate of the loan is " + interestRate*100 + "%");
System.out.println();
System.out.println("The monthly payment is: $" + calculations());
System.out.println();
}
As a general OOP guideline, the monthlyPay variable shouldn't be declared as an object field/variable, but rather as a method variable (technically how you have it is legal to the compiler and will give you the correct results, but it takes more memory, can cause some issues because of the public declaration, and it's also just not the OOP way :P ):
Code :
public double calculations()
{
double montlyPay;
// the rest of the method as you had
}
Re: Object Oriented program, no output
THANK YOU!! I believed that it had to be something simple and a newbie error, but I could not figure out what.
I had tried using an object in the calculation using
Code :
("The monthly payment is: $\n", new Object[] { Double.valueOf(calculations())} );
, but someone at another Java forum (which shall be nameless) said it was overkill and not to do it. Oh well, I'm sure it was good advise, I probably just misunderstood.
Thank you so much! My instructor in the course has not been of much (OK, any) assistance and I was wondering if I was totally off base, or even in the ballpark.
I made the changes you suggested and it runs and displays correctly!!!
Thanks again!!! ^:)^