2013-12-21 4 views
0

과제 완료에 대한 빠른 도움이 필요합니다. 간단히 말해서 사용자가 pop(), push() 및 top()을 허용하는 프로그램을 작성해야하는 과제를 완료했습니다.) 배열.작동시킬 푸시 방법을 얻을 수 없습니다.

나는 그것을했지만 내 교사는 코드 레이아웃이 잘못되어 스택 클래스에 System.out.println 문이 있고 이것들이 메인 메뉴 응용 프로그램에 있어야하며 스택 클래스는 메서드를 호출해야한다고 말했다. 배열 클래스에서 상속받습니다. 공정

충분히 나는 생각 나는 코드를 수정하지만 푸시() 메소드가 제대로 이제 동작하지 않습니다 : (나는 메뉴 응용 프로그램 푸시에 Genio.getInteger 방법을 사용할 필요가 있음을 알고/

)방법.

아무도 도와 줄 수 있습니까?

스택 클래스 :

public class Stack extends Array 
    { 
    private int x; 


public Stack() 
{ 
    super();// initialise instance variables 
    x = 0; 
} 

public Stack(int newsize) 
{ 
    super(newsize); 
    System.out.println("Stack Created!"); 
} 

/** 
    * @push user is asked to enter value at keyboard which is then added to the top of the stack 
    * 
    */ 
public boolean push(int item) 
{ 
    return add(item); 
} 


/** 
    * @pop removes the current value staored at the top of the stack 
    * 
    */ 
public int pop() 
{ 
     deleteLast(); 
     return getItemback(); 
}   

/** 
    * @top displays the current value stored at the top of the stack 
    * 
    */ 
public int top() 
{ 
    displayLast(); 
    return getItemback(); 
}   

} 

메뉴 응용 프로그램 :

public static void main() 
    { 
     int option; 
     int item; 

     Stack s = new Stack(); 
     String []menuitems = {"1 - Display Stack","2 - Pop Stack", "3 - Push Onto Stack","4 - Top Of Stack","5 - Quit Program"};    
     Menu m = new Menu(menuitems,5); 

     s.add(12);s.add(2);s.add(1);s.add(13);s.add(24); 

     do 
     { 
      clrscr(); 
      option = m.showMenu(); 
      if (option == 1)     
      {      
       s.display(); 
       pressKey(); 
      } 
      if (option == 2)     
      {  
       if (!s.isEmpty()) 
       System.out.println ("Number Popped: "); 
       else 
       System.out.println ("The Stack Is Empty! "); 
       pressKey(); 
      } 


      // THIS IS THE PART I CANNOT GET TO WORK!! NOT SURE WHERE/HOW TO CALL PUSH  
      // METHOD? 
      if (option == 3)     
      {  
       item = Genio.getInteger(); 
       if (!s.isFull()) 
       System.out.println("Please Enter Number To be Pushed(" + item + ")"); 
       else 
       System.out.println("Stack Overflow! "); 
       pressKey(); 
      } 
      if (option == 4)     
      {  
       if (!s.isEmpty()) 
       s.top(); 
       else 
       System.out.println ("The Stack Is Empty! "); 
       pressKey(); 
      } 
     } 
     while (option != 5); 
     System.out.println("\nDone! \n(You Can Now Exit The Program)\n"); 
    } 
/** 
* @clrscr removes all text from the screen 
* 
*/ 
    public static void clrscr() 
    {   
     for (int i=1;i<=50;i++) 
      System.out.println(); 
    } 
/** 
* @pressKey requires the user to press return to continue 
* 
*/ 
    public static void pressKey() 
    { 
     String s; 
     System.out.print("\nPress return to continue : "); 
     s = Genio.getString(); 
    }    
} 

편집 :? 경우 관련

public class Genio 

{

/** 
* Constructor for objects of class genio, but nothing needing constructed! 
*/ 
public Genio() 
{ 
} 


/** 
* getStr() is a private method which safely returns a string for use 
* by the public methods getString() and getCharacter() in the class. 
* 
* @return String for further processing withing the class 
*/ 

private static String getStr() 
{ 
    String inputLine = ""; 
    BufferedReader reader = 
     new BufferedReader(new InputStreamReader(System.in)); 
    try 
    { 
     inputLine = reader.readLine(); 
    } 

    catch(Exception exc) 
    { 
     System.out.println ("There was an error during reading: " 
          + exc.getMessage()); 
    } 
    return inputLine; 
} 

/** 
* getInteger() returns an integer value. Exception handling is used to trap 
* invalid data - including floating point numbers, non-numeric characters 
* and no data. In the event of an exception, the user is prompted to enter 
* the correct data in the correct format. 
* 
* @return validated int value 
*/ 
public static int getInteger() 
{ 
    int temp=0; 
    boolean OK = false; 

    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); 
    do 
    { 
     try 
     { 
      temp = Integer.parseInt(keyboard.readLine()); 
      OK = true; 
     } 

     catch (Exception eRef) 
     { 
      if (eRef instanceof NumberFormatException) 
      { 
       System.out.print("Integer value needed: "); 
      } 
      else 
      { 
       System.out.println("Please report this error: "+eRef.toString()); 
      } 
     } 

    } while(OK == false); 
    return(temp); 
} 

/** 
* getFloat() returns a floating point value. Exception handling is used to trap 
* invalid data - including non-numeric characters and no data. 
* In the event of an exception (normally no data or alpha), the user is prompted to enter 
* data in the correct format 
* 
* @return validated float value 
*/   
public static float getFloat() 
{ 
    float temp=0; 
    boolean OK = false; 

    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); 
    do 
    { 
     try 
     { 
      temp = Float.parseFloat(keyboard.readLine()); 
      OK = true; 
     } 


     catch (Exception eRef) 
     { 
      if (eRef instanceof NumberFormatException) 
      { 
       System.out.print("Number needed: "); 
      } 
      else 
      { 
       System.out.println("Please report this error: "+eRef.toString()); 
      } 
     } 

    } while(OK == false); 

    return(temp); 
} 

/** 
* getDouble() returns a double precision floating point value. 
* Exception handling is used to trap invalid data - including non-numeric 
* characters and no data. 
* In the event of an exception, the user is prompted to enter 
* data in the correct format 
* 
* @return validated double precision value 
*/   
public static double getDouble() 
{ 
    double temp=0; 
    boolean OK = false; 
    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); 
    do 
    { 
     try 
     { 
      temp = Double.parseDouble(keyboard.readLine()); 
      OK = true; 
     } 

     catch (Exception eRef) 
     { 
      if (eRef instanceof NumberFormatException) 
      { 
       System.out.print("Number needed: "); 
      } 
      else 
      { 
       System.out.println("Please report this error: "+eRef.toString()); 
      } 
     } 

    } while(OK == false); 

    return(temp); 
} 

/** 
* getCharacter() returns a character from the keyboard. It does this by 
* reading a string then taking the first character read. Subsequent characters 
* are discarded without raising an exception. 
* The method checks to ensure a character has been entered, and prompts 
* if it has not. 
* 
* @return validated character value 
*/ 

public static char getCharacter() 
{ 
    String tempStr=""; 
    char temp=' '; 
    boolean OK = false; 
    do 
    { 
     try 
     { 
      tempStr = getStr(); 
      temp = tempStr.charAt(0); 
      OK = true; 
     } 

     catch (Exception eRef) 
     { 
      if (eRef instanceof StringIndexOutOfBoundsException) 
      { 
       // means nothing was entered so prompt ... 
       System.out.print("Enter a character: "); 
      }    
      else 
      { 
       System.out.println("Please report this error: "+eRef.toString()); 
      } 
     } 

    } while(OK == false); 

    return(temp); 
} 

/** 
    * getString() returns a String entered at the keyboard. 
    * @return String value 
    */ 

public static String getString() 
{ 
    String temp=""; 
    try 
    { 
     temp = getStr(); 
    } 
    catch (Exception eRef) 
    { 
     System.out.println("Please report this error: "+eRef.toString()); 
    } 
    return(temp); 
}  
니오 클래스3210

}

+0

'Genio' 란 무엇입니까? –

+0

Genio 클래스 코드를 추가했습니다. 나는 사용자 입력을 다루는 것 외에는 확실치 않습니다. 모든 개인 교사는 "메뉴 앱에서 푸시하기 위해 Genio를 사용해야 할 것"이라고 말했습니다. –

+3

어디에서 push 메서드를 호출합니까? – hichris123

답변

0

질문을 올바르게 이해했는지 s.push가 호출되지 않았는지 확실하지 않은 경우? (s.pop 중 하나 아닌가요?)

다 얻을 것이다 당신이

if (!s.isFull() && s.push(item)) 

몇 가지 작업과

if (!s.isFull()) 

을 대체 할 수 있습니다 경우?

오직 프롬프트가되도록하려면 중괄호를 사용하십시오 (어쨌든 사용하면 어느 날 무시 무시한 버그에서 벗어날 수 있습니다). 이 같은.

if (!s.isFull()) { 
    System.out.println("enter a number to add"); 
    Scanner sc = new Scanner(System.in); 
    s.push(sc.nextInt()); 
} 
관련 문제