Question Details

(Answered)-1) The first step is to develop a general-purpose class that will


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()

public int 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 bitMask() 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.

(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.
import java.io.*;

 

public class Driver

 

{ static BitMap bitMap;

 

static BufferedReader in = new BufferedReader

 

(new InputStreamReader(System.in));

 

// Method to start the driver program.

 

public static void main(String args)

 

{ int

 

bit, map, option;

 

BitMap bitMap = new BitMap();

 

BitMap secondMap;

 

// Use a menu to test all of the other options.

 

option = menu();

 

while (option != 10)

 

{ switch (option)

 

{ case 1: // Set bit.

 

bit = getBit();

 

bitMap.setBit(bit);

 

System.out.println(bitMap);

 

break;

 

case 2: // Clear bit.

 

bit = getBit();

 

bitMap.clearBit(bit);

 

System.out.println(bitMap);

 

break;

 

case 3: // Check bit.

 

bit

 

= getBit();

 

if (bitMap.checkBit(bit))

 

System.out.println("Bit is set");

 

else System.out.println("Bit is not set");

 

System.out.println(bitMap);

 

break;

 

case 4: // Clear all bits.

 

bitMap.clearAll();

 

System.out.println(bitMap);

 

break;

 

case 5: // Set all bits.

 

bitMap.setAll();

 

System.out.println(bitMap);

 

break;

 

case 6: // Count number of true bits.

 

System.out.println(bitMap);

 

System.out.println(bitMap.countTrue()

 

+ " bits are set");

 

break;

 

case 7: // Compare to bit maps.

 

secondMap = getMap();

 

System.out.println(bitMap);

 

System.out.println(secondMap);

 

System.out.println("compareTo = "

 

+ bitMap.compareTo(secondMap));

 

break;

 

case 8: // See if maps equal.

 

secondMap = getMap();

 

System.out.println(bitMap);

 

System.out.println(secondMap);

 

if (bitMap.equals(secondMap))

 

{ System.out.println("Maps equal" ); }

 

else

 

{ System.out.println("Maps not equal" ); }

 

break;

 

case 9: // toString.

 


 

System.out.println(bitMap);

 

break;

 

default:

 

System.out.println("Illegal Option - try again");

 

break;

 


 

}

 

option = menu();

 


 

}

 


 

}

 

System.out.println("End of Driver for BitMap");

 

// End main.

 


 

// Method to display menu and get selection.

 

public static int menu()

 

{ System.out.println("Menu of driver options");

 

System.out.println(" 1 = setBit");

 

System.out.println(" 2 = cleartBit");

 

System.out.println(" 3 = checkBit");

 

System.out.println(" 4 = clearAll");

 

System.out.println(" 5 = setAll");

 

System.out.println(" 6 = countTrue");

 

System.out.println(" 7 = compareTo");

 

System.out.println(" 8 = equals");

 

System.out.println(" 9 = toString");

 

System.out.println("10 = exit");

 

System.out.print("Enter option: ");

 

return readInt(1,10);

 

} // 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 static BitMap getMap()

 

{ boolean success = false;

 

BitMap bitMap = null;

 

do

 

{ try

 

{ System.out.println(

 

"Enter string of 't','T','f', or 'F' ");

 

String values = in.readLine().toLowerCase();

 

System.out.println(

 

"Enter 'b' or 'B' for Boolean map");

 

String type = in.readLine().toLowerCase();

 

if (type.length()!=0 && type.charAt(0)=='b')

 

{ boolean bools = new boolean[values.length()];

 

for (int i=0; i<values.length(); i++)

 

{ if(Character.toLowerCase(values.charAt(i))=='t')

 

bools[i] = true;

 

else bools[i] = false;

 

bitMap = new BitMap(bools);

 

} }

 

else bitMap = new BitMap(values);

 

success = true;

 


 

}

 


 

}

 

catch (Exception e) {System.out.println(e); }

 

while (!success);

 


 

}

 


 

return bitMap;

 

// 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;

 

try

 

{ token = in.readLine();

 

value = Integer.parseInt (token);

 

if (value<min || value>max) ok = false;

 

}

 

catch (Exception exception) {ok = false;}

 

if (!ok)

 

{ System.out.print("Illegal input, enter between "

 

+ min + " and " + max + ": ");

 

} }

 

return value;

 

} // End readInt().

 

// End class.

 


 

 


Solution details:
STATUS
Answered
QUALITY
Approved
ANSWER RATING

This question was answered on: Oct 07, 2020

PRICE: $15

Solution~0002152450.zip (25.37 KB)

Buy this answer for only: $15

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free copy (Deadline assured. Flexible pricing. TurnItIn Report provided)

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .
SiteLock

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Oct 07, 2020

EXPERT

Tutor

ANSWER RATING

GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

NEW ASSIGNMENT HELP?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN YOUR SET DEADLINE.

Order Now