# sum of digits depreciation

• 11-08-2008, 06:39 PM
jleas
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;
}

}
• 11-08-2008, 08:15 PM
Norm
Quote:

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.
• 11-08-2008, 10:23 PM
jleas
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);

}

}
• 11-08-2008, 10:24 PM
jleas
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)
• 11-08-2008, 10:28 PM
jleas
debugging
I tried debugging the main class but the out remained the same.
• 11-08-2008, 11:01 PM
Norm
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:
Quote:

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:
Quote:

result2 = result2 + y + "\t " + df.format(rate2) + "\t " + df.format(depreciationAmount2) + "\t " + df.format(depreciationSum2) + "\t " + df.format(assetValue2) + "\n";
• 11-08-2008, 11:13 PM
jleas
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.
• 11-08-2008, 11:42 PM
Norm
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.
• 11-09-2008, 01:13 AM
jleas
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.
• 11-09-2008, 03:36 AM
Norm
Quote:

getSumYears() method is calculating sumYears to be zero.
Great you've found the problem.
• 11-09-2008, 05:03 AM
jleas
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.
• 11-09-2008, 05:04 AM
jleas
That Norm I finally got the output I was looking for.
• 11-09-2008, 05:04 AM
jleas
sorry i meant thanks
• 11-09-2008, 02:37 PM
Norm
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.