2016-05-31 16 views
0

私の最後の目的は、メソッドmatches()を作成するよう求めます。それは、別のGenericMemoryCellをパラメータとして受け取り、格納された値の両方が現在のGenericMemoryCellの格納された値で見つかる場合、真を返します。格納された値の順序は重要ではありません。メソッドの作成同じクラスの別のインスタンスを渡して、格納されている値を現在の格納されている値と比較します

は難しいことではありませんでしたが、私はGenericMemoryCellの別のインスタンスを渡すという概念のまわりで私の頭をラップすることはできませんのでmain()からそれを呼び出す方法に失われています。最初にstoredValueAstoredValueBという別のペアを取得するのはどこですか? matches()はプログラム全体の仮想インスタンスを「実行中」ですか?

import java.util.*; 

public class GenericMemoryCell<T>{ 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter valueA: "); 
     String readerA = input.next(); 
     System.out.print("Enter valueB: "); 
     String readerB = input.next(); 

     GenericMemoryCell<String> values = new GenericMemoryCell<>(readerA, readerB); 
     System.out.println("storedValueA: " + values.readA()); 
     System.out.println("storedValueB: " + values.readB()); 

     values.writeA(readerA); 
     values.writeB(readerB); 
    } 

    public GenericMemoryCell(T storedValueA, T storedValueB) 
    { this.storedValueA = storedValueA; this.storedValueB = storedValueB; writeA(storedValueA); writeB(storedValueB); } 

    public T readA() 
    { return storedValueA; } 

    public T readB() 
    { return storedValueB; } 

    public void writeA(T x) 
    { storedValueA = x; } 

    public void writeB(T y) 
    { storedValueB = y; } 

    public boolean matches(GenericMemoryCell<T> that){ 
     return (this.storedValueA.equals(that.storedValueA) && this.storedValueB.equals(that.storedValueB)); } 

    private T storedValueA, storedValueB; 
} 
+0

... – Leo

+0

@Leo、方法は、すべての(何が静的ではない)呼び出しを行うオブジェクトを操作する必要があります。 – mzeeirka

答えて

1

私はあなたがうまくいけば、この

public class GenericMemoryCell { 

    public static void main(String[] args) { 
     GenericMemoryCell g1 = new GenericMemoryCell(); 
     //set g1 values here 

     GenericMemoryCell g2 = new GenericMemoryCell(); 
     //set g2 values here 

     System.out.println(g1.matches(g2)); 

    } 

    public boolean matches(GenericMemoryCell g) { 
     //implement the logic here 
     return ...; 
    } 
} 
0

ようなものが必要だと思う、それはあなたのために働くかもしれません。しかし、システムが繰り返し入力を求めたい場合は、何らかの種類のloopが必要です。私はあなたが静的および非静的メソッドの違いを理解する必要があると思う

public class GenericMemoryCell { 

    public static void main(String[] args) { 
     List<Integer> list = new ArrayList<Integer>(); 
     Scanner scanner = new Scanner(System.in); 
     System.out.println("Please enter first input: "); 
     int firstInput = scanner.nextInt(); 
     System.out.println("Please enter second input"); 
     int secondInput = scanner.nextInt(); 
     list.add(firstInput); 
     list.add(secondInput); 

     Scanner scannerObj = new Scanner(System.in); 
     System.out.println("Please enter first input: "); 
     int firstArg = scannerObj.nextInt(); 
     System.out.println("Please enter second input: "); 
     int secondArg = scannerObj.nextInt(); 
     boolean isMatches = isInputMatches(firstArg, secondArg, list); 
     if (isMatches) { 
      System.out.println("These inputs were already stored before. Please try again with different inputs"); 
     } else { 
      System.out.println("The inputs are successfully stored. Thank you."); 
     } 
     scanner.close(); 
     scannerObj.close(); 

    } 

    private static boolean isInputMatches(int firstArg, int secondArg, List<Integer> list) { 
     return list.contains(firstArg) && list.contains(secondArg); 
    } 
} 
関連する問題