
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 tradein 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 = pricedownPaymenttradeIn;
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.

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.

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

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?

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 = pricedownPaymenttradeIn;
1 is being divided by interest, and I'm assuming that interest is being equal to 0.

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.

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

Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?

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

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.

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.

Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?
For example: 20.0/12 = 1.66667

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...ningcode.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 tradein 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)

Re: Programming Exercise Help
Can you give an example of the calculation with actual numbers?
For example: 20.0/12 = 1.66667

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...ningcode.html

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?

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.

Re: Programming Exercise Help
Can you post some sample numbers for the variables: annualInterest and interest?
For example: 24 and 2

Re: Programming Exercise Help
For annual interest 0.05, which is then divided by 12 or something for interest I think.

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.

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.

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.

Re: Programming Exercise Help
Interest is annual interest / 12 is it not?

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?

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.