在同一个类中调用堆栈上的方法

时间:2023-01-02 16:45:49

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。我无法弄清楚如何调用此堆栈上的其他方法。关于如何在nextNumber方法中调用所有其他方法的任何建议?

StatisticianStack:

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.

有小费吗?谢谢。

2 个解决方案

#1


3  

Now I get it. You may want to think Object Oriented here.

现在我懂了。您可能想要在这里考虑面向对象。

You have one object of type StatisticianStack:

您有一个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:

您确认 - 这是使用Java 8 Lambda表达式完成的示例:

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:

或者如果你喜欢没有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?

您可能想澄清您想要的“最小”数字:最接近零?最负面的?

#2


0  

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?

这里有很多psudo代码和缺少的上下文。但是你有stack.push(stackNums)和stacks.push(stackNums)。你能提供更多的上下文和错误信息吗?

#1


3  

Now I get it. You may want to think Object Oriented here.

现在我懂了。您可能想要在这里考虑面向对象。

You have one object of type StatisticianStack:

您有一个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:

您确认 - 这是使用Java 8 Lambda表达式完成的示例:

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:

或者如果你喜欢没有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?

您可能想澄清您想要的“最小”数字:最接近零?最负面的?

#2


0  

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?

这里有很多psudo代码和缺少的上下文。但是你有stack.push(stackNums)和stacks.push(stackNums)。你能提供更多的上下文和错误信息吗?