Reputation: 13
I have 2 Classes, DigitMath.java and DigitMathRunner.java, the setup the lab requires uses the DigitMathRunner as the launcher for DigitMath.
My code's purpose is to show the Average of Number then output it into a sentence. The compiler gives no error but the output is below.
> >
run DigitMathRunner
234 has a digit average of 0.0
10000 has a digit average of 0.0
111 has a digit average of 0.0
9005 has a digit average of 0.0
84645 has a digit average of 0.0
8547 has a digit average of 0.0
123456789 has a digit average of 0.0
>
Here is the code for DigitMath.java
import static java.lang.System.*;
public class DigitMath
{
private int number;
private int count;
private int s;
private int sum;
private int input;
private double average;
public DigitMath()
{
number = 0;
sum=0;
count = 0;
input = 0;
}
public DigitMath(int s)
{
number = s;
input = s;
}
public void setNums(int s)
{
number = s;
input = s;
}
public int sumDigits()
{
int sum=0;
while(input > 0)
{
sum += input % 10;
input /= 10;
}
return sum;
}
public int countDigits()
{
count = (int)(Math.log10(number)+1);
return count;
}
public double averageDigits()
{
double average = sum/count;
return average;
}
public int output()
{
System.out.println(""+number +" has a digit average of "+""+average);
return number;
}
}
and the next block of code is the Runner.
//Name - Seth Garcia
//Date - 5/12/16
//Class - 3rd Period Monaghan
//Lab - DigitMath
import static java.lang.System.*;
public class DigitMathRunner
{
public static void main( String args[] )
{
DigitMath test = new DigitMath();
test.setNums(234);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(10000);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(111);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(9005);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(84645);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(8547);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
test.setNums(123456789);
test.sumDigits();
test.countDigits();
test.averageDigits();
test.output();
}
}
also I am in Computer Science 1 in Highschool, if my code is bad please tell me how to improve, thanks.
Upvotes: 1
Views: 470
Reputation: 2164
you also could change your code and use streams for this. (only want to mention it, that you can solve a problem in different ways)
IntSummaryStatistics summaryStatistics = String.valueOf(number).chars().map(i -> i - '0').summaryStatistics();
System.out.println(summaryStatistics.getAverage());
.
String.valueOf(number)
makes a String of your number
.chars()
makes a stream of the chars of your string (as int value)
.map(i -> i - '0')
subtract the intvalue of '0'
.summaryStatistics()
gives you a IntSummaryStatistics object
Upvotes: 0
Reputation: 191844
Variable shadowing is your problem.
double average = sum/count;
Remove double
, please.
And force floating point division
average = sum/(double)count;
Similar problem is in sumDigits()
Additionally, you can remove the private double average
entirely because it is a calculated value.
public double averageDigits()
{
return sum/(double)count;
}
And use that method inside the output
method instead of average
Upvotes: 6
Reputation: 1925
You need to use the class's members in sumDigits()
and averageDigits()
instead of using local variable:
public int sumDigits()
{
sum=0;
while(input > 0)
{
sum += input % 10;
input /= 10;
}
return sum;
}
public double averageDigits()
{
average = (double)sum/count;
return average;
}
Upvotes: 2