Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 14th, 2012, 06:12 PM
 xemkruz2012
Registered User

Join Date: Sep 2012
Posts: 2
Time spent in forums: 10 m 51 sec
Reputation Power: 0

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 correctly-formed
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
 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.

#2
September 14th, 2012, 06:43 PM
 MrFujin
Lord of the Dance

Join Date: Oct 2003
Posts: 3,359
Time spent in forums: 2 Months 2 Weeks 4 Days 22 h 33 m 54 sec
Reputation Power: 1828
The problem is when you do the division:
Code:
` (rate / 100)`

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:
Code:
`(rate / 100.0) `

or with type casting
Code:
`(double)(rate / 100) `

#3
September 14th, 2012, 09:38 PM
 xemkruz2012
Registered User

Join Date: Sep 2012
Posts: 2
Time spent in forums: 10 m 51 sec
Reputation Power: 0
Quote:
 Originally Posted by MrFujin The problem is when you do the division: Code: ` (rate / 100)` 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: Code: `(rate / 100.0) ` or with type casting Code: `(double)(rate / 100) `

This is what I wrote, error shows up

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

}
}

#4
September 15th, 2012, 08:44 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,593
Time spent in forums: 2 Weeks 3 Days 3 h 35 m 23 sec
Reputation Power: 347
Quote:
 error shows up

Please post the full text of the error message.

Also Please edit your post and wrap the code in code tags.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Simple Homework, New to Java, Please Help