September 14th, 2012, 06:12 PM

Simple Homework, New to Java, Please Help
This is my assignement, my professor wnats me to output 4500.0 in interest, but I'm getting is 0.
All codes are below,
public class Interest {
public static void main(String[] args) {
//declare variables
double loan=5000, interest=0.0;
int years=15, rate=6;
//calculations
interest = loan * (rate/100) * years;
//output
System.out.println(interest);
}
}
THIS IS THE ASSIGNMENT PAPER
Assignment 2 (10 points): Calculating Interest
Our goal is to calculate the interest given the loan amount, rate, and years to be taken out.
Your program should have the following:
Make the name of the project Interest
4 comment lines that state the purpose of the program, author, date and JDK
used. (1 point)
Include 4 variables for the amount of loan, rate, years, and interest. The amount
of loan and interest variables are decimal numbers. The years and rate
variables should be integers. Make up your own meaningful correctlyformed
variable names for these 4 items and declare them appropriately as an int or
double. (4 points)
Set the loan amount to be 5000. Set interest rate to be 6. Set years to be 15.
With an assignment statement, have the computer calculate the interest using the
following formula: (3 points)
interest = amount * (rate/100) * years
Note: Please use your own variable names in above formula.
Have the computer display the amount of loan, rate, years and the interest that you
calculated. You should print this on several lines. (2 points)
Compile your program until you have no compilation errors. When you run this
application, you should get an answer for interest as 4500.0 If you are getting an
answer of 0, THINK!! Don't change the variable types. Don't worry about it
appearing with dollars and cents since formatting has not been covered yet.
September 14th, 2012, 06:43 PM

The problem is when you do the division:
As both numbers are integer, it will not use decimal wherefore the result of 0.06 will turn into 0.
Multiplying with 0 can and will only give one result
To keep the decimal, you will have to use one value of type double/float.
For example:
or with type casting
Code:
(double)(rate / 100)
September 14th, 2012, 09:38 PM

Originally Posted by MrFujin
The problem is when you do the division:
As both numbers are integer, it will not use decimal wherefore the result of 0.06 will turn into 0.
Multiplying with 0 can and will only give one result
To keep the decimal, you will have to use one value of type double/float.
For example:
or with type casting
Code:
(double)(rate / 100)
This is what I wrote, error shows up
/* Adnan Alvee
* 9/14/2012
* JDK 1.6
* This program will calculate interest
*/
public class Interest {
public static void main(String[] args) {
//declare variables
double loan=5000, interest=0.0;
int years=15, rate=6;
(double)(rate/100);
//calculations
rate = rate/100;
interest = loan * (rate/100) * years;
//output
System.out.println(loan);
System.out.println(rate);
System.out.println(years);
System.out.println(interest);
}
}
September 15th, 2012, 08:44 AM

Please post the full text of the error message.
Also Please edit your post and wrap the code in code tags.