Results 1 to 10 of 10
  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Does this constructor look right?

    Hi I am still getting familiar with the basics behind constructors. I am trying to make a program that computes loan amounts, interest rates, etc. My program works however the answer I'm getting seems a bit crazy. Here is my code:


    public class ComputePayment{


    public double computePayment(
    double loanAmt,
    double rate,
    double futureValue,
    int numPeriods) {
    double interest = rate / 100.0;
    double partial1 = Math.pow((1 + interest),
    - numPeriods);
    double denominator = (1 - partial1) / interest;
    answer = (-loanAmt / denominator)
    - ((futureValue * partial1) / denominator);
    return answer;
    }

    void printAnswer() {
    // loanAmt, rt, futv, numP
    computePayment(4000000,.04,5000000,22);
    System.out.println("The answer is: " +
    answer);

    }

    public static void main(String[] args) {
    ComputePayment pay=new ComputePayment();
    pay.printAnswer();
    }
    }
    I also don't understand money all that well, but my answer is negative, should that be the case, or is my program wrong or did I just enter in the values wrong

  2. #2
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: Does this constructor look right?

    The answer is the monthly amount

  3. #3
    AlexHail is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Does this constructor look right?

    Hello there.

    The code you have written is not a constructor. It is a double return type method. A constructor is a method with no type that has the exact same name of the class it's within and returns nothing.

    A constructor of your class would be:

    Java Code:
    public ComputePayment(){
    
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Does this constructor look right?

    First, please use code tags and not quote tags (Use the # button in the advanced editor). I've done it here to make it more legible (at least for me)

    Java Code:
    public class ComputePayment{
     
     
        public double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) {
        double interest = rate / 100.0;
        double partial1 = Math.pow((1 + interest), 
                        - numPeriods);
        double denominator = (1 - partial1) / interest;
        answer = (-loanAmt / denominator)
                        - ((futureValue * partial1) / denominator);
        return answer;
    }
     
        void printAnswer() {
            //            loanAmt, rt, futv, numP
            computePayment(4000000,.04,5000000,22);
             System.out.println("The answer is: " +
                     answer);
     
        }
     
         public static void main(String[] args) {
             ComputePayment pay=new ComputePayment();
             pay.printAnswer();
         }
    }
    Now, you are not using any constructors in this program (except the implied one). What you have is a method which computes a value and returns the value. But your printAnswer() method does not retrieve the return value from computePayment(). In fact, does this code even compile? It looks like answer is local to printAnswer() is not declared anywhere. Nor have you declared answer in ComputePayment().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: Does this constructor look right?

    ok, that makes sense. So why is my value negative when I say computePayment(4000000,.04,5000000,22);
    Did I put that in wrong or is my method wrong?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Does this constructor look right?

    I have no idea. Did you post your current code? The code shown should not even compile.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    AlexHail is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Does this constructor look right?

    Add
    Java Code:
     double answer;
    Right above double computePayment, and it should compile. Please post the algorithm you are using

  8. #8
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    96
    Rep Power
    0

    Default Re: Does this constructor look right?

    This compiles and gives me a long negative answer.
    Java Code:
    public class ComputePayment extends ConsoleProgram{
        double answer;
        double loanAmt;
        double rate;
        double futureValue;
        int numPeriods;
       
        public double computePayment(
                      double loanAmt,
                      double rate,
                      double futureValue,
                      int numPeriods) {
                    this.loanAmt=loanAmt;
            this.rate=rate;
            this.futureValue=futureValue;
            this.numPeriods=numPeriods;
    
        double interest = rate / 100.0;
        double partial1 = Math.pow((1 + interest), 
                        - numPeriods);
        double denominator = (1 - partial1) / interest;
        answer = (-loanAmt / denominator)
                        - ((futureValue * partial1) / denominator);
        return answer;
    }
        
        void printAnswer() {
            //            loanAmt, rt, futv, numP
            computePayment(400000,.04,500000,24);
             System.out.println("Your montly payments are: " +
                     answer);
             
        }
        
         public static void main(String[] args) {
             ComputePayment pay=new ComputePayment();
             pay.printAnswer();
         }
    }
    The answer is: -37487.787442077475
    Last edited by JosAH; 07-29-2013 at 07:53 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Does this constructor look right?

    code tags, not quote tags; I fixed them for you this time ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    CruelSoulz is offline Member
    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Default Re: Does this constructor look right?

    Constructor should be no return type ( even void ).

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •