Reputation: 59
I am making a program that takes in a sequence of Integers
and puts them into a StatisticianStack
. I am having trouble trying to figure out how to call the other methods on this stack. Any suggestions on how I can call all the other methods within the nextNumber
method?
StatisticianStack:
public class StatisticianStack {
Stack<Integers> stack = new Stack();
public void nextNumber(Integer stackNums){
stack.push(stackNums);
stack.length(); // what I would want to do, but do not know what to pass it.
}
public static int length(StatisticianStack numbers){
if(numbers.isEmpty())
return 0;
int sizeOfStack = numbers.size();
return sizeOfStack;
}
public static int sum(Stack<Integer> numbers){
int sum = 0;
if(numbers.isEmpty())
return sum;
for(int i = 0; i < numbers.size(); i++)
sum = sum + numbers.pop();
return sum;
}
public static Double mean(Stack<Integer> numbers){
double mean = 0;
double sum = 0;
if(numbers.isEmpty())
return Double.NaN;
for (int i = 0; i < numbers.size(); i++){
sum += i;
}
mean = sum/numbers.size();
return mean;
}
public static Double largestNum(Stack<Integer> numbers){
double largestNum = numbers.firstElement();
if(numbers.isEmpty())
return Double.NaN;
for (int i = 0; i < numbers.size(); i++){
if(largestNum < numbers.pop())
largestNum = numbers.pop();
}
return largestNum;
}
public static Double smallestNum(Stack<Integer> numbers){
double smallestNum = numbers.firstElement();
if(numbers.isEmpty())
return Double.NaN;
for (int i = 0; i < numbers.size(); i++){
if(smallestNum > numbers.pop())
smallestNum = numbers.pop();
}
return smallestNum;
}
}
Main:
public class StatisticianStackDemonstartion {
public static Integer numbers;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
StatisticianStack stack = new StatisticianStack();
stack.nextNumber(-1);
}
}
`
Any tips? Thank you.
Upvotes: 2
Views: 87
Reputation: 13858
Now I get it. You may want to think Object Oriented here.
You have one object of type StatisticianStack:
StatisticianStack stack = new StatisticianStack();
Then you put some numbers into that stack:
stack.nextNumber(-1);
stack.nextNumber(2.34);
stack.nextNumber(17);
Then you want to do some calculations on all the elements inside your stack:
int length = stack.getLength();
double mean = stack.getMean();
double largest = stack.getLargestNumber();
double smallest = stack.getSmallestNumber();
For this, you'd need to change your method signatures first:
public class StatisticanStack {
Stack<Double> stack = new Stack<Double>();
public void nextNumber(Double stackNums) {
stack.push(stackNums);
}
public int length() {
return stack.size();
}
public double sum() {
}
public double mean() {
}
public double largestNum() {
}
public double smallestNum() {
}
}
Please confirm this is what you want - then we fill these methods with life ;-)
You confirmed - here's completed example using Java 8 Lambda Expressions:
import java.util.Stack;
public class StatisticanStack {
Stack<Double> stack = new Stack<Double>();
public void nextNumber(Double stackNums) {
stack.push(stackNums);
}
public void nextNumber(Integer stackNums) {
stack.push(new Double(stackNums));
}
public int length() {
return stack.size();
}
public double sum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.sum();
}
public double mean() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.average().getAsDouble();
}
public double largestNum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.max().getAsDouble();
}
public double smallestNum() {
return stack.stream()
.mapToDouble(Double::doubleValue)
.min().getAsDouble();
}
public static void main(String... args) {
StatisticanStack stack = new StatisticanStack();
stack.nextNumber(-1);
stack.nextNumber(2.34);
stack.nextNumber(17);
System.out.println(stack.smallestNum());
System.out.println(stack.largestNum());
System.out.println(stack.mean());
System.out.println(stack.sum());
}
}
Or if you prefer working without Lambdas:
public double sum() {
double sum = 0;
for(Double entry : stack) {
sum += entry.doubleValue();
}
return sum;
}
and so on.
You might want to clarify what you want as "smallest" number: Closest to zero? Most negative?
Upvotes: 3
Reputation: 1804
There is a lot of psudo code and missing contex here. But you have both stack.push(stackNums)
and stacks.push(stackNums)
. Could you provide more context and the error message?
Upvotes: 0