I need help with this assignment, In my code, I need to remove the second line o
ID: 3908890 • Letter: I
Question
I need help with this assignment, In my code, I need to remove the second line of the output. I mean that, as that address is not in one of the chips, the code just print the line that says so and then goes on to the other lines.
THis is the entire assigment, (just to add more information on what I need.)
The Numbers on the RAMerrors file are:
Please provide pictures of the code running and include descriptions.
This is the code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MemoryCalculator
{
//retursn a 4bit binary form for given hex char
private static String hexToBin(char hex)
{
int deci = 0;
StringBuilder bin = new StringBuilder("0000");
//get decimal value of the hex char
if(hex >= '0' && hex <= '9')
deci = hex - '0';
else if(hex >= 'a' && hex <= 'f')
deci = 10 + hex - 'a';
else if(hex >= 'A' && hex <= 'F')
deci = 10 + hex - 'A';
//repeatedly divide by 2 and set the remainder starting from last index
int index = 3;
while(deci != 0)
{
int rem = deci % 2;
bin.replace(index, index + 1, rem+"");
deci /= 2;
index--;
}
return bin.toString();
}
//return binary form of the given hex value
private static String hexToBin(String hex)
{
String bin = "";
for(int i = 0; i < hex.length(); i++)
bin += hexToBin(hex.charAt(i));
return bin;
}
//convert a binary value to decimal
private static long binToDeci(String bin)
{
long decimal = 0;
long powerOf2 = 1; //starting power of 2 for last bit (LSB)
for(int i = bin.length() - 1; i >= 0; i--)
{
char ch = bin.charAt(i);
if(ch == '1')
decimal += powerOf2;//add the power of 2 only if the char is 1
powerOf2 *= 2; //next power
}
return decimal;
}
public static boolean contains_num(int[] arr, int num)
{
for(int i: arr)
{
if(i == num)
{
return true;
}
}
return false;
}
public static void main(String[] args)
{
Scanner keybd = new Scanner(System.in);
String filename ;
System.out.print("Please enter filename: ");
filename = keybd.nextLine();
Scanner inFile = null;
try
{
inFile = new Scanner(new File(filename));
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
return;
}
long KB = 1024 * 8; //no. of bits in 1KB
long MB = 1024 * KB; //no. of bits in 1MB
long GB = 1024 * MB; //no of bits in 1GB
int[] chip_nums = {0, 1, 2, 3};
while(inFile.hasNext())
{
// getting the hex value from the file
String hex = inFile.next();
// converting it to binary
String bin = hexToBin(hex);
// converting the binary into decimal
long deci = binToDeci(bin);
// finding on which chip the passed hex value of the error lies
int chip = (int) (deci / GB);
System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip, hex, deci );
if(! contains_num(chip_nums, chip))
{
System.out.printf("Hex: %s is the error address as it does not lie on any of the RAM Chips. ", hex);
}
}
inFile.close();
}
}
javier@javier-VirtualBox:-/Desktop/program2$ java MemoryCalculator Please enter filename: RAMerrors Ram Chip 1, Hex: 3CDAEFFAD, Decimal: 16,335,699,885 Ram Chip 5, Hex: ABCDEFABC, Decimal: 46,118,402,748 Hex: ABCDEFABC is the error address as it does not lie on any of the RAM Chips. Ram Chip 3, Hex: 7AOEDF301, Decimal: 32,764,719,873 Ram Chip ?, Hex: 1A00D0000, Decinal: 6,980,173,824 javier@javier-VirtualBox:-/Desktop/program2$Explanation / Answer
In that case we need to modify
----------------------------original code-------------------------------
System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip, hex, deci );
if(! contains_num(chip_nums, chip))
{
System.out.printf("Hex: %s is the error address as it does not lie on any of the RAM Chips. ", hex);
}
----------------------------original code-------------------------------
----------------------------modified code-------------------------------
//System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip, hex, deci );
if(! contains_num(chip_nums, chip))
{
System.out.printf("Hex: %s is the error address as it does not lie on any of the RAM Chips. ", hex);
}
else {
System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip, hex, deci );
}
----------------------------modifie code-------------------------------