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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Programming Exercise Help

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Programming Exercise Help

     
    /*
    Chapter 3 Debugging Assignment
    Programmer: Nicolas
    Date: 18 February 2014
    Program Name: bert.java
    Purpose:
    */
     
    import java.util.Scanner;
     
    public class bert
    {
        public static void main(String[] args)
        { 
            //Declaring Variables
            int price, downPayment, tradeIn, months;
     	double annualInterest;
            String custName;
     
            Scanner reader = new Scanner (System.in);
     
            //Get Input from User
            System.out.println("What is your first name? ");
            custName = reader.next();
            System.out.print("What is the price of the car? ");
            price = reader.nextInt();
            System.out.print("What is the downpayment? ");
            downPayment = reader.nextInt();
            System.out.print("What is the trade-in value? ");
            tradeIn = reader.nextInt();
            System.out.print("For how many months is the loan? ");
            months = reader.nextInt();
            System.out.print("What is the decimal interest rate? ");
            annualInterest = reader.nextDouble();
     
            //Output
    calculatePayment (price, downPayment, tradeIn, annualInterest, months, custName);
        }
     
        public static void calculatePayment(int price, int downPayment, int tradeIn,
                double annualInterest, int months, String custName)
        {
            int interest;
            double loanAmt;
     	double payment;
     
    	//Calculations
    interest = (int)annualInterest / 12;
    loanAmt = price-downPayment-tradeIn;
     
     
    payment = ((1/interest)-(1/(interest*Math.pow(1+interest,months))))/loanAmt;
     
     
    	System.out.print("The monthly payment for " + custName + " is $");
    	System.out.println(payment);
     
        }
    }

    So far all I can gather is it is a problem with the equation and zero. Farther than that I am lost and I just can't seem to figure the rest out. It's the last error I need to fix.

    Help is much appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    a problem with the equation and zero
    Can you give more details about what the problem is? Copy the output, paste it here and add some comments saying what is wrong and show what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    My bad, the error that appeared when compiled and run is:
    "Exception in thread "main" java.lang.ArithmeticException: / by zero
    at bert.calculatePayment(bert.java:53)
    at bert.main(bert.java:38)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    "Exception in thread "main" java.lang.ArithmeticException: / by zero
    at bert.calculatePayment(bert.java:53)
    The error message says that the code on line 53 tried to divide by zero. Since that is not defined the jvm threw an exception.

    The code should test that the divisor is not zero before trying to do a divide.

    What code is on line 53?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    payment = ((1/interest)-(1/(interest*Math.pow(1+interest,months))))/loanAmt;

    That is line 53. It refers back to

    interest = (int) ((double)annualInterest / 12);
    loanAmt = price-downPayment-tradeIn;

    1 is being divided by interest, and I'm assuming that interest is being equal to 0.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    I'm assuming that interest is being equal to 0.
    Use the println() method to print its value so you can see what it is.
    interest = (int) ((double)annualInterest / 12);
    What value for interest do you want from this statement?
    Can you give an example of an annualInterest value and what you want from that as interest.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    I know that interest is appearing as 0.

    The interest rate I need is determined by the annual interest in decimal form presented earlier (in first code) instituted into the second code.

    System.out.print("What is the decimal interest rate? ");
            annualInterest = reader.nextDouble();
    interest = (int) ((double)annualInterest / 12);

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    Can you give an example of the calculation with actual numbers?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    The inputs I used art the following which includes error, shown in output.

    run:
    What is your first name? 
    Ames
    What is the price of the car? 17500
    What is the downpayment? 500
    What is the trade-in value? 1250
    For how many months is the loan? 60
    What is the decimal interest rate? .05
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    	at bert.calculatePayment(bert.java:52)
    	at bert.main(bert.java:38)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 25 seconds)

  10. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Programming Exercise Help

    When you cast a floating point number (float or double) to an integer it will always round down to the next whole number. If your annual interest is 0.05 and you calculate your interest like this:
    interest = (int) ((double)annualInterest / 12);
    then the result will inevitably be 0.
    So either your calculation function is wrong or your input is not acceptable.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    Quote Originally Posted by Cornix View Post
    When you cast a floating point number (float or double) to an integer it will always round down to the next whole number. If your annual interest is 0.05 and you calculate your interest like this:
    interest = (int) ((double)annualInterest / 12);
    then the result will inevitably be 0.
    So either your calculation function is wrong or your input is not acceptable.
    That is basically the point of the exercise. It's a debugging exercise and I can't figure out how to fix this one completely.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    Can you give an example of the calculation with actual numbers?
    For example: 20.0/12 = 1.66667
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    The calculation is where the problem is I believe. You actually answered the same type of question once, where I think the problem was the same..

    http://www.javaprogrammingforums.com...ning-code.html

    run:
    What is your first name? 
    Ames
    What is the price of the car? 17500
    What is the downpayment? 500
    What is the trade-in value? 1250
    For how many months is the loan? 60
    What is the decimal interest rate? .05
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    	at bert.calculatePayment(bert.java:52)
    	at bert.main(bert.java:38)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 25 seconds)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    Can you give an example of the calculation with actual numbers?
    For example: 20.0/12 = 1.66667
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    I'm kind of confused because the calculations don't occur due to error. I believe you helped answer approx. the same problem where I'm still confused.

    http://www.javaprogrammingforums.com...ning-code.html

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    I'm trying to determine what you want that line of code to do. What value of annualInterest is used in the equation? What value do you want to be assigned to the variable: interest? Can you give an example of the equation giving values to those two variables?
    In my example I set annualInterest to 20 and got a value for interest of 1.666667.
    What value are you working with and what do you want for the result?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    We are trying to find the payment you have to make. Interest is suppose to be determined by the annual interest already presented in the questions divided by 12.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    Can you post some sample numbers for the variables: annualInterest and interest?

    For example: 24 and 2
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    For annual interest 0.05, which is then divided by 12 or something for interest I think.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    For annual interest 0.05
    That's a value for annualInterest. Now what is the value for the interest that the code is supposed to compute?

    If you don't know what the code is supposed to do, it is very hard to write a program that gets the correct result.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    Your telling me.. It's a debugging exercise trying to determine the payment by doing some equation in the code. Interest is suppose to come from the input you give, I usually put 0.05 when you run the program and you start with the question inputs.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    Why can't you answer the simple question: given annualInterest of 0.05 what value do you want assigned to the variable: interest?

    When you have that value, then look at the code to see why that value is NOT getting to the variable: interest.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    Interest is annual interest / 12 is it not?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Programming Exercise Help

    I'm not asking for an equation. I'm asking for real numbers like: 3 or 4.55 or 0.444444

    The objective is to get a good value assigned to the variable: interest
    What value do you expect to be assigned to interest?
    What is the value of annualInterest/12?
    What value is being assigned to interest?
    Why the difference?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Programming Exercise Help

    Oh, 0.004 is expected. I'm getting the error and when I println the problem it shows interest as a 0. Why the difference is what I can't figure out. Not sure why it's getting a 0.

Page 1 of 2 12 LastLast

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Question about an exercise from D.S. Malik's Book "Java Programming"
    By Lahoree in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 15th, 2013, 01:47 AM
  4. Java Programming Exercise
    By Garron5899 in forum Loops & Control Statements
    Replies: 5
    Last Post: July 27th, 2012, 08:12 AM
  5. Need help with programming exercise.
    By X0BeachBabe0x in forum Loops & Control Statements
    Replies: 2
    Last Post: December 29th, 2011, 10:08 PM