Goals
1)
Be able to work with individual bits in java
.
2)
Understand the serializable interface.
3)
Understand the comparable interface.
4)
Answer questions about a general-purpose class to be developed.
5)
Understand the use of a driver program for ‘glass box’ debugging.
6)
Develop a program that can grade true/false tests.
Synthesis Questions
1.
Why is it a good idea to test a general class with a driver program before trying to use it?
2.
What is the difference between black box and glass box debugging?
3.
In pseudo-code, describe
CreateTest.java.
4.
In pseudo code, describe the driver program that is provided below.
5.
If x = 37, y=23, z=-110, r=-32 answer the following questions. Assume that we are working with bytes. To answer these questions, you will need to convert from decimal to binary. Remember that
System.out.println
DOES NOT print binary values. You will need to convert your results to decimal. Of course you can verify your results by computer. Remember that there will be questions like this on the quiz, so try them yourself first.
a.
System.out.println
(
x&y
);
b.
System.out.println
(
x^y
);
c.
System.out.println
(~
x|y
);
d.
System.out.println
(~x>>2);
e.
System.out.println
((byte)(z<<(~r>>4)));
f.
System.out.println
((byte)(x + (x<<1) + (x<<2) + (x<<3)));
g.
System.out.println
(y>>>3);
h.
System.out.println
(
x&y|z^r
);
i.
System.out.println
(~x + x);
j.
System.out.println
(x|(3<<4));
6.
In
about
a sentence, give a reasonable example of when the following operators would be useful when working with strings of bits.
a.
or operation (|):
b.
and operation (&):
c.
exclusive or operation (^):
d.
left shift operation: (<<):
7.
Suppose we need to create a multiple-choice exam, where each question has four possible choices. Briefly describe (in a few sentences) how we could create a new class that uses inheritance that would allow this. Giving class API and a brief description of the needed methods would also suffice.
8.
Suppose we needed to modify the
BitMap
class to handle a bit stream of up to
16384
bits. Briefly describe the changes necessary to handle this refinement. Can you think of a way that the maximum number of bits to be handled is completely flexible?
Description
1)
The first step is to develop a general-purpose class that will be able to perform operations on strings of bits. The class API follows:
public class
BitMap
implements Comparable,
Serializable
{ public static final int BITSIZE = 64;
private
long bitString;
public BitMap
() // Three constructors.
public
BitMap
(String s)
throws
IndexOutOfBoundsException,ArithmeticException
public
BitMap
(
boolean
[] bits)
throws
IndexOutOfBoundsException
private
long
bitMask
(
int
b) // Other class methods.
public void
setBit
(
int
b)
public void
clearBit
(
int
b)
public
boolean
checkBit
(
int
b)
public
int
countTrue
()
public void
clearAll()
public void
setAll()
.
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Goals1)Be able to work with individual bits in java.2).docx
1. Goals
1)
Be able to work with individual bits in java
.
2)
Understand the serializable interface.
3)
Understand the comparable interface.
4)
Answer questions about a general-purpose class to be
developed.
5)
Understand the use of a driver program for ‘glass box’
debugging.
6)
Develop a program that can grade true/false tests.
Synthesis Questions
1.
Why is it a good idea to test a general class with a driver
program before trying to use it?
2.
What is the difference between black box and glass box
debugging?
3.
2. In pseudo-code, describe
CreateTest.java.
4.
In pseudo code, describe the driver program that is provided
below.
5.
If x = 37, y=23, z=-110, r=-32 answer the following questions.
Assume that we are working with bytes. To answer these
questions, you will need to convert from decimal to binary.
Remember that
System.out.println
DOES NOT print binary values. You will need to convert your
results to decimal. Of course you can verify your results by
computer. Remember that there will be questions like this on
the quiz, so try them yourself first.
a.
System.out.println
(
x&y
);
b.
System.out.println
(
x^y
);
c.
System.out.println
(~
x|y
);
d.
4. or operation (|):
b.
and operation (&):
c.
exclusive or operation (^):
d.
left shift operation: (<<):
7.
Suppose we need to create a multiple-choice exam, where each
question has four possible choices. Briefly describe (in a few
sentences) how we could create a new class that uses
inheritance that would allow this. Giving class API and a brief
description of the needed methods would also suffice.
8.
Suppose we needed to modify the
BitMap
class to handle a bit stream of up to
16384
bits. Briefly describe the changes necessary to handle this
refinement. Can you think of a way that the maximum number
of bits to be handled is completely flexible?
Description
1)
The first step is to develop a general-purpose class that will be
able to perform operations on strings of bits. The class API
follows:
public class
BitMap
5. implements Comparable,
Serializable
{ public static final int BITSIZE = 64;
private
long bitString;
public BitMap
() // Three constructors.
public
BitMap
(String s)
throws
IndexOutOfBoundsException,ArithmeticException
public
BitMap
(
boolean
[] bits)
throws
IndexOutOfBoundsException
private
long
bitMask
(
int
b) // Other class methods.
public void
7. compareTo
(Object
bm
) //For Comparable.
public
boolean
equals(
BitMap
bm
)
public String
toString()
}
Notes:
a.
The only instance variable that is needed is
bitString
.
b.
Use BITSIZE for the maximum index value of your loops.
The above looks like a lot of methods, but the whole class
requires about a page of code when the methods are filled in.
Some methods can use others instead of duplicating code. For
example, the first constructor can just call the method,
clearAll
(
)
. The method
b
itMask
8. (
)
can be used by four or five other methods whenever a bit
operation is called for. We'll discuss that in class.
The operations to be performed by each method are briefly
described below:
a)
Constructors
(i) The first constructor just needs to set all bits to
false
(or
zero
). The method
clearAll()
does the same thing.
(ii) The second constructor takes a character string as input.
Each character of the string is either a
t
(for
true
) or
f
(for
false
) value. The bits with a
t
character are to be set on in the bit map; bits with an
f
character should be set off in the bit map. Throw an
ArithmeticException
if the input string has characters other
than ‘t’
, ‘T’, ‘f’, or ‘F’ appear. Throw
IndexOutOfBoundsException
if the string is too long.
9. (iii) The third constructor works just like the second except the
input is a
boolean
array. The bits corresponding to the array elements that have a
true
value should be set to a value of one;
the elements having a
false
value shoule
be
set to a value of zero.
b)
Primary Methods
The methods,
setBit(int), clearBit(int), and checkBit(int)
respectively set a given bit on, off or check a bits current value.
The method,
countTrue()
returns the total number of bits that are set. It can be used by
the
compareTo()
method. The method
setAll(
)
turns on all the bits;
clearAll
()
clears all bits. Both
setAll
(
)
and
clearAll
()
methods can be written with one instruction.
10. c)
Comparable interface
The
compareTo()
and
equals()
methods should be provided to conform to the standard way
that java programs compare objects. One
BitMap
object is considered less than another if it contains less
true
bits. In effect, this method of comparison can be used to
determine if one BitMap object has more bits on than another.
The
equals(
)
method can compare whether two
BitMaps
have all of their
on
and
off
bits in the same positions.
d) The
toString(
)
method should return an appropriate string of 't' and 'f' values.
The System.out.print methods use this method.
2)
The next step is to debug the class that was created in the above
step. I provide the program
driver.java
for this purpose; its code is at the bottom of this document.
Don’t modify this program in any way; use it to test your
class.
11. It contains a menu that has options to test every option. Once
the testing is complete,
BitMap
,
could be used as a general tool for working with bits and could
be used in many programs.
3)
Use notepad to create a file of true/false questions.
4)
Now write a program (
CreateTest.java)
that constructs a
true/false
test. This program reads the file created in step 3 to ask a
series of true false questions and record the resulting answers in
a bit map object. Be sure to use a
fileChooser
to ask the user for the file name. Make sure you catch all
exceptions (programs should never crash).
You can use your TextReader from the previous lab as the
starting point for this program. Better yet, just instantiate a
BufferedReader
and read lines from the text file till you encounter a null line.
Make sure to have at least
25
questions in your test. When the test is completed, the program
should use an
ObjectOutputStream
to write one record (
the BitMap object
) to a sequential binary file. That is why the
BitMap
class must have ‘implements
12. serializable
’ on its signature line. Name the disk file
ans.bin.
Hopefully, you'll know the answers to your questions so the
answer file will represent a perfect score.
5)
Finally, create a new application (Test.java). You can copy and
paste the program that you just wrote (and save it as
Test.java
), and then modify it appropriately. This program should read
the answer file (using an
ObjectInputStream
) and compare the answers given by someone taking the test to
ans.bin
. Display the score earned by the test taker.
6)
Answer the synthesis questions in an rtf or doc file (answers.rtf
or answers.doc).
Type your name and the lab number on this file and include the
questions with the answers.
7)
Zip your Eclipse project along with the synthesis answers and
email to
[email protected]
.
Driver.java
// Driver program to test the
BitMap
class.
import
java.io.*;
24. }
// End menu().
// Method to accept a bit number.
public
static
int
getBit
()
{
int
bit;
System.out.print
(
"Enter bit number: "
);
bit =
readInt
(0,BitMap.BITSIZE-1);
return
bit;
}
// End
getBit
().
// Method to instantiate either a
boolean
or string bit map.
public
29. ;
}
// End
getMap
().
// Method to get an integer between min and max.
public
static
int
readInt
(
int
min,
int
max)
{ String token;
int
value = 0;
boolean
ok =
false
;
while
(!ok)
{ ok =
true
;
30. try
{ token =
in.readLine
();
value =
Integer.parseInt
(token);
if
(valuemax) ok =
false
;
}
catch
(Exception exception) {ok =
false
;}
if
(!ok)
{
System.out.print
(
"Illegal input, enter between "
+ min +
" and "
+ max +
": "
);
} }
return
value;