Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Update MyByte class only to run MyByte 2 Tester.java ( method stubs marked / / TODO ) : package MyByte 2 Starter; public class MyByte

Update MyByte class only to run MyByte2Tester.java (method stubs marked //TODO):
package MyByte2Starter;
public class MyByte {
public static final int MAX_VAL =0; //unsigned (positive only)
public static final int MIN_VAL =0; //unsigned (positive only)
public static final int MAX_SIGNED_VAL =0;
public static final int MIN_SIGNED_VAL =0;
public static final MyByte MAX_BIN = new MyByte("11111111");
public static final MyByte MIN_BIN = new MyByte("00000000");
public static final int NUM_BITS =8;
private boolean isSigned = false;
private boolean[] val;
public MyByte(String byteString){
this.val = new boolean[NUM_BITS];
if (byteString.length()!= NUM_BITS){
throw new IllegalArgumentException("Invalid binary string length");
}
for (int i =0; i NUM_BITS; i++){
this.val[i]= byteString.charAt(i)=='1';
}
}
public MyByte(int value){
this.val = new boolean[NUM_BITS];
if (value > MAX_VAL){
throw new IllegalArgumentException("Value is too large to store in a byte");
} else if (value MIN_VAL){
if (value MIN_SIGNED_VAL){
throw new IllegalArgumentException("Value is too negative to store in a byte");
}
this.isSigned = true;
// For negative values, use two's complement representation
value = MAX_VAL + value +1;
}
// Convert the integer to binary representation
for (int i = NUM_BITS -1; i >=0; i--){
this.val[i]=(value & 1)==1;
value >>=1;
}
}
public void setBit(int index, boolean val){
this.val[index]= val;
}
public boolean getBit(int index){
return this.val[index];
}
public String toString(){
String binaryText ="";
for(int i =0; i8; i++){
if( val[i]) binaryText +="1";
else binaryText +="0";
if( i ==3) binaryText +=""; //add a space to write 4 bits grouped
}
return binaryText;
}
public MyByte add(String value){
MyByte operand = new MyByte(value);
return add(operand);
}
public MyByte add(int value){
MyByte operand = new MyByte(value);
return add(operand);
}
public MyByte add(MyByte value){
// TODO Auto-generated method stub
return null;
}
public int getSignedVal(){
// TODO Auto-generated method stub
return 0;
}
public int getUnsignedVal(){
// TODO Auto-generated method stub
return 0;
}
public MyByte and(MyByte op2){
MyByte result = new MyByte(0);
for (int i =0; i NUM_BITS; i++){
result.setBit(i, getBit(i) && op2.getBit(i));
}
return result;
}
public MyByte or(MyByte op2){
MyByte result = new MyByte(0);
for (int i =0; i NUM_BITS; i++){
result.setBit(i, getBit(i)|| op2.getBit(i));
}
return result;
}
public MyByte xor(MyByte op2){
MyByte result = new MyByte(0);
for (int i =0; i NUM_BITS; i++){
result.setBit(i, getBit(i)^ op2.getBit(i));
}
return result;
}
}
package MyByte2Starter;
public class MyByte2Tester {
public static void main(String[] args){
int score =0;
score += testConstants();
score += testConstructors();
score += testSigned();
score += testAdd_Int();
score += testAdd_MyByte();
score += testAdd_String();
score += testAND();
score += testOR();
score += testXOR();
System.out.println("current score: "+ score +"="+(int)(score*100/61.0)+"%");
System.out.println("End of program.");
}
private static int testXOR(){
int points =0;
MyByte op1= new MyByte("10111010");
MyByte op2= new MyByte("11110010");
MyByte xorByte = op1.xor(op2);
if( xorByte.equals( new MyByte("01001000"))){
points +=2;
}else {
System.out.println("Failed OR test: 10111010 XOR 11110010->01001000
"+
"\tYou had "+ op1.toString()+" XOR "+ op2.toString()+"->"+ xorByte.toString());
}
return points;
}
private static int testOR(){
int points =0;
MyByte op1= new MyByte("10111010");
MyByte op2= new MyByte("11110010");
MyByte orByte = op1.or(op2);
if( orByte.equals( new MyByte("11111010"))){
points +=2;
}else {
System.out.println("Failed OR test: 10111010 OR 11110010->11111010
"+
"\tYou had "+ op1.toString()+" OR "+ op2.toString()+"->"+ orByte.toString());
}
return points;
}
Chegg does not allow me to post the rest of the MyByte2Tester class
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions