BigDecimal ClassTo be able to write Java programs that deal with finances, you need to know about the BigDecimal class and numeric formatting. BigDecimal will deal with double numbers as an integer with fix position of the decimal dot. There are 16 constructors for the class.What you should do is create the BigDecimal directly with the string ".35" as shown here: BigDecimal d = new BigDecimal(".35"); .35 = 0.35 Since you can't necessarily store the value of a BigDecimal in a primitive object like a double, it is best to create your BigDecimal objects from a String. To demonstrate this error, here's a simple example: double dd = .35; BigDecimal d = new BigDecimal(dd); System.out.println(".35 = " + d); .35 = 0.34999999999999997779553950749686919152736663818359375 In the first case, the String argument to the BigDecimal constructor determines the value of the object created. To display the amount, you can use the BigDecimal class's toString() method: System.out.println(d.toString()); If you call println with an object argument directly, it calls object's toString() method first and then show the result of this method. BigDecimal operations BigDecimal methods for adding and subtracting numbers are add() and subtract(), respectively. For example, to add 1,115.37 and 115.37, we could do the following: BigDecimal balance = new BigDecimal("1115.37");BigDecimal transaction = new BigDecimal("115.37"); BigDecimal newBalance = balance.add(transaction); The BigDecimal's newBalance object now holds the value of 1,230.74. Similarly, we can subtract , multiplying and dividing with BigDecimal's subtract(), multiply(), and divide() methods. // Create via a string BigDecimal bd3; bd1 = bd1.pow(n); Precision and Scale d2 = d2.setScale(2, BigDecimal.ROUND_HALF_UP); For more on BigDecimal Class: ( http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html )Task
1- Mortgage.java: Write a java program that takes two command line arguments (L-loan amount, I-interest rate per period) and reads bunch of payments from the standard input steam and shows the result in standard output. Each payment represents the passage of period of the loan. For each payment the program computes the interest on the current balance, adds it to the balance, and then subtracts the payment from the balance. The interest is the rate times the balance rounded to the nearest penny. Each time the string “Balance” appears in the input the program prints the balance. For positive balance print the word 'left' after the number, for negative balance print 'over', and for zero print 0.00 and nothing afterewards. Note: set the scale to always 2 digits after decimal point regardless of the input strings. Sample Command Line: Sample Input balance Sample Output |