Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Party Bits problem

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Party Bits problem

    Alright So I am working on the first problem of this assignment and I have hit a road block. When my answer prints out it puts both a 1 and a 0 on and I only want it to do wone or the other depending on whether or not the number of ones is even or odd.
    This is my code
    public class message
    {
        private String parity1;
        private int countOfOnes;
        public message(String Parity1)
        {
            parity1 = Parity1;
            int countOfOnes;
     
        }
     
        public String getParity()
        {
            return parity1;
        }
     
        public void setParity(String Parity1)
        {
            parity1 = Parity1;
        }
     
        public int countOnes()
        {
           String abc = parity1;
           int countOfOnes = abc.length()-abc.replaceAll("1","").length();
           return countOfOnes;   
        }
     
        public String sendWithEvenParity()   
        { 
     
           if(countOfOnes %2 == 0)
           {
                parity1 = parity1 + "1";
     
            }
     
            else
            {
                parity1 = parity1 + "0";
            }
           return parity1;
     
        }
     
        public String sendWithOddParity()
        {
     
            if(countOfOnes%2 == 0 )
            {
                parity1 = parity1 + "0";
            }
            else
            {   if(countOfOnes%2 == 1 )
                    parity1 = parity1 + "1";
            }
     
            return parity1;
       }
     
       public String toString()
        {
            return parity1;
        }
     
     
    }
    Attached Files Attached Files


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Party Bits problem

    Nowhere in your code do I see a print statement.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Party Bits problem

    It has a driver class that out puts the info
    public class parityFun
    {
        public static void main(String[]args)
     {
        message parity = new message("1111111");
        message parity2 = new message("1111110");
     
        System.out.println("Original Message: "+ parity);
     
        parity.sendWithEvenParity();
        parity.getParity();
        System.out.println("\tWith even Parity: " + parity);
     
     
        parity.sendWithOddParity();
        parity.getParity();
        System.out.println("\tWith odd Parity: " + parity);
     
        System.out.println("Original Message: "+ parity2);
     
        parity2.sendWithEvenParity();
        parity2.getParity();
        System.out.println("\tWith even Parity: " + parity2);
     
        parity2.sendWithOddParity();
        parity2.getParity();
        System.out.println("\tWith odd Parity: " + parity2);
     
     }
    }

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Party Bits problem

    So the error occurs in the code you didn't originally post! Do you think posting relevant information would make it easier for us to help you?

    When you want to do one thing or another what kind of statement would you use?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Party Bits problem

    Well I assumed my error occurred in the other class not the driver class. I would use an if-else statement if I wanted to do one thing or another

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Party Bits problem

    What happened when you tried?
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Party Bits problem

    Original Message: 1111111
    With even Parity: 11111111
    With odd Parity: 111111110
    Original Message: 1111110
    With even Parity: 11111101
    With odd Parity: 111111010


    that is what happened the first output is correct but all others thereafter are incorrect I'm not sure why this is.

Similar Threads

  1. How to view and edit the bits of a jpg image file?
    By enakta13 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: March 26th, 2013, 03:44 PM
  2. get gray scale value from 16-bits image
    By div111 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 16th, 2013, 07:52 AM
  3. How to get data from a third party website
    By Duff in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 03:24 PM
  4. [SOLVED] Using 3rd Party JAR Files
    By oss_2008 in forum Java Theory & Questions
    Replies: 12
    Last Post: June 12th, 2009, 10:27 AM