Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
February 26th, 2013, 09:29 PM
 puckandtravel
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 49 m 56 sec
Reputation Power: 0
Homework - XOR operation simulation

I have to do the following in Java:

Quote:
 Write a program that simulates an XOR operation. The input should be a word representing a binary number (0s and 1s). Your program should XOR all the digits from left to right and output the results as "True" or "False." In an XOR operation, a XOR b is true if a or b is true but not both; otherwise, it is false. In this program, we will consider the character "1" to represent true and a "0" to represent false. For instance, if the input is 1011, then the output will be 1 (XOR 0 is 1, then 1 XOR 1 is 0, then 0 XOR 1 is 1, which causes the output to be "True.") You can assume that the input word is guaranteed to contain only 0s and 1s.

I'm just not even sure where to begin. Could someone point me in the right direction?

I think I can figure out how to perform an XOR on two characters, but I'm not sure how to get those characters out of the string in the manner I need. I know how to get a character from a certain position in the string, but I need to do more than that I think.

This assignment comes from a chapter on loops, so I assume the solution contains at least one loop. Material covered in class so far includes data types, variables, constants, arithmetic operators, classes, if, if/else, if/else/if, switch, and loops.

#2
February 26th, 2013, 09:58 PM
 puckandtravel
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 49 m 56 sec
Reputation Power: 0
I think I actually may have just gotten it because the actual outputs match my expected outputs, unless I just have no clue how to evaluate the binary numbers with XOR by hand.

Does this look right?

Code:
```

//Import the Scanner class
import java.util.Scanner;

public class XOR
{
public static void main (String[] args)
{
//Instantiate a Scanner object
Scanner scan = new Scanner(System.in);

//Ask user for a binary number
System.out.print("Enter a binary number (0s and 1s) > ");
String binaryNumber = scan.next();

//Declare a variable for the index of the last character
int lastIndex = binaryNumber.length() - 1;

//Declare variables for characters
char character1 = binaryNumber.charAt(0);
char character2;

//Run a for loop for each character of the string
for (int i = 0; i < lastIndex; i++)
{
//Set character2 to the character to the right of position i
character2 = binaryNumber.charAt(i + 1);

//XOR character1 and character2
if ((character1 == '0' || character2 == '0') && !(character1 == '0' && character2 == '0'))
{
character1 = '1';
}
else
{
character1 = '0';
}

}

//If the XOR operation on the string results in 1, print true; otherwise, print false.
if (character1 == '1')
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}
}

```

#3
February 26th, 2013, 11:40 PM
 Aurum84
Contributing User

Join Date: Nov 2012
Posts: 74
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Hint: the operator for the exclusive-OR is the ^

#4
February 26th, 2013, 11:45 PM
 puckandtravel
Registered User

Join Date: Feb 2013
Posts: 3
Time spent in forums: 49 m 56 sec
Reputation Power: 0
Quote:
 Originally Posted by Aurum84 Hint: the operator for the exclusive-OR is the ^
I've missed class several times, but as far as I know, we haven't learned that, so I don't think we're expected to use that operator. Plus, we're supposed to simulate that operation, so I don't think we're supposed to actually use it.

Wouldn't this code basically be doing what the ^ operation would do?

Code:
`((character1 == '0' || character2 == '0') && !(character1 == '0' && character2 == '0'))`

#5
February 27th, 2013, 04:39 AM
 Aurum84
Contributing User

Join Date: Nov 2012
Posts: 74
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Change the '0' to a '1' and you will be correct.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - XOR operation simulation