program # 2 pgm2_5dCGS3767OS This assignment is worth 6 points total, partial cr
ID: 3587673 • Letter: P
Question
program # 2 pgm2_5dCGS3767OS This assignment is worth 6 points total, partial credit will be given, 1 - Worth 6 points In your Ubuntu VM (virtual machine), using terminal mode ONLY, do the following: Create a folder named pgm2 In this folder (pgm2) place the text file located at: http://users.cis.fiu.edu/~mrobi002/databases/RAMerrors Each record in this file represents the location of an error found in RAM 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 address ----------- 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 bits In the same folder (pgm2), in terminal mode, using an editor, create a Java program named: your lastName, First letter of your first name, _OS, pgm2, java example: robinsonM_OSpgm2.java This program will do the following: - Open the text file - Read each record, which is the location of an error in RAM, in hex - Convert that hex value to binary - Convert the binary value to decimal value - Print the RAM chip number where the error is located for each record note: Location addresses for RAM chips are decimal *** DO NOT CHANGE THE FILE NAME *** MAKE SURE THE FILE IS IN THE SAME FOLDER AS YOUR JAVA PROGRAM *** CREATE YOUR OWN METHODS THAT WILL CONVERT HEX TO BINARY AND BINARY TO DECIMAL *** DO NOT USE JAVA'S AUTOMATIC CONVERSION METHODS *** DO NOT USE JAVA'S PARSE COMMANDS *** USE System.out.printf commands ONLY to print any data
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class robinsonM_OSpgm2
{
public static String hexToBinary(String hex) throws IOException {
String result = "";
String binVal;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
switch (hexChar) {
case ('0'):
binVal = "0000";
break;
case ('1'):
binVal = "0001";
break;
case ('2'):
binVal = "0010";
break;
case ('3'):
binVal = "0011";
break;
case ('4'):
binVal = "0100";
break;
case ('5'):
binVal = "0101";
break;
case ('6'):
binVal = "0110";
break;
case ('7'):
binVal = "0111";
break;
case ('8'):
binVal = "1000";
break;
case ('9'):
binVal = "1001";
break;
case ('A'):
binVal = "1010";
break;
case ('B'):
binVal = "1011";
break;
case ('C'):
binVal = "1100";
break;
case ('D'):
binVal = "1101";
break;
case ('E'):
binVal = "1110";
break;
case ('F'):
binVal = "1111";
break;
default:
binVal = "invalid input";
break;
}
result += binVal;
}
return result;
}
public static Double binaryToDecimal(String result) {
double j=0;
for(int i=0;i<result.length();i++)
{
if(result.charAt(i)== '1')
{
j= j + Math.pow(2, result.length()-1-i);
}
}
return j;
}
public static void main(String args[]) throws IOException {
Scanner scanner = new Scanner(new File("D:/RAMerrors.txt"));
String result;
Double decimal;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
Scanner input = new Scanner(line);
String hex = input.next();
result = hexToBinary(hex);
System.out.println("Binary of " + hex + ":" + result);
decimal = binaryToDecimal(result);
System.out.println("Decimal value: " + decimal);
}
}
}
OUTPUT:
Binary of ABCDEFABC:101010111100110111101111101010111100
Decimal value: 4.6118402748E10
Binary of 1A00D0000:000110100000000011010000000000000000
Decimal value: 6.980173824E9
Binary of 7A0EDF301:011110100000111011011111001100000001
Decimal value: 3.2764719873E10
Binary of 3CDAEFFAD:001111001101101011101111111110101101
Decimal value: 1.6335699885E10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.