[Pytanie] Poczatkujacy blad algorytmu

0

Witam, to bedzie moj pierwszy post , dopiero zaczynam nauke javy wiec spotakalem sie z bledami w kodzie prostego algorytmu, musze znalezc bledy:

public static void main(String[] args) {
			int firstValue = 0;
			secondValue;
			String operator = "";
			int proposedResult;
			int realResult
			
			// depending on the number of provided arguments use appropriate case block
			switch(args.length) {
				case 4 :
					operator = args[0];
					firstValue= fromBinaryString(args[1]);
					proposedResult = fromBinaryString(args[3]);
					break;
				case 5 :
					firstValue = fromBinaryString(args[0]);
					operator = args[1];
					secondValue = fromBinaryString(args[2]);
					proposedResult = fromBinaryString(args[4]);
					break;
				default :
					System.err.println("Wrong argument count - only four or five arguments allowed!");
					System.err.println("\tExamples:");
					System.err.println("\t\t0101 AND 1111 = 0000");
					System.err.println("\t\tNOT 1111 = 0000");
					System.exit(0);
			}
			// count the real value
			if (operator.equals("AND")) {
				realResult = firstValue & secondValue;
			} if else (operator.equals("NOT")) {
				realResult = ~firstValue;
			} else {
				System.err.println("ERROR! Unknown operator: "+operator);
				System.exit(0);
			}
			// display the results
			if (proposedResult == realResult) {
				System.out.println("CONGRATULATIONS! "+args[0]+" "+args[1]+" "+args[2]+" is "+toBinaryString(realResult));
			} else {
				System.out.println("BAD ANSWER! "+args[0]+" "+args[1]+" "+args[2]+" is "+toBinaryString(realResult)+" not "+proposedResult);
			}
static int fromBinaryString(String s) {
			int result = 0;
			for (int i = 0; i < s.length(); i++) {
				char c = s.charAt(i);
				if      (c == '0') result = 2 * result;
				else if (c == '1') result = 2 * result + 1;
			}
			return result;
		}

		static String toBinaryString(int n) {
			String s = "";
			for (int sh = 31; sh >= 0; sh--) {
				s += ((n >> sh) & 1);
			}			
			return s;
		}
		}

Dodatkowo musze dopisac kod obsługujący pozostałe operacje binarne (OR, XOR) oraz arytmentyczne (+, -, ...).

Wpisalem kod do eclipse i takie podstawowe bledy jak brak";" to oczywiscie wychwycilem natomiast nie wiem co ma wykonywac ten algorytm w zwiazku z czym nie wiem co zrobic z wartosciami propesedresult i realresult. Jezeli jest to mozliwe poprosze o poprawny kod (nie wime jakie zwyczaje panuja na forum) i jezeli ktos bylby na tyle mily i chociaz tak po krotce wyjasnil co ten kod wykonuje. Z gory dziekuje za odpowiedz
0

Błędy:

secondValue; //-> brak typu
int realResult //-> brak ;
} if else (operator.equals("NOT")) { //-> błędna kolejność

static String toBinaryString(int n)
static int fromBinaryString(String s) //-> te 2 funkcje powinny być poza main

secondValue, proposedResult, realResult //-> powinny być za inicjalizowane

Co do algorytmu to wykonuje operacje na bitach takie jak (AND, NOT)

Co do dopisania kodu to w tym miejscu

if (operator.equals("AND")) {
    realResult = firstValue & secondValue;
} else if (operator.equals("NOT")) {
    realResult = ~firstValue;
} else {
    System.err.println("ERROR! Unknown operator: " + operator);
    System.exit(0);
}

musisz dopisać odpowiednie operacje

PS:
nie wiem czy to zaliczasz do błędu ale

if (proposedResult == realResult) {
    System.out.println("CONGRATULATIONS! " + args[0] + " " + args[1] + " " + args[2] + " is " + toBinaryString(realResult));
} else {
    System.out.println("BAD ANSWER! " + args[0] + " " + args[1] + " " + args[2] + " is " + toBinaryString(realResult) + " not " + proposedResult);
}

powinno być rozróżnione od ilości argumentów

0

Ok dzieki troche mi to rozjasniles , ide sie pobawic :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1