0

(学生ソフトウェア開発者はこちら!)

私たち (私たちのクラス) は最近、BlueJ を使用して新しい Java トピックを開始しました。これは私にとってまったく新しいことですが、今のところ楽しんで半分理解しています。

5 つのオプションを持つメニューを使用して、垂直に積み重ねられた配列を作成する必要があります。1- プッシュ 2- ポップ 3- トップ 4- 表示 5- 終了

「トップ」以外のすべてをコーディングすることができました(スタック/配列の上部に整数を表示します)

私の講師は私たちにこのヒントを与えてくれました。

これはメニューの私のコードです:

public static void main()
    {
        int option;
        Array a = new Array();
        String []menuitems = {"1 - Push","2 - Pop","3 - Top","4 - Display","5 - Quit"};            
        Menu m = new Menu(menuitems,5);

        a.add(4);a.add(2);a.add(28);a.add(15);

        do
        {
            clrscr();
            option = m.showMenu();
            if ( option == 1 )                                
                doPush(a);   
            if ( option == 2 )                                
                doPop(a);   
            if ( option == 3 )                                
                doTop(a);   
            if ( option == 4 )                
            {                    
                a.display();
                pressKey();
            }
        }
        while ( option != 5 );
        System.out.println("Done - You Can Now Close");
    }

これはプッシュ用の私のコードです:

public static void doPush(Array a)
    {
        if ( a.isFull() )
                {
                    System.out.print("\nArray Full!");                        
                }
                else {
                    int item;
                    System.out.print("Enter number to push: ");
                    item = Genio.getInteger();

                    if ( a.addToFront(item) == false)
                        System.out.print("\nArray Is Full!");
                    System.out.print("\nArray with new value: \n");
                    a.display();
                }
                pressKey();
    }

これはポップの私のコードです:

public static void doPop(Array a)
    {
        if ( a.isEmpty() ) {
            System.out.println("\nArray is Empty!");
            pressKey();
            return;
        }
        else
        {
            int item;
            item = Genio.getInteger();
            System.out.println("\nArray popped!");
        }


                pressKey();
    }
4

1 に答える 1

0

クラス自体にメソッドpop()push()メソッドを定義することをお勧めします。次に、次のようにArray定義できます。top()

public int top() {
    int topValue = pop();
    push(topValue);
    return topValue;
}

つまり、スタックからポップして、値をメモしてから、スタックにプッシュします。それは非常に効率的な実装ではありませんが、それが彼のヒントであるなら、私はそのようにします.

System.out.println()また、エラー条件ではなく例外を使用し、配列Stack使用する特定のクラスを定義することをお勧めします。

public class Stack {

    private Array array = new Array();

    public int push(int item) {
        if (!array.addToFront(item)) {
            throw new IllegalStateException("Stack is full");
        }
        /* TODO: It would be better (more idiomatic) for the Array.addToFront()
         * method threw this exception rather than returning a boolean.
         */ 
    }

    public int pop() {
        assertStackNotEmpty();

        // TODO: Remove the item from the front of the array
        //       and shuffle everything along

        return item;
    }

    public int peek() {
        assertStackNotEmpty();
        return array.get(0);
    }

    /**
     * Lecturer's suggested implementation of peek()
     */
    public int top() {
        int item = pop();
        push(item);
        return item;
    }

    private void assertStackNotEmpty() {
       if (array.isEmpty()) {
           throw new EmptyStackException("Stack is empty");
       }
    }
}
于 2013-11-06T19:56:37.393 に答える