#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    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 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
    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.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,535
    Rep Power
    1906
    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)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    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

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



    }
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    error shows up
    Please post the full text of the error message.

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

IMN logo majestic logo threadwatch logo seochat tools logo