Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Object Oriented program, no output

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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.

    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:
    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
     
    }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Object Oriented program, no output

    You didn't call the display method:

    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.

    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 ):

    public double calculations()
    {
         double montlyPay;
         // the rest of the method as you had
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    boardbreaker (November 17th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up 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
    ("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!!!

Similar Threads

  1. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  2. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  3. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM
  4. Redirect error and output stream using java program
    By leenabora in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 16th, 2009, 04:12 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM