2012-06-29 4 views
2

TruthTable 클래스 &을 입력 할 때 AND 게이트의 입력을 설정할 때 배열의 개별 슬롯에 액세스 할 수 없습니까?배열이있는 클래스로 작업하는 데 문제가 있습니다

threeAndGates.java 오류가

import java.util.Scanner; 

public class threeAndGates { 

    public static void main(String[] args){ 
     LogicGate and1 = new LogicGate(LogicGate.AND); 
     LogicGate and2 = new LogicGate(LogicGate.AND); 
     LogicGate and3 = new LogicGate(LogicGate.AND); 

     System.out.print("What is the number of Inputs? "); 
     Scanner scan = new Scanner(System.in); 
     int numOfInputs = scan.nextInt(); 

     System.out.print("What is the number of Outputs? "); 
     int numOfOutputs = scan.nextInt(); 

     TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs); 
     Table1.PopulateTruthTable(); 

     //below is where it is giving me "the type of the expression must be an array type but it resolves to TruthTable" 
     for(int r = 0; r<(Math.pow(2, numOfInputs)) ; r++){ 
     and1.setInput1(Table1[r][0]); 
     and1.setInput2(Table1[r][1]); 
     and2.setInput1(Truth1[r][2]); 
     and2.setInput2(Truth1[r][3]); 
     and3.setInput1(and1.getOutput()); 
     and3.setInput2(and2.getOutput()); 

     Table1[r][numOfInputs + numOfOutputs] = and3.getOutput(); 
     } 

     Table1.printTruthTable(); 
    } 
} 

TruthTable.java

public class TruthTable { 
    private int numOfInputs; 
    private boolean[][] table; 

    public TruthTable(int inputs, int outputs){ 
     this.numOfInputs = inputs; 
     int rows = (int) Math.pow(2,inputs); 
     int columns = inputs + outputs; 
     table = new boolean[rows][columns]; 
    } 

    public void printTruthTable(){ 
     for(int r = 0 ; r < table.length ; r++){ 
     for(int c = 0; c < table[r].length; c++) 
      System.out.printf("%-5b ", table[r][c]); 
     System.out.println(); 
     } 
    } 

    public String toString(){ 
     String outStr = new String(); 
     for(int r = 0; r < table.length; r++){ 
     for(int c = 0; c < table[r].length; c++) 
      outStr += String.format("%-5b ", table[r][c]); 
     outStr += '\n'; 
     } 

     return outStr; 
    } 

    public boolean[][] PopulateTruthTable(){ 
     String s; 
     String r =""; 
     int[] Line = new int[numOfInputs]; 
     boolean bit; 

     for (int i= 0; i < Math.pow(2,numOfInputs) ; i++){ 
     int x = numOfInputs - Integer.toBinaryString(i).length(); 
     for(int j = 0; j<x ; j++) 
      r += "0"; 
     s = r + Integer.toBinaryString(i); 
     for(int k=0; k<s.length() ;k++){ 
      Line[k] = s.charAt(k)-48; 
     } 

     for(int m=0 ; m<numOfInputs ; m++){  
      if(Line[m]==1) bit = true; 
      else bit = false; 
      table[i][m] = bit; 
     } 
     r=""; 
     } 
     return table; 
    } 
} 

답변

2

귀하의 TruthTable 클래스를 발생 -THE 클래스는 배열이 아니다. 그것은 배열을 포함합니다. 당신은 당신의 TruthTable 클래스에 get 및 set 메소드를 추가 할 수 있습니다

public boolean getValueAt(int x, int y) { 
    return this.table[x][y]; 
} 

public void setValueAt(int x, int y, boolean value) { 
    this.table[x][y] = value; 
} 

과 TruthTable 값으로 작동하는 것을 사용한다.


이것은 사용자의 문제와 관련이 없지만 클래스에서 변수의 이름을 지정할 때는 일반적으로 소문자를 사용합니다. 예를 들어, 당신은이 :

TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs); 

아마 가장

TruthTable truthTable = new TruthTable(numOfInputs,numOfOutputs); 

TruthTable table1 = new TruthTable(numOfInputs,numOfOutputs); 

더 나은 것 더 나은 당신이 아래로 읽을 수있는 쉬운이 될 것 것들에게 이름을 더 일치 도로.

+0

+! - 명명 규칙에 대한 좋은 팁! 앞으로 자신의 코드를 읽을 때 도움이 될뿐만 아니라 코드를 더 빨리 이해할 수 있으므로 사용자가 자신의 문제를 더 빨리 해결할 수 있습니다. –

+0

정확한 코드 강조 표시만으로도 처리가 훨씬 쉬워졌고 클래스 이름도 대문자로 시작해야한다는 것을 명심해야했지만 내 게시물이 이미 오래되었다고 생각했습니다. –

+0

나는 당신이 의미하는 것을 안다. 메소드 이름조차도 대문자로되어있다 : Table1.PopulateTruthTable();'( –

1

TruthTable 클래스는 다차원 배열이 아닙니다. 그것은 다차원 배열 필드를 가지고 있습니다. 그 때문에, 다음과 같은 구문을 사용할 수 없습니다 : 당신 TruthTable의 테이블 필드는 아직 공개, 이상이라면

tableInstance[x][y] 

를, 그것은 게터했다, 당신은 ... 대신이 같은 womthing 할 수

tableInstance.getTable()[x][y] 

일부 언어 (예 : C#)는 [] 인덱스 연산자 (또는 +, / 등)를 사용하는 동작을 정의 할 수있는 연산자 오버로드를 지원합니다. 이렇게하면 색인 생성 작업을 수행 할 수 있습니다. 불행하게도 Java에는이 기능이 없습니다.

1

이것은 답변보다 더 많은 의견이지만 더 많은 공간이 필요했습니다.

코드로 인해 나중에 문제가 발생하는 경우 제안 할 수 있습니까? Each method should do exactly one thing

또한 populateTruthTable을 2 ~ for "TruthTable 클래스의 메서드에 루프를 엮어서 main 메서드에서 호출해야합니다. Tell an object what to do rather than asking for it's data.

내가 잘못하고 있다고 말하려고하는 것이 아니라, 분명히 잘하고 있지만, 항상 그렇습니다. 당신이 따라갈 때 더 많은 코딩 트릭/연습을 선택하는 것이 좋으며, 이들이 편리 할 수준에있는 것처럼 보입니다.

+0

+1 매우 잘하고 건설적이다. –

관련 문제