Class StringOfBits java.lang.Object StringOfBits Represents a string of bit valu
ID: 3687647 • Letter: C
Question
Class StringOfBits
java.lang.Object
StringOfBits
Represents a string of bit values (0 or 1).
Constructor Summary
Constructs the empty bit string; length() == 0.
Constructs a bit string from a sequence of '0' and '1' characters.
Copy constructor.
Method Summary
Appends the bit string representation of the parameter to this bit string; false corresponds to 0, true corresponds to 1.
Appends the bit string representation of the parameter to this bit string.
Appends the bit string representation of the parameter to this bit string.
Appends the bit string representation of the parameter to this bit string.
Appends the parameter to this bit string.
Returns a boolean corresponding to the bit at the specified index.
Returns a char corresponding to the bit at the specified index.
Returns an int corresponding to the bit at the specified index.
Returns the length of this bit string.
Sets the bit at the specified index.
Sets the bit at the specified index.
Sets the bit at the specified index.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
Constructor Detail
StringOfBits
Constructs the empty bit string; length() == 0.
StringOfBits
Copy constructor.
Parameters:
sb - the object to be cloned
StringOfBits
Constructs a bit string from a sequence of '0' and '1' characters. If the parameter is not comprised of '0' and '1' characters the behavior is undefined.
Parameters:
chars - the sequence to convert into bits
Method Detail
length
Returns the length of this bit string.
Returns:
the number of bits in this string
append
Appends the bit string representation of the parameter to this bit string. If the parameter is not '0' or '1' the behavior of this method is undefined.
Parameters:
c - the bit to append; must have value '0' or '1'
Returns:
a reference to this bit string
append
Appends the bit string representation of the parameter to this bit string; false corresponds to 0, true corresponds to 1.
Parameters:
b - the bit to append
Returns:
a reference to this bit string
append
Appends the bit string representation of the parameter to this bit string. Each digit represents a single bit. If any digit of the parameter is not 0 or 1 the behavior is undefined.
Parameters:
i - an integer representation of a bit sequence
Returns:
a reference to this bit string
append
Appends the bit string representation of the parameter to this bit string. Each substring of "0" corresponds to 0; each "1" corresponds to 1. If the string contains other than "0" and "1" the behavior of this method is undefined.
Parameters:
str - a string
Returns:
a reference to this bit string
append
Appends the parameter to this bit string.
Parameters:
bitstr - a bit string to be appended
Returns:
a reference to this bit string
charAt
Returns a char corresponding to the bit at the specified index.
Parameters:
index - the index of the desired bit value
Returns:
the char value at the specified index
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length()
intAt
Returns an int corresponding to the bit at the specified index.
Parameters:
index - the index of the desired bit value
Returns:
an int value corresponding to specified index (0 == 0; 1 == 1)
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length()
booleanAt
Returns a boolean corresponding to the bit at the specified index.
Parameters:
index - the index of the desired bit value
Returns:
the boolean value at the specified index (0 == false; 1 == true)
Throws:
IndexOutOfBoundsException - if index is negative or greater than or equal to length()
setBitAt
Sets the bit at the specified index. If the parameter is not '0' or '1' the behavior of this method is undefined.
Parameters:
index - the index of the bit to modify
c - the new value ('0' == 0; '1' == 1)
setBitAt
Sets the bit at the specified index. If the parameter is not 0 or 1 the behavior of this method is undefined.
Parameters:
index - the index of the bit to modify
i - the new value (0 == 0; 1 == 1)
setBitAt
Sets the bit at the specified index.
Parameters:
index - the index of the bit to modify
b - the new value (false == 0; true == 1)
toString
Overrides:
toString in class Object
Constructors Constructor and Description StringOfBits()Constructs the empty bit string; length() == 0.
StringOfBits(CharSequence chars)Constructs a bit string from a sequence of '0' and '1' characters.
StringOfBits(StringOfBits sb)Copy constructor.
Explanation / Answer
java.lang.Object
It is a parent class to any class in java.
for example:
A.java
class A{
}
In this i didn't use extends key word to make objective class as a parent to A.java
How we can say that it is parent class in java?
ans: observe here when ever we compile (javac A.java) any program. the command (javac A.java) invokes compiler then compiler verifies java syntex in java program and
check a program has a parent or not. At compilation compiler adds java.lang.Object class as a parent to the A.java. So this is direct parent class.
Then converts into:
class A extends java.lang.Object {
}
agin if we worte B.java as a child program to the A.java then the process is that
java.lang.Object (indirect parent class)
|
A
|
B
Object class contains common methods which are required for any class in java, it has 11 methods.
--------------------------------------------------------------------------------------
In this you will learn how you could calculate the length of any string which you have stored in the object.
Now if you need to calculate the length of the string that how many characters are presented into the string.
So for doing this one simple thing which you will do it that you could just print out through referenced object (s1.length()) as below.
So this length() method represented within the string class, you could manipulate the object s1 for calculating the string of that string that we have created.
example program: abcd.java
public class abcd {
public static void main(String [] args) {
String s1 = "Hemanth Raju";
System.out.println(s1.length()); // s1.length() method going to print number of characters in that string i.e., ["Hemanth Raju" = 12], Here space is also calculated.
}
}
output:
12
--------------------------------------------------------------------------------------
append():
suppose we have write a string called 'Hemanth' and you want to add 'Raju' to it instead of deleting entire string restrating and typing. so we can also do it on runtime simply by append() method
example prgarm:
import class Main {
public static void main(String args[]) {
String first;
String last;
String full;
Scanner sc = new Scanner (System.in);
StringBuffer sb = new StringBuffer();
first = sc.nextLine();
last = sc.nextLine();
full = sb.append(first + " " + last).toString(); // Here is the append method which adds first, last names each.
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------