Results 1 to 14 of 14
Thread: sum of digits depreciation
- 11-08-2008, 05:39 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 07:15 PM #2
Can you show the output you get?keep getting question marks for the output
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, 09:23 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 09:24 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 09:28 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
debugging
I tried debugging the main class but the out remained the same.
- 11-08-2008, 10:01 PM #6
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:
"\n";result = result + y + "\t " + df.format(rate) + "\t " + df.format(depreciationAmount) + "\t " + df.format(depreciationSum) + "\t " + df.format(assetValue) +
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";
- 11-08-2008, 10:13 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 10:42 PM #8
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, 12:13 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 02:36 AM #10
Great you've found the problem.getSumYears() method is calculating sumYears to be zero.
- 11-09-2008, 04:03 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
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, 04:04 AM #12
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
That Norm I finally got the output I was looking for.
- 11-09-2008, 04:04 AM #13
Member
- Join Date
- Nov 2008
- Posts
- 10
- Rep Power
- 0
sorry i meant thanks
- 11-09-2008, 01:37 PM #14


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks