Results 1 to 14 of 14
  1. #1
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default sum of digits depreciation

    I am having problem trying to calculate the sum-of-digits depreciation method. Could anyone help my this is what I have but I keep getting question marks for the output. The bold part is what is giving me problems. Thanks.


    import javax.swing.JOptionPane;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;

    public class Depreciation
    {
    String result, result2;
    static String s;
    int year, b;
    double amount;
    double depreciationAmount, depreciationSum, assetValue, depreciationAmount2, depreciationSum2, assetValue2;
    double rate, rate2;
    int sumYears;

    public Depreciation(double a, int y)
    {
    amount = a;
    year = y;
    result = "";
    result2 = "";
    }

    static double getAmount(String str)
    {
    s = JOptionPane.showInputDialog(str);
    return Double.parseDouble(s);
    }

    static int getYear(String str)
    {
    s = JOptionPane.showInputDialog(str);
    return Integer.parseInt(s);
    }

    public void calculateStraightLine()
    {
    rate = 1/(double)year;
    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat)nf;
    df.applyPattern("0.00");
    int y = 1;
    while(y <= year)
    {
    depreciationAmount = (amount)*rate;
    depreciationSum = depreciationSum + depreciationAmount;
    assetValue = amount - depreciationSum;
    result = result + y + "\t " + df.format(rate) + "\t " + df.format(depreciationAmount) + "\t " + df.format(depreciationSum) + "\t " + df.format(assetValue) + "\n";
    y = y + 1;
    }

    }

    private int getSumYears(int t)
    {
    int r = 0;

    while( b > 0)
    {
    b = t - r;
    sumYears = sumYears + b;
    r = r + 1;
    }
    return sumYears;
    }

    public void calculateSumOfDigits()
    {
    int y = 1;
    rate2 = year/(double)getSumYears(year);
    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat)nf;
    df.applyPattern("0.00");
    while(y <= year)
    {

    depreciationAmount2 = (amount)*rate2;
    depreciationSum2 = depreciationSum2 + (depreciationAmount2);
    assetValue2 = amount - depreciationSum2;
    result2 = result2 + y + "\t " + df.format(rate2) + "\t " + df.format(depreciationAmount2) + "\t " + df.format(depreciationSum2) + "\t " + df.format(assetValue2) + "\n";
    rate2 = rate2 - 1/(double)getSumYears(year);
    y = y + 1;
    }

    }

    public String toString()
    {
    return result + "\n" + result2;
    }

    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    keep getting question marks for the output
    Can you show the output you get?
    What method call is returning the question marks?
    Try debugging your code by adding println()s to see how variables values change and the execution flow.

  3. #3
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default this is my test class

    public class TestDepreciation
    {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args)
    {
    // TODO code application logic here
    int y = Depreciation.getYear("Enter Term in Years");
    double a = Depreciation.getAmount("Enter Amount");
    Depreciation d = new Depreciation(a, y);
    System.out.println("Year\tDepreciation Rate\tDepreciation Amount\tAccumulated Depreciation\tAsset Value");
    d.calculateStraightLine();
    d.calculateSumOfDigits();
    System.out.println(d);

    }

    }

  4. #4
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default this is the output that I get with 5 years and $1000

    init:
    deps-jar:
    compile-single:
    run-single:
    Year Depreciation Rate Depreciation Amount Accumulated Depreciation Asset Value
    1 0.20 200.00 200.00 800.00
    2 0.20 200.00 400.00 600.00
    3 0.20 200.00 600.00 400.00
    4 0.20 200.00 800.00 200.00
    5 0.20 200.00 1000.00 0.00

    1 ? ? ? -?
    2 ? ? ? ?
    3 ? ? ? ?
    4 ? ? ? ?
    5 ? ? ? ?

    BUILD SUCCESSFUL (total time: 7 seconds)

  5. #5
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default debugging

    I tried debugging the main class but the out remained the same.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    Yes, debugging the code won't change what it does. The point of debugging is to find out what the code is doing incorrectly so you can fix it.
    What were the invalid values that cause the question mark? For example did you print out all the variables in this statement:
    result = result + y + "\t " + df.format(rate) + "\t " + df.format(depreciationAmount) + "\t " + df.format(depreciationSum) + "\t " + df.format(assetValue) +
    "\n";

    result, rate, depreciationAmount, depreciationSum, assetValue

    Can you show What were they?
    And the same for this statement:
    result2 = result2 + y + "\t " + df.format(rate2) + "\t " + df.format(depreciationAmount2) + "\t " + df.format(depreciationSum2) + "\t " + df.format(assetValue2) + "\n";

  7. #7
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    for result:
    >>the rate is .2
    >>the depreciationAmount is 200.00
    >>the depreciationSum are the values: 200.00, 400.00, 600.00, 800.00, and 1000.00 from top to bottom.
    >>the assetValue are the values: 800.00, 600.00, 400.00, 200.00, and 0.00 from top to bottom
    >>the values are all correct.

    for result2:
    the values are the same and in the same order. I know the problem is with the getSumYears() method but I dont know why. I've rewritten the statement 10 times none of them correct.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    I'm confused. Your last post does NOT show the output with the question marks. What happened to them? Does it work now?

    When you post program output, do NOT edit it. Copy and post it exactly as it is output by the program.

  9. #9
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    No the output is my third post. You asked for an explanation of some of my variables. The question marks remain when the program runs d.calculateSumOfDigits(), but the problem is with my getSumYears() method. If the years is 5, sumYears is supposed to be 5+4+3+2+1 or 15. I think the question marks are because the getSumYears() method is calculating sumYears to be zero. And obviously its impossible to divide by zero when the program calculates rate2.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    getSumYears() method is calculating sumYears to be zero.
    Great you've found the problem.

  11. #11
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    yea I knew that coming in. I just dont know why it calculates it to zero. I wanted to see if you could look over my while statement for my getSumYears() method.

  12. #12
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    That Norm I finally got the output I was looking for.

  13. #13
    jleas is offline Member
    Join Date
    Nov 2008
    Posts
    10
    Rep Power
    0

    Default

    sorry i meant thanks

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,880
    Rep Power
    25

    Default

    You use too many global variables. Many of them should be local to the methods that use them and not be available across method calls or for other methods to use.
    Also some of your variable names are poory chosen. b, t and r don't explain what they are used for.

Posting Permissions

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