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

getMessage()); return; } long KB = 1024 * 8; //no. of bits in 1KB long MB = 1024

ID: 3906262 • Letter: G

Question

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

while(inFile.hasNext())
{
String hex = inFile.next();
String bin = hexToBin(hex);
long deci = binToDeci(bin);
long chip = deci / GB;
System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip,
hex, deci );
//System.out.print(", Binary " + bin + " ");
}
inFile.close();
}
}

Assume you have a computer with 4 gigs of RAM, each gig in a different memory chip, therefore you have 4 one gig RAM chips -decimal- HINT: RAM chip 0 contain addresses: 0-8,589,934,584 bits RAM chip 1 contain addresses: 8,589,934,585 - 17,179, 869,184 bits RAM chip 2 contain addresses: 17,179,869,185 - 25,769, 803, 768 bits RAM chip 3 contain addresses: 25, 769, 803,769 - 34,359, 738,368 bit:s 01,073,741,823 bytes RAM chip 1 contain addresses: 1,073,741,824-2,147,483,648 bytes 3,221,225,471 bytes RAM chip 3 contain addresses: 3,221,225,472 -4,294,967,296 bytes HINT: RAM chip 0 contain addresses: RAM chip 2 contain addresses: 2,147,483, 647 In the same folder, in terminal mode using an editor, create a rogram to do the following:

Explanation / Answer

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

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

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

}

//System.out.println(hex + "= " + bin.toString());

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 void main(String[] args) {

/*you can directly mention file path instead of inputting file name as content in file will be changed but not file name, here \ is used reason is is an escape character hence \ will add an one .

* if you need to input file name and process. Take user input for file name and then use InputStream function in try block instead of BufferReader to get classpath and add file name to it

*/

final String FILENAME = "C:\Users\VISHNU\Desktop\Java_Wokspace\Hello\src\RAMerrors";

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

try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {

String sCurrentLine;

while ((sCurrentLine = br.readLine()) != null) {

String hex = sCurrentLine.toUpperCase(); //will optimize code as memory address will be "0-F" or "0-f" but not in combination

String bin = hexToBin(hex);

long deci = binToDeci(bin);

long chip = deci / GB;

System.out.println("Ram Chip : "+chip+", Hex : "+hex+", Decimal : "+deci );

}

} catch (IOException e) {

e.printStackTrace();

}

}

}