Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default 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;
    }
    
    }

  2. #2
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    I would remove the print statement in the loop, cause the requirement is only to output the final cost after the specified years.

  3. #3
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    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;
    }
    
    }

  4. #4
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    if ( no comments == 0)
    {

    :(

    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    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.
    :)

  6. #6
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    kkk

    i will improve on that ....thank you :)

  7. #7
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Funniest thread title of the year

  8. #8
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default

    whats so funny about it? :)

  9. #9
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by search4survival View Post
    whats so funny about it? :)
    The spelling of assess provides the lulz

  10. #10
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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