Results 1 to 6 of 6
  1. #1
    xemkruz2012 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default 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
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Simple Homework, New to Java, Please Help!!

    In the expression
    Java Code:
    (rate/100)
    both rate and 100 are integers so it's an integer operation also. Which means instead of 0.06 you get 0. And anything multiplied by 0 is 0.

    To solve this you can do several things.
    - You can make rate be of a floating-point number type in the first place.
    - You can write 100 as a floating-point number.
    - You can cast either rate or 100 in the (rate/100) expression to a floating-point number.

  3. #3
    xemkruz2012 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Simple Homework, New to Java, Please Help!!

    Quote Originally Posted by JavaAdviser View Post
    In the expression
    Java Code:
    (rate/100)
    both rate and 100 are integers so it's an integer operation also. Which means instead of 0.06 you get 0. And anything multiplied by 0 is 0.

    To solve this you can do several things.
    - You can make rate be of a floating-point number type in the first place.
    - You can write 100 as a floating-point number.
    - You can cast either rate or 100 in the (rate/100) expression to a floating-point number.

    I tried making rate floating as "float rate = 6F;", ans comes out like 4499.9999999

    How do I make 100 a floating point?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Homework, New to Java, Please Help!!

    For one, don't use floats when you can use double since the accuracy is much greater with double with minimal cost. For another, please understand that no programming language that runs on a digital computer will be able to represent floating point type values with exact precision. Instead you need to accept a very close approximation. This is not a fault of Java but rather a limitation of digital computers in general. What many do is to format our values to the significant digit desired when converting to String for display.

  5. #5
    xemkruz2012 is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Simple Homework, New to Java, Please Help!!

    Quote Originally Posted by Fubarable View Post
    For one, don't use floats when you can use double since the accuracy is much greater with double with minimal cost. For another, please understand that no programming language that runs on a digital computer will be able to represent floating point type values with exact precision. Instead you need to accept a very close approximation. This is not a fault of Java but rather a limitation of digital computers in general. What many do is to format our values to the significant digit desired when converting to String for display.
    Can you please tell me what to put and where to put the correct code so that I can get the output 4500, please... Im new to Java, so having problems understanding that

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Simple Homework, New to Java, Please Help!!

    I don't like preventing you from a learning experience, so I'd rather give you suggestions, but you should write the code.

    As noted above, do floating point division, say by dividing rate by 100.0, a double literal, not 100, an int literal. This will result in a double result, what you want. If later you want to display the result as a String with no decimal part, the use String.format(...) or a DecimalFormat object to do this for you. For example, String.format("%.0f", interest) will return a String representation of the interest variable with no decimal portion. The API of the Formatter class will give all the gory details of how to use this construct.

Similar Threads

  1. A simple bank program as homework
    By jackandjill in forum New To Java
    Replies: 4
    Last Post: 08-21-2012, 11:57 AM
  2. Simple Homework applet problems
    By ADustedEwok in forum Java Applets
    Replies: 1
    Last Post: 12-08-2011, 11:11 PM
  3. Need HW help on java homework
    By sourlemons in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 06:44 AM
  4. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 09:12 PM
  5. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 03:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •