Results 1 to 10 of 10
Thread: please asses my code
 12072010, 08:53 AM #1Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
please asses my code
Hi friends
i am learning on my own. from reading books and various material. I do not know how good / bad i am when it comes to writing code.
So if you can please asses my code and give me suggestions...
Question :
It is difficult to make a budget that spans several years, because prices are not stable.
If your company needs 200 pencils per year, you cannot simply use this year’s price
as the cost of pencils two years from now. Because of inflation, the cost is likely to
be higher than it is today. Write a program to gauge the expected cost of an item in
a specified number of years. The program asks for the cost of the item, the number
of years from now that the item will be purchased, and the rate of inflation. The
program then outputs the estimated cost of the item after the specified period.
Have the user enter the inflation rate as a percentage, such as 5.6 (percent). Your
program should then convert the percent to a fraction, such as 0.056, and should
use a loop to estimate the price adjusted for inflation.
Java Code:import java.util.*; public class CountInflationCost { public static void main(String[] args) { Scanner readInput = new Scanner(System.in); System.out.println("Please enter cost of the item:do not use $,% etc "); double costToday = readInput.nextDouble(); System.out.println("Please enter the number of years from now that the item will be purchased: "); double years = readInput.nextDouble(); System.out.println("Please enter the rate of inflation: "); double inflationRate = readInput.nextDouble(); double fraction = convertPercentageToFraction(inflationRate); for ( int i = 0 ; i < years ; i++) { costToday += costToday * fraction; System.out.println(costToday); } double expectedCost = costToday; System.out.printf("expected cost is $ %.2f " , expectedCost); } public static double convertPercentageToFraction(double nInflationRate) { nInflationRate = nInflationRate / 100 ; return nInflationRate; } }
 12072010, 11:29 AM #2Member
 Join Date
 Dec 2010
 Posts
 27
 Rep Power
 0
I would remove the print statement in the loop, cause the requirement is only to output the final cost after the specified years.
 12082010, 03:31 PM #3Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
You have just purchased a stereo system that cost $1,000 on the following credit
plan: no down payment, an interest rate of 18% per year (and hence 1.5% per
month), and monthly payments of $50. The monthly payment of $50 is used to
pay the interest, and whatever is left is used to pay part of the remaining debt.
Hence, the first month you pay 1.5% of $1,000 in interest. That is $15 in interest.
So, the remaining $35 is deducted from your debt, which leaves you with a debt of
$965.00. The next month, you pay interest of 1.5% of $965.00, which is $14.48.
Hence, you can deduct $35.52 (which is $50  $14.48) from the amount you owe.
Write a program that tells you how many months it will take you to pay off the
loan, as well as the total amount of interest paid over the life of the loan. Use a loop
to calculate the amount of interest and the size of the debt after each month. (Your
final program need not output the monthly amount of interest paid and remaining
debt, but you may want to write a preliminary version of the program that does
output these values.) Use a variable to count the number of loop iterations and
hence the number of months until the debt is zero. You may want to use other variables,
as well. The last payment may be less than $50 if the debt is small, but do not
forget the interest. If you owe $50, your monthly payment of $50 will not pay off
your debt, although it will come close. One month’s interest on $50 is only 75
cents.
Java Code:import java.util.*; public class Debtcounter { public static int months = 0 ; public static double totalInterestPaid = 0; public static void main(String[] args) { Scanner readInput = new Scanner(System.in); System.out.println("Please enter cost of goods you purchased: "); double costOfGoods = readInput.nextDouble(); System.out.println("Please enter downpayment amount: "); double downpayment = readInput.nextDouble(); System.out.println("Please enter interest rate per year: "); double interestRate = readInput.nextDouble(); System.out.println("Please enter choice of monthly payment: "); double monthlyPayment = readInput.nextDouble(); double montlyInterestRate = interestRatePerMonth( interestRate); double fraction = convertPercentageToFraction(montlyInterestRate); double priceAfterDownpayment = costOfGoods  downpayment; for ( int i = 0; !(priceAfterDownpayment <= 0) ; i++, months++ ) { double interestPaid = priceAfterDownpayment * fraction; double debtPaid = monthlyPayment  interestPaid; priceAfterDownpayment = priceAfterDownpayment  debtPaid; totalInterestPaid += interestPaid; System.out.printf(" Total interest paid is $ %.2f " ,totalInterestPaid ); System.out.println(); } System.out.println("Total months are " + months); } public static double interestRatePerMonth( double nInterestRate) { nInterestRate = nInterestRate / 12 ; return nInterestRate; } public static double convertPercentageToFraction(double nInterestRate) { nInterestRate = nInterestRate / 100 ; return nInterestRate; } }
 12082010, 03:39 PM #4Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
if ( no comments == 0)
{
:(
}
 12082010, 03:41 PM #5Moderator
 Join Date
 Apr 2009
 Posts
 12,918
 Rep Power
 22
You need to learn to indent properly, otherwise you'll end up with some really basic flow errors. However, you do have the correct format for naming of methods, classes and variables, which is a novelty to see round here.
:)
 12082010, 03:44 PM #6Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
kkk
i will improve on that ....thank you :)
 12082010, 05:10 PM #7Member
 Join Date
 Dec 2010
 Posts
 45
 Rep Power
 0
Funniest thread title of the year
 12092010, 12:34 AM #8Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
whats so funny about it? :)
 12092010, 03:14 AM #9Member
 Join Date
 Dec 2010
 Posts
 45
 Rep Power
 0
 12092010, 11:42 AM #10Member
 Join Date
 Apr 2010
 Posts
 22
 Rep Power
 0
Similar Threads

Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08302010, 04:21 AM 
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03042010, 12:19 PM 
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08122009, 11:14 AM 
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08092009, 01:21 PM 
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04262007, 03:52 PM
Bookmarks