# Programming Exercise Help

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• February 18th, 2014, 03:10 PM
Amity
Programming Exercise Help
Code :

```  /* 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.
• February 18th, 2014, 03:25 PM
Norm
Re: Programming Exercise Help
Quote:

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.
• February 18th, 2014, 03:42 PM
Amity
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)"
• February 18th, 2014, 04:14 PM
Norm
Re: Programming Exercise Help
Quote:

"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?
• February 18th, 2014, 04:17 PM
Amity
Re: Programming Exercise Help
Code :

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

That is line 53. It refers back to

Code :

```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.
• February 18th, 2014, 04:25 PM
Norm
Re: Programming Exercise Help
Quote:

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.
Code :

`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.
• February 18th, 2014, 04:32 PM
Amity
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.

Code :

```System.out.print("What is the decimal interest rate? "); annualInterest = reader.nextDouble();```
Code :

`interest = (int) ((double)annualInterest / 12);`
• February 18th, 2014, 04:58 PM
Norm
Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?
• February 18th, 2014, 05:56 PM
Amity
Re: Programming Exercise Help
The inputs I used art the following which includes error, shown in output.

Code :

```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)```
• February 18th, 2014, 06:12 PM
Cornix
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:
Code :

`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.
• February 18th, 2014, 06:17 PM
Amity
Re: Programming Exercise Help
Quote:

Originally Posted by Cornix
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:
Code :

`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.
• February 18th, 2014, 06:24 PM
Norm
Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?
For example: 20.0/12 = 1.66667
• February 18th, 2014, 06:42 PM
Amity
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

Code :

```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)```
• February 18th, 2014, 06:43 PM
Norm
Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?
For example: 20.0/12 = 1.66667
• February 18th, 2014, 06:45 PM
Amity
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
• February 18th, 2014, 06:56 PM
Norm
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?
• February 18th, 2014, 07:02 PM
Amity
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.
• February 18th, 2014, 07:17 PM
Norm
Re: Programming Exercise Help
Can you post some sample numbers for the variables: annualInterest and interest?

For example: 24 and 2
• February 18th, 2014, 07:19 PM
Amity
Re: Programming Exercise Help
For annual interest 0.05, which is then divided by 12 or something for interest I think.
• February 18th, 2014, 07:22 PM
Norm
Re: Programming Exercise Help
Quote:

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.
• February 18th, 2014, 07:29 PM
Amity
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.
• February 18th, 2014, 07:33 PM
Norm
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.
• February 18th, 2014, 07:37 PM
Amity
Re: Programming Exercise Help
Interest is annual interest / 12 is it not?
• February 18th, 2014, 07:45 PM
Norm
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?
• February 18th, 2014, 07:49 PM
Amity
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last