I need help writing two classes, BitInputStream and BitOutputStream. I will prov
ID: 3733315 • Letter: I
Question
I need help writing two classes, BitInputStream and BitOutputStream. I will provide the skeleton code for both classes. BitInputStream is supposed to be able to read individual bits from a file. BitOutputStream is supposed to take in a charActer, and be able to write the bits to an output file.
BitInputStream
import java.io.FileInputStream;
import java.io.IOException;
public class BitInputStream {
// add additional protected variables as needed
// do not modify the public methods signatures or add public methods
protected DataInputStream d;
public BitInputStream(String filename) {
try {
d = new DataInputStream(new FileInputStream(filename));
} catch (IOException e) {
}
}
public int readBit() {
// return the next bit in the file
}
public void close() {
}
}
BitOutputStream
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BitOutputStream {
// add additional protected variables as needed
// do not modify the public methods signatures or add public methods
protected DataOutputStream d;
public BitOutputStream(String filename) {
try {
d = new DataOutputStream(new FileOutputStream(filename));
} catch (IOException e) {
}
}
public void writeBit(char bit) {
// PRE: bit is a '0' or a '1'
}
public void close() {
}
}
Explanation / Answer
BitInputStream
import java.io.FileInputStream;
import java.io.IOException;
public class BitInputStream {
// add additional protected variables as needed
// do not modify the public methods signatures or add public methods
protected DataInputStream d;
public BitInputStream(String filename) {
try {
d = new DataInputStream(new FileInputStream(filename));
} catch (IOException e) {
}
}
public int readBit() {
}
public void close() {
if(d!=null)
d.close();
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BitOutputStream {
// add additional protected variables as needed
// do not modify the public methods signatures or add public methods
protected DataOutputStream d;
String s = "Hello World!!";
public BitOutputStream(String filename) {
try {
d = new DataOutputStream(new FileOutputStream(filename));
char c = g.charAt(0);
char[] c_arr = s.toCharArray();
writeBit(c_arr)
} catch (IOException e) {
}
}
public void writeBit(char bit) {
d.writeBytes(bit);
}
public void close() {
if(d!=null)
d.close();
}
}