GIDForums

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Feb-2007, 12:20
bobbiegirl bobbiegirl is offline
New Member
 
Join Date: Oct 2006
Location: Upstate New York
Posts: 10
bobbiegirl is an unknown quantity at this point
Red face

Code for Homework


I have written a code for a homework assignment and would like someone to look at it and seif it is okay, it compiles and answers the questions of what the interest for the life of the loan and the number of months the repay is, but for some reason I cannot get the interest payments to be with only 2 decimals, they have several trailing numbers after the decimal.
JAVA Code:
import java.util.Scanner;

public class SteroLoan {

        public static void main(String args[]){
            int count=0;
            int numberOfMonths=0;
            double debtAmount=1000;
            double monthlyIntrest = 0.015;  //Yearly 18% i.e 1.5% monthly
            int monthlyPayment = 50;
            double totalInterestPaid =0;

             while(debtAmount>0){
                double intrestAmount = debtAmount * monthlyIntrest;

                totalInterestPaid = totalInterestPaid+ intrestAmount;


System.out.println("intrestAmount"+intrestAmount);

                double principleAmount= monthlyPayment -intrestAmount;


System.out.println("principleAmount:"+principleAmount);

                debtAmount = debtAmount - principleAmount;

                System.out.println("debtAmount :" + debtAmount);
                  count++;

            }


            System.out.println("The amount of InterestPaid:" + totalInterestPaid);
            System.out.println("Number of months :" + count);

           }



}

Can some one advise me?

Thanks

Barb
Last edited by admin II : 27-Feb-2007 at 06:42. Reason: Please surround your Java code with [java] ... [/java]
  #2  
Old 26-Feb-2007, 12:49
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Code for Homework


Suggestions (These are only suggestions based on my own experience, YMMV, no warranty, no claim of fitness for any particular purpose, void where prohibited by law, no animals were harmed in the making of these suggestions with the possible exception of that dog next door who barks incessantly and deserves to be shot):

- In Java, use objects instead of base types. That is, prefer to use Double instead of double.
- When dealing with money, prefer BigDecimal to Double. The reason is, BigDecimal is "arbitrary precision", has eight user-selectable rounding modes, etc. etc.

Check it out... http://java.sun.com/j2se/1.5.0/docs/...igDecimal.html
  #3  
Old 26-Feb-2007, 20:34
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 872
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Code for Homework


You need a 'formatter'.

Maybe the NumberFormat class, perhaps?
JAVA Code:
import java.text.NumberFormat;
Since all your code is in main, then IN main:
JAVA Code:
NumberFormat currency = NumberFormat.getCurrencyInstance();
Then, anywhere you need a value formatted, use this:
JAVA Code:
// an example wrapped around the 'intrestAmount' variable in your output:
currency.format(intrestAmount) 
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
25-35% off on all shared & reseller packages FOR LIFE! my-e-space Web Hosting Advertisements & Offers 0 11-May-2006 21:00
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 14:48
functions seems not to exit from a certain code block jaro C Programming Language 3 21-Mar-2006 23:08
Problem with int mixed with char,... leitz CPP / C++ Forum 17 07-Dec-2004 20:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.