please help completing this short assignment that needs to b
please help completing this short assignment that needs to be done in linux,
in java code create a program The program must do the conversion without using any of the built-in functions from the Java library. Thank you in advance.
*** DO NOT USE JAVA\'S AUTOMATIC CONVERSION METHODS*****
to do the following:
In your Ubuntu, linux, using terminal mode ONLY, do the following:
- Create a folder named pgm2
- In this folder place the text file located at: http://users.cis.fiu.edu/~mrobi002/databases/RAMerrors4
***some of the lines in the file have spaces at the biggining, read the lines overcoming that trick instructed by the professor*****
Do not modify the file putting it adjacent to the left.
******* MAKE SURE NOT TO CHANGE THE FILE NAME *******
Each record in this file represents the location of an error found in RAM
Assume your computer has 4 gigs of RAM, each gig in a different memory chip, therefore you have 4 one gig RAM chips. A gigabyte contains 1,073,741,824 or 1024^3 or 2^30 bytes, which is also equivalent to 1,048,576 kilobytes or 1,024 megabytes.
These are the addresses in each RAM chip in a computer with 4 (gigs) of RAM:
------ decimal adresses ----------
RAM chip 0 contain addresses: 0 - 1,073,741,823 bytes
RAM chip 1 contain addresses: 1,073,741,824 - 2,147,483,647 bytes
RAM chip 2 contain addresses: 2,147,483,648 - 3,221,225,471 bytes
RAM chip 3 contain addresses: 3,221,225,472 - 4,294,967,295 bytes
- In the same folder (pgm2), use an editor in terminal mode, create a Java program to do the following:
- Open the text file
- Read each record in the file
- Convert the record to binary
- Convert the binary result to decimal
- Find in which RAM chip is the decimal error found
- Print the RAM memory chip where the error is located for each record as follows: example:
hex number = binary number = decimal number located at x RAM chip
*** CREATE YOUR OWN METHODS THAT WILL CONVERT HEX TO BINARY AND BINARY TO DECIMAL
*** DO NOT USE JAVA\'S AUTOMATIC CONVERSION METHODS, IT HAS TO BE DONE DOING MATH ONLY *****
Solution
You can create a directory in UNIX using mkdir command.
$mkdir pgm2
Now, for JAVA program (WITHOUT ANY AUTOMATIC CONVERSION FUNCTION).
1. checkRAMChip(): This method checks if the converted decimal number will be in chip 0 or chip 1 or chip 2 or chip 3.
2. binToDecimal(): This method takes a binary number (in string form) as an input and returns its decimal equivalent.
3. getHexEquivalent(): This method returns the hexadecimal value (in the form of string) of the given single binary digit (in the form of a single char).
4. hexToBinary(): This method converts the hexadecimal value to binary.
5. removeSpaces(): This method removes the leading and trailing space from the string and returns the new string with only data value.
6. startCalculation(): This method invokes all the other methods to perform the final calculation.
JAVA SOURCE CODE (RAMERROR.java):
import java.util.*;
import java.io.*;
class RAMERROR{
// check if the input falls into the chip 0/1/2/3.
int checkRAMChip(double deci){
int chip=-1;
if(deci>=0 && deci<=1073741823.0)
chip=0; // chip 0
else
if(deci>=1073741824.0 && deci<=2147483647.0)
chip=1; // chip 1
else
if(deci>=2147483648.0 && deci<=3221225471.0)
chip=2; // chip 2
else
if(deci>=3221225472.0 && deci<=4294967295.0)
chip=3; // chip 3
return chip;
}
// convert binary to decimal value
double binToDecimal(String bin){
double d=0;
for(int i=0; i<bin.length();i++){
if(bin.charAt(i)==\'1\'){
// calculate if digit is 1
d=d+Math.pow(2,bin.length()-1-i);
}
}
return d;
}
// get the hexadecimal equivalent of input character
String getHexEquivalent(char c){
String h=\"\";
switch(c){
case \'0\':
h=\"0000\"; break;
case \'1\':
h=\"0001\"; break;
case \'2\':
h=\"0010\"; break;
case \'3\':
h=\"0011\"; break;
case \'4\':
h=\"0100\"; break;
case \'5\':
h=\"0101\"; break;
case \'6\':
h=\"0110\"; break;
case \'7\':
h=\"0111\"; break;
case \'8\':
h=\"1000\"; break;
case \'9\':
h=\"1001\"; break;
case \'A\':
case \'a\':
h=\"1010\"; break;
case \'B\':
case \'b\':
h=\"1011\"; break;
case \'C\':
case \'c\':
h=\"1100\"; break;
case \'D\':
case \'d\':
h=\"1101\"; break;
case \'E\':
case \'e\':
h=\"1110\"; break;
case \'F\':
case \'f\':
h=\"1111\"; break;
default:
assert(false);
h=\"\";
}
return h;
}
// hexadecimal to binary
String hexToBinary(String hex){
String bin=\"\";
for(int i=0; i<hex.length(); i++){
bin+=getHexEquivalent(hex.charAt(i));
}
return bin;
}
// start calculating the values and print the desired output
void startCalculation(String hex){
String bin;
int chip;
bin=hexToBinary(hex); // convert hex to bin
double deci = binToDecimal(bin); // convert bin to decimal
chip=checkRAMChip(deci); // check which RAM it belongs to
if(chip!=-1){
System.out.printf(\"%s = %s = %.0f located at %d RAM chip\ \",hex,bin,deci,chip);
System.out.println(\"-----------------------\");
}
else{
System.out.printf(\"%s = %s = %.0f not located at any RAM chip (out of memory)\ \",hex,bin,deci);
System.out.println(\"-----------------------\");
}
}
// remove the spaces (leading + trailing) in the input string.
String removeSpaces(String str){
String s=\"\";
char c=\' \';
for(int i=0; i<str.length();i++){
c=str.charAt(i);
if(c!=\' \'){ // create new string if char is not space
s+=c;
}
}
return s;
}
public static void main(String args[]){
// create an object of Triangle
RAMERROR rerror = new RAMERROR();
try{
// File reading using FileReader
File file = new File(\"RAMerrors4\");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
//StringBuffer stringBuffer = new StringBuffer();
String line;
String value=\"\";
// process each line
while ((line = bufferedReader.readLine()) != null) {
value=rerror.removeSpaces(line);
rerror.startCalculation(value);
}
fileReader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT:
080000000 = 000010000000000000000000000000000000 = 2147483648 located at 2 RAM chip
-----------------------
0F2D05DFF = 000011110010110100000101110111111111 = 4073741823 located at 3 RAM chip
-----------------------
4DE743E0 = 01001101111001110100001111100000 = 1307001824 located at 1 RAM chip
-----------------------
FFfDFF5DF = 111111111111110111111111010111011111 = 68717376991 not located at any RAM chip (out of memory)
-----------------------
0046535FF = 000000000100011001010011010111111111 = 73741823 located at 0 RAM chip



