Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program to demonstrate using bitmaps and bitwise operators to sort

ID: 672648 • Letter: W

Question

Write a Java program to demonstrate using bitmaps and bitwise operators to sort and remove duplicates from a file of random phone numbers. Do not confuse the term bitmap used for compressing data into smaller spaces with the bitmap that has come to mean a graphic image.

You will use this file of 4 million random phone numbers with possible repeats in args[0]. In addition, will be given an output filename provided in args[1] into which you will write the sorted list of phone numbers without duplicates.

Each phone number begins with the digit 2 or greater so phone numbers are 2000000 through 9999999. There are no area codes or country code prefixes to deal with. The file looks something like:

Here are some declarations to get you started:

Scanner inFile = new Scanner(new FileReader(args[0]));

PrintWriter outFile = new PrintWriter(args[1]);

byte[] bitmap = new byte[1000000];

You will use a bitmap as an array of 1,000,000 bytes (that is, the type byte). Each bit in the array of one million bytes will represent one phone number. The bit will be turned on if and only if the number was read from the file. Those bits that remain off will indicate the number was never found in the file.

You will want to review Bitwise Operators.

Using this bitmap association will guarantee no repeats in the resultant file and as a side effect automatically sorts the data since you will process the bitmap sequentially from beginning to end inspecting every bit and writing the phone number represented by that bit to the output file if the bit is turned on.

Process all values before writing to the output file.

Your output file should have all the found numbers with no duplicates in ascending, sorted order.

As a guide d2.dat is provided above and is 32,000,000 bytes. Your result file should be 25,195,720 bytes on Unix or 28,345,185 in Windows/DOS. Values other than this indicate an error in processing and you may want to consider a simpler file for diagnosis.

Scanner inFile = new Scanner(new FileReader(args[0]));

PrintWriter outFile = new PrintWriter(args[1]);

byte[] bitmap = new byte[1000000];

Explanation / Answer

import java.io.File;
import java.io.*;
import java.util.*;
import java.util.Arrays;
public class d3
{
public static void main (String[] args) throws Exception
{
FileReader fr;
try
{
fr = new FileReader(args[0]);
}
catch (FileNotFoundException e)
{
System.out.println("File Not Found.");
return;
}
Scanner sc = new Scanner(fr);
while (sc.hasNextLine())
{
int[] phone_num = new int[8000000];
String[] phone_num_str;
phone_num_str = new String[8000000];
phone_num_str = sc.nextLine();
phone_num_str = phone_num_file;
phone_num = Integer.parseInt(phone_num_str);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote