Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 26th, 2013, 09:29 PM
puckandtravel puckandtravel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 puckandtravel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 26th, 2013, 09:58 PM
puckandtravel puckandtravel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 puckandtravel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
       }
    }
}


Reply With Quote
  #3  
Old February 26th, 2013, 11:40 PM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Hint: the operator for the exclusive-OR is the ^

Reply With Quote
  #4  
Old February 26th, 2013, 11:45 PM
puckandtravel puckandtravel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 puckandtravel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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'))

Reply With Quote
  #5  
Old February 27th, 2013, 04:39 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - XOR operation simulation

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap