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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: bit array and register problem

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default bit array and register problem

    I'm having some problems with my bit class and register class for a homework assignment. Here's the first part of the question and my code for it. The code compiles but it doesn't work with the register class. Here's the question for the bit class.

    You will write a class named Bit which will have one data field for storing the value of
    the bit. Since bits have only two possible values, you will use boolean for the data type.
    You will decide whether true has the value 1 or the value 0 for the bit, and this will not be
    communicated outside your Bit class (that is, use private instance variables).
    The following member methods and constructors must be implemented:
    1. Bit
    There will be two constructors for Bit objects. The first constructor will have no
    parameters and will construct a Bit object of the default value, and you will decide
    whether 0 or 1 is the default. The second constructor will have a single int parameter
    whose value is the value stored in the bit. (Actually, you will store true or false in the
    data field for the bit based on how you mapped true/false to 0/1.
    2. set
    This method makes the value stored in the Bit object equal to 1.
    3. reset
    This method makes the value stored in the Bit object equal to 0.
    4. flip
    This method changes the value stored in the Bit object.
    5. value
    This method returns an int with a value of 0 or 1, based on the value stored in the Bit
    object

    public class Bit
    {
    	private int bit;
    	public boolean Bit()
    	{
    		bit = 0;
    	}
    	public Bit(int bit)
    	{
    		this.bit = bit;
    	}
    	public int set()
    	{
    		return bit = 1;
    	}
    	public int reset()
    	{
    		return bit = 0;
    	}
    	public int flip()
    	{
    		if (bit == 0)
    		{
    			return bit = 1;
    		}
    		else
    		{
    			return bit = 0;
    		}
    	}
    	public int value()
    	{
    		return bit;
    	}
    }

    Here's the question for the second part, the register class.

    The central processing unit of a computer uses registers to store information. A register
    can be thought of as an array of bits. The operations we use in solving problems are converted
    into operations on registers. In this project, you will be implementing register operations,
    plus some other operations. In particular, you will write a class named Register. Register
    will have an array, each slot of that array will be of type Bit. This will be the most important
    data field of a Register object, and that array will have private access. The following member
    methods of Register will be implemented:
    1. LSR
    This is the logical shift right operation, it moves each bit one place to the right in the
    register. The leftmost bit has a 0 placed into it.
    2. LSL
    This is the logical shift left operation. It moves each bit one place to the left in the
    register. The rightmost bit has a 0 placed into it.
    3. ASR
    This is the arithmetic shift right operation. The only difference between this and LSR
    is that the value in the leftmost bit remains unchanged by the operation, that is, it is
    both shifted to the right and it is copied into itself.
    4. ASL
    This is the arithmetic shift left operation, and it has the exact same effect as the LSL
    operation.2
    5. COMP
    This is the complement operation, or the 1’s complement operation. It flips every bit.
    That is, if the bit has a value 1 it is changed to 0, and if it has a value 0 it is changed
    to a value 1.
    6. NEG
    This is the negate operation. The effect of this operation is to replace the contents of
    the register with its 2’s complement value. The easiest way to explain this operation
    is to take the 1’s complement, as in the COMP operation above, and then add 1. This
    in fact negates the contents of the register.
    7. ADD
    This is an operation on 2 registers. Its effect is to replace the contents of one of the
    registers with the sum of the values in the two registers.
    8. SUB
    This is the subtract operation on two registers. It has the effect of replacing the
    contents of one register with the result of subtracting the other register’s contents
    from its original contents. Subtraction is never performed directly. Instead, the register
    to be subtracted is copied into another temporary register, the NEG operation is
    performed on that temporary register, then the temporary register is added to register
    to be subtracted from.
    9. Register
    This method is the Register constructor. Its purpose is to construct a Register. The
    address, or reference, of that Register will be returned to the statement which called
    the constructor. There will be two constructors, both of course named Register, but
    with different signatures. There will be a constructor with one parameter which is the
    number of bits in the register. This constructor will create a Register object of the
    correct size in which every bit will have the value 0. There will also be a constructor
    with 2 parameters, the first is the number of bits as above, the second will be the
    value stored in the register. Note that this value must be converted to binary, and
    the individual bits stored in the register. Note also that this value may be positive or
    negative, but the NEG operation will be useful for any negative value.
    Internally, your Register object will represent the data fields of the register as an array
    of Bit objects. The Bit object will be discussed later in this document.
    10. showRegister
    This method will return a string whose value is the sequence of 1’s and 0’s stored in
    the register, displayed in order from left to right.3
    11. showValue
    This method will return an int whose value is the integer value stored in the register.
    12. size
    This method returns the number of bits in the register as an int.



    The problem I'm having with this code is that my array isn't working with the Bit class above. I'm pretty sure it's my lack of understanding of arrays and java in general but I'm hoping someone can shine a light on this problem for me. I also can't seem to make the methods I defined in the Bit class to work for the register class. Here's the code I have for the Register class.

     public class Register
    {
    	private static int i;
    	private static int size;
    	private static Bit[] bit = new Bit[size];
    	public static int LSR(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
    			bit[i + 1] = bit[i];
    			bit[0].reset();
    		}
    	}
    	public static void LSL(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
    			bit[i] = bit[i + 1];
    			bit[bit.length].reset();
    		}
    		bit[bit.length].reset();
    	}
    	public static void ASR(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
    			bit[i + 1] = bit[i];
    		}
    	}
    	public static void ASL(Bit[] bit)
    	{
    	for (i = 0; i < bit.length - 1; i++)
    		{
    			bit[i] = bit[i + 1];
    		}
    	}
    	public static void COMP(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
    			bit[i].flip();
    		}
    	}
    	public static void NEG(Bit[] bit)
    	{
    		bit = COMP(bit);
    		for (i = bit.length; i > 0; i--)
    			{
    				if (bit[i].equals(0))
    				{
    					bit[i].set();
    					i = 0;
    				}
    				else
    				{
    					bit[i].reset();
    				}
    			}
     
    	}
    	public static void ADD(Bit[] bit,Bit[] bit2)
    	{
    	for (i = bit.length; i > 0; i--)
    		{
    			if ((bit[i].equals(bit2[i])) && (bit[i].equals(1)))
    			{
    				bit[i].reset();
    				bit[i-1].flip();
    			}
    			else if ((bit[i].equals(bit2[i])) && (bit[i].equals(0)))
    			{
    				bit[i].reset();
    			}
    			else
    			{
    				bit[i].set();
    			}
    		}
    	}
    	public static void SUB(Bit[] bit, Bit[] bit2)
    	{
    	bit2 = NEG(bit2);
    	for (i = bit.length; i > 0; i--)
    		{
    			if ((bit[i].equals(bit2[i])) && (bit[i].equals(1)))
    			{
    				bit[i].reset();
    				bit[i-1].flip();
    			}
    			else if ((bit[i].equals(bit2[i])) && (bit[i].equals(0)))
    			{
    				bit[i].reset();
    			}
    			else
    			{
    				bit[i].set();
    			}
    		}
    	}
    	}
    	public static void Register(int size)
    	{
    		this.size = size;
    	}	
    	public static void Register(int size, int number)
    	{
    		this.size = size;
     
    	}
    	public static void showRegister(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
     
    		}
    	}	
    	public static void showValue(Bit[] bit1)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
     
    		}
    	}	
    	public static int size()
    	{
    		return bit.length;
    	}		
     
    }
    Last edited by ryu2le; August 31st, 2011 at 04:06 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: bit array and register problem

    Does your code even compile? It looks as though it should require a no-args constructor for the array declaration, and you seem to have messed up the no-args constructor in your Bit class, instead creating a method called Bit() that would return a boolean. There's an example of a no-argument constructor near the top of this page:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    You seem also to have not followed the instruction:
    you will use boolean for the data type

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    Please edit your post and remove the code tags from around the text. The folding of the lines makes it hard to read.
    The problem I'm having with this code is that my array isn't working with the Bit class above
    Can you explain what "isn't working" means.
    You get errors.
    The results are wrong. If so please show what the results are and explain what they should be.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    sean4u:

    I don't really understand what the boolean data type means. Does my professor want the output to be true/false or just 0/1?

    norm:
    The register class isn't compiling at all. I'm getting problems like incompatible type and cannot find symbol. I keep confusing how to use my Bit() class and int().

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    I'm getting problems
    Please copy and paste here the full text of the error messages.

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: bit array and register problem

    I don't really understand what the boolean data type means
    It's a 'primitive data type':
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    ----jGRASP exec: javac -g Register.java

    Register.java:11: cannot find symbol
    symbol : method reset()
    location: class Bit[]
    bit[0] = bit.reset();
    ^
    Register.java:21: incompatible types
    found : int
    required: Bit
    bit[bit.length] = 0;
    ^
    Register.java:41: incompatible types
    found : int
    required: Bit
    bit[i] = bit[i].flip();
    ^
    Register.java:46: COMP(Bit[]) in Register cannot be applied to (Bit)
    bit[i] = COMP(bit[i]);
    ^
    Register.java:57: cannot find symbol
    symbol : variable bit1
    location: class Register
    Bit[] bit3 = bit1[i] + bit2[i];
    ^
    Register.java:57: cannot find symbol
    symbol : variable bit2
    location: class Register
    Bit[] bit3 = bit1[i] + bit2[i];
    ^
    Register.java:57: incompatible types
    found : java.lang.String
    required: Bit[]
    Bit[] bit3 = bit1[i] + bit2[i];
    ^
    Register.java:65: operator - cannot be applied to Bit,Bit
    Bit[] bit3 = bit2[i] - bit1[i];
    ^
    8 errors

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    bit[0] = bit.reset();
    The variable bit is an array. Arrays do not have a method reset().

    What are you trying to do with that statement?

    bit[bit.length] = 0;
    What does the bit array contain? You can only assign values to elements in the array with the data that they are supposed to contain. 0 is an int not a Bit object.

    Most of these errors are of the same type. You are mixing apples and oranges.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I defined the reset() method in the class Bit() to set the value of the bit to 0.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    What is the variable: bit?
    Is it a Bit object?
    or is it an array of Bit objects?
    You need a Bit object to call the Bit class's methods.

    To get a Bit object from an array of Bit objects you'd code something like this: bit[ix]

  11. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I had the line
    private static Bit[] bit = new Bit[size];
    to create a Bit[] with the variable name bit with an array of Bit objects. I want to set the first bit ( bit[0] ) equal to zero, which is what I defined my reset() method is supposed to do.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    The code you posted did NOT use bit[0] as the object reference. It was used as a reference to the array.

    Go back and see how you coded it.

    You need to get a reference to the Bit object and call its method. Something like this:
    refToBitObject.itsMethod()

  13. #13
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I hate asking stupid questions but I just don't seem to understand.

    bit[0] refers to the bit[] array but isn't the bit[] array an array of the Bit object? Doesn't that mean that the bit[0] is also a Bit object?

    Or do I have to do something like this:
    private Bit bitVariableName = new Bit();

    So that bitVariableName refers to the Bit object. Though if I do this, then bitVariableName is no longer an array.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    Yes bit[0] is a Bit object and you should use that to call the Bit class's methods.
    bit is NOT a Bit object and you can NOT use that to call the Bit class's methods

    You coded: bit.reset();
    You need to code: bit[0].reset();

  15. #15
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    okay that makes sense. So now I have this
    	public static void LSR(Bit[] bit)
    	{
    	for (i = 0; i < bit.length; i++)
    		{
    			bit[i + 1] = bit[i];
    			bit[0] = bit[0].reset();
    		}
    	}

    but I'm getting the error:
    Register.java:11: incompatible types
    found : void
    required: Bit
    bit[0] = bit[0].reset();

    What does it mean by the "required: Bit" the bit[0] should still be a Bit object.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    What type does the bit reset() method return? The compiler sees that the reset method is void.
    You are assigning what that method returns to an element in the bit array which can only be assigned Bit objects.

    Think about what an assignment statement does.
    The right hand of the = must evaluate to or return a value.
    That value is assigned to the variable to the left of the =

  17. #17
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    My reset method is currently void. Should I change it to Bit?

    If I have a code like this
    	public int reset()
    	{
    		return bit = 0;
    	}

    then the method will return an integer. If I want it to return a Bit, should I do something like this?
    	public Bit reset()
    	{
    		return bit = bit.Bit(0);
    	}

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    BEFORE you write the code for a method you need to define what the method is to do.
    What is the purpose of the reset method?
    Does it create a new Bit object that you want to save in the bit array?
    Or does it change the contents of the Bit object it is in?
    Or what ?

  19. #19
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    the reset method is supposed to change the contents of the Bit object. It is supposed to change the value of the content to 0. That's why I originally though to use int object.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    If the reset method does NOT return anything then you should not be assigning it to a slot in the array:
    bit[0] = bit.reset(); // assigns what reset returns to a slot in the array

    If you want to reset the element at 0 code:
    bit[0].reset();

  21. The Following User Says Thank You to Norm For This Useful Post:

    ryu2le (August 31st, 2011)

  22. #21
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    Okay I think I fixed most of those errors. Now I have to create a Register method. What the professor wants is this:

    This method is the Register constructor. Its purpose is to construct a Register. The
    address, or reference, of that Register will be returned to the statement which called
    the constructor. There will be two constructors, both of course named Register, but
    with different signatures. There will be a constructor with one parameter which is the
    number of bits in the register. This constructor will create a Register object of the
    correct size in which every bit will have the value 0. There will also be a constructor
    with 2 parameters, the first is the number of bits as above, the second will be the
    value stored in the register. Note that this value must be converted to binary, and
    the individual bits stored in the register. Note also that this value may be positive or
    negative, but the NEG operation will be useful for any negative value.
    Internally, your Register object will represent the data fields of the register as an array
    of Bit objects. The Bit object will be discussed later in this document.


    If I interpret this correctly, I think he wants something like this:

    	public Register(int numberOfBits)
    	{
    		Bit[] bit = new Bit[numberOfBits];
    	}	
    	public Register(int numberOfBits, int valueStored)
    	{
    		Bit[] registerBit = new Bit[numberOfBits];
    		//code to parse valueStored from int to binary and store it in registerBit[]?
    	}

    but when I try to compile that, I get this error:

    ----jGRASP exec: javac -g Register.java

    Register.java:101: class, interface, or enum expected
    public static void Register(int numberOfBits)
    ^
    Register.java:104: class, interface, or enum expected
    }
    ^
    Register.java:105: class, interface, or enum expected
    public static void Register(int numberOfBits, int valueStored)
    ^
    Register.java:110: class, interface, or enum expected
    }

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    What code is at the lines in the error messages?
    It sounds like a mismatched pair of {}s. Check that all {s have a matching } where it should be.
    Also check for matching pairs of [] and ().

    Why is the posted text formatted with so many short lines?
    You could take the time to make the text easier to read by removing all the extra line ends so there are not so many short lines.

  24. #23
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I found it. There was an extra } before this statement.
    Last edited by ryu2le; August 31st, 2011 at 04:35 PM.

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: bit array and register problem

    That's the most common cause.
    Another is code statements outside of a method.

  26. #25
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    for
    	public Register(int numberOfBits)
    	{
    		Bit[] bit = new Bit[numberOfBits];
    	}
    I'm supposed to set the array to all values of 0. I thought I did this when I did:
    	public Bit()
    	{
    		bit = 0;
    	}

    however, I'm getting null values for the entire array. Is the Bit() method not doing what I think I'm telling it to do.

Page 1 of 2 12 LastLast

Similar Threads

  1. How to validate username?this my register.jsp
    By surendran610 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 22nd, 2011, 01:42 AM
  2. Replies: 1
    Last Post: May 20th, 2011, 03:58 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Cash Register Exercise
    By Consus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 11:52 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM