2012-02-17 1 views
2

기본적으로 프로그램은 경영진의 "라운드 테이블"을 만들고, 회장은 변경할 수 없습니다. 나는 내가하고있는 일을 거의 잘 알고 있으며 중역을 삽입하고 제거하는 방법에 대해서 중도 쯤에 있지만, 코드를 테스트하여 코드가 어떻게 진행되는지를 확인하려고 노력했으며, 의장을 입력하자마자 오류가 발생합니다. 정보. 또한, 나는 ExecutiveList의 removeByCorporation 메서드에 대해 어떻게 생각하는지 잘 모르겠습니다. 나는이 방법이 거의 모두 틀렸다고 거의 긍정적이다. 그리고 나는 이런 식으로 순환 이중 연결리스트에서 노드를 제거하는 방법이 아니다.Java로 구성된 순환 복식 링크 프로그램 (Homework Help)

* 인쇄 방법을 도와 줄 필요가 없습니다. 아직 인쇄하지 못했습니다.

tl; dr : 1) 왜 지금 당장 부서 지나요? 2) removeByCorporation 메소드가 완전히 잘못되었다는 것이 확실합니다. 그렇다면 수정 방법에 대한 제안이나 도움이 필요하십니까?

여기 제가 두 가지 문제가 있습니다. 다른 사람들이 내게 알려 주면 알려 드리겠습니다.하지만 그들은 99 % 게터와 세터입니다.

FIRST CLASS

public class ExecutiveList { 

private ExecutiveNode chair; 
ExecutiveNode cursor; 

public ExecutiveList() { 

} 

public ExecutiveList (Executive chairperson) { 

    chair.setExecutive(chairperson); 
    chair.left = chair; 
    chair.right = chair; 
} 

public void insertLeftOfChair(Executive exec) { 

    ExecutiveNode newExec = new ExecutiveNode(); 
    newExec.setExecutive(exec); 
    chair.setLeft(newExec); 

} 

public boolean insertRightOfExec (Executive exec, String target) { 
    cursor = chair; 
    ExecutiveNode newExec = new ExecutiveNode(); 

    do { cursor = cursor.getLeft(); 
    if (cursor.getExecutive().equals(exec)) { 
     newExec.setExecutive(exec); 
     cursor.getRight().setLeft(newExec); 
     newExec.setLeft(cursor); 
     newExec.setRight(cursor.getRight()); 
     cursor.setRight(newExec); 
     return true; 
    } 
    else { 
     return false; 
    } 
    }  while (cursor.getExecutive().getExecutiveName() != target); 


} 

public boolean insertLeftOfExec (Executive exec, String target) { 
    cursor = chair; 
    ExecutiveNode newExec = new ExecutiveNode(); 
    do { cursor = cursor.getLeft(); 
     if (cursor.getExecutive().equals(exec)) { 
      newExec.setExecutive(exec); 
      cursor.getLeft().setRight(newExec); 
      newExec.setRight(cursor); 
      newExec.setLeft(cursor.getRight()); 
      cursor.setLeft(newExec); 

      return true; 
     } 
     else { 
      return false; 
     } 
    }  while (cursor.getExecutive().getExecutiveName() != target); 
} 

public boolean removeTargetExec(String name) { 
    if (chair.equals(name)) { 
     return false; 
    } 
    else { 
     return false; 
    } 
} 

public int removeByCorporation(String corporation) { 
    int removed = 0; 
    cursor = chair; 
    do { 
     if (cursor.getExecutive().getCompanyName().equals(corporation)) { 
      cursor.setExecutive(null); 
      cursor.getLeft(); 
      removed = removed + 1; 
     } 
    } while (removed > 0); 

    return removed; 
} 

public void printByCorporation(String corporation) { 

} 

public void printAllClockwise() { 

} 

public void printAllCounterClockwise() { 

} 
    } 

SECOND CLASS

import java.util.Scanner; 

    public class MeetingManager { 
public static void main(String[] args) { 

    // scanner to read the users input 
    Scanner input = new Scanner(System.in); 

    // strings to pass information about the chairperson 
    String chairpersonName; 
    String chairpersonCompany; 

    // strings to pass information about executives other 
    // than the chairperson 
    String execName; 
    String execCompany; 
    String target; 

    // holds information on whether on not an operation 
    // was successful and how many executives were removed 
    // for the remove by corporation command. 
    boolean success; 
    int numRemoved = 0; 

    // prompts the user for information about the chairperson 
    // and sets it to an executive object name chairperson 
    System.out.println("Enter the name of the chairperson: "); 
    chairpersonName = input.next(); 
    if (chairpersonName.length() < 1) { 
     System.out.println("Please enter a full name"); 
    } 
    System.out.println("Enter the company of the chairperson: "); 
    chairpersonCompany = input.next(); 
    if (chairpersonCompany.length() < 1) { 
     System.out.println("Please enter a full name"); 
    } 
    Executive chairperson = new Executive(chairpersonName, chairpersonCompany); 

    // creates a new ExecutiveList object and passes information 
    // about the chairperson 
    ExecutiveList list = new ExecutiveList(chairperson); 

    // for loop to repeatedly print the menu and take instructions 
    // from the user until they choose to exit. 
    for (int i = 1; i > 0; i++) { 
     ShowMenu(); 
     String option = input.next(); 

     // error message for improper input 
     if (option.length() > 3) { 
      System.out.println("You can only enter one option"); 
     } 

     // insert left of chairperson 
     else if (option.toUpperCase().equals("ILC")) { 
      System.out.println("Enter the executives name: "); 
      execName = input.next(); 
      System.out.println("Enter the executives company: "); 
      execCompany = input.next(); 
      Executive newGuy = new Executive(execName, execCompany); 
      list.insertLeftOfChair(newGuy); 
      System.out.println("Insertion successful."); 
     } 

     // insert left of executive 
     else if (option.toUpperCase().equals("ILE")) { 
      System.out.println("Enter the executives name: "); 
      execName = input.next(); 
      System.out.println("Enter the executives company: "); 
      execCompany = input.next(); 
      Executive newGuy = new Executive(execName, execCompany); 
      System.out.println("Enter the name of the target executive: "); 
      target = input.next(); 

      success = list.insertLeftOfExec(newGuy, target); 
      if (success == true) { 
       System.out.println("Insertion successful."); 
      } 
      else { 
       System.out.println("The executive could not be inserted."); 
      } 
     } 


     // insert right of executive 
     else if (option.toUpperCase().equals("IRE")) { 
      System.out.println("Enter the executives name: "); 
      execName = input.next(); 
      System.out.println("Enter the executives company: "); 
      execCompany = input.next(); 
      Executive newGuy = new Executive(execName, execCompany); 
      System.out.println("Enter the name of the target executive: "); 
      target = input.next(); 

      success = list.insertRightOfExec(newGuy, target); 
      if (success) { 
       System.out.println("Insertion successful."); 
      } 
      else { 
       System.out.println("The executive could not be inserted."); 
      } 
     } 

     // remove target executive 
     else if (option.toUpperCase().equals("RTE")) { 
      System.out.println("Enter the name of the executive to remove: "); 
      execName = input.next(); 
      success = list.removeTargetExec(execName); 

      if (execName.equals(chairpersonCompany)) 
       list.removeTargetExec(execName); 
      if (success) { 
       System.out.println(execName + " has been removed from the meeting."); 
      } 
      else { 
       System.out.println(execName + " could not be found."); 
      } 
     } 

     // remove by corporation 
     else if (option.toUpperCase().equals("RBC")) { 
      System.out.println("Enter the name of the corporation to remove: "); 
      execCompany = input.next(); 
      numRemoved = list.removeByCorporation(execCompany); 
      if (execCompany.equals(chairperson.getCompanyName())) { 
       System.out.println("Invalid command: cannot remove all employees from the chairperson's corporation"); 
      } 
      else if (numRemoved < 1) { 
       System.out.println("That corporation could not be found and no executives were removed."); 
      } 
      else { 
       System.out.println(numRemoved + " executive(s) from " + execCompany + " have been removed from the meeting."); 
      } 
     } 

     // prints by corporation 
     else if (option.toUpperCase().equals("PBC")) { 
      System.out.println("Enter the name of a corporation to display: "); 
      execCompany = input.next(); 
      list.printByCorporation(execCompany); 
     } 

     // prints all counter-clockwise 
     else if (option.toUpperCase().equals("PCC")) { 

      list.printAllCounterClockwise(); 
     } 

     // prints all clockwise 
     else if (option.toUpperCase().equals("PCL")) { 

      list.printAllClockwise(); 
     } 

     else if (option.toUpperCase().equals("EXT")) { 
      System.out.println("Terminating program..."); 
      break; 
     } 

     // Error message 
     else { 
      System.out.println("Please select a valid option."); 
     } 

    } 

} 



// displays menu and prompts user for input 
public static void ShowMenu() { 
    System.out.println("\nILC) Insert an executive to the left of the chairperson\nILE) Insert an executive to the left of a given executive\nIRE) Insert an executive to the right of a given executive\nRTE) Remove Target Executive"); 
    System.out.println("RBC) Remove By Corporation\nPBC) Print By Corporation\nPCC) Print all in counter-clockwise order\nPCL) Print all in clockwise order\nEXT) Exit the program\n\nSelect a menu option: "); 
} 

} 마지막

은 어떤 방식으로 모양이나 형태로 제안이나 조언이나 실제 도움이 모든 종류의를 제공 사람에게 감사합니다 . 나는 학생들이 숙제 질문을 할 때 어떤 이유로 든 화가났다는 것을 알고 있습니다. 왜냐하면 학생들이 "나를 위해 숙제를하라고"요청하고 있다고 생각하기 때문입니다.하지만 그것은 제가하는 일이 아닙니다. 나는 단지 조언이나 조언을 원할 뿐이다. 나는 네가 나를 위해 공백을 채우고 모든 것을 고치라고 요구하지는 않는다. (내가 그것에 반대하지는 않을 것이다 : P). 감사.

+0

우리에게 그것을 말하는 대신에 던져지고 예외를 제공하기 위해 더 나은 것은 – Rocky

+0

콘솔 충돌 : 의장의 회사 입력 DERP : 이 위원장의 이름을 입력 스레드에서 HERP 예외 " main "java.lang.NullPointerException \t at ExecutiveList. (ExecutiveList.java:23) \t at MeetingManager.main (MeetingManager.java:41) – Dim

답변

1

removeByCorporation 메서드에서 Executive를 null로 설정하고 있지만 이중 연결 목록이라고 생각하면 이전 및 다음 경영자의 참조를 설정해야한다고 생각하지 않으므로 이중 연결 목록에는 '휴식.

0

트릭은 모든 작업에서 변경중인 항목과이 항목을 참조하는 다른 두 항목을 업데이트하는 것입니다 (이중 연결된 목록에서 아주 쉽게 참조 할 수있는 두 항목도 참조하는 두 항목 임) .

각 방법을 확인하고 변경 사항마다 4 개의 필드 (제목에서 2 개, 제목에서 2 개씩)를 업데이트해야합니다.