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

Part 1 - Java program named MemoryCalculator In your Ubuntu VM (virtual machine)

ID: 3786017 • Letter: P

Question

 Part 1 - Java program named MemoryCalculator       In your Ubuntu VM (virtual machine), using terminal mode ONLY, do the following:            Create the folder program2            In this folder place the text file located on my faculty website in Module 2 called RAMerrors (Do not rename this file, it has no extension.)            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---------------         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                              HINT: RAM chip 0 contain addresses:             0 -  1,073,741,823 bytes            RAM chip 1 contain addresses: 1,073,741,824 -  2,147,483,648 bytes             RAM chip 2 contain addresses: 2,147,483,647 -  3,221,225,471 bytes             RAM chip 3 contain addresses: 3,221,225,472 -  4,294,967,296 bytes                                         In the same folder, in terminal mode using an editor, create a Java program       to do the following:      - Call the Java program - MemoryCalculator      - Open the RAMerrors text file        - Read each record      - Print the RAM memory chip where the error is located for each record        *** CREATE YOUR OWN METHODS THAT WILL CONVERT             HEX TO BINARY AND BINARY TO DECIMAL       *** DO NOT USE JAVA'S AUTOMATIC CONVERSION METHODS       Part 2 - Linux Shell Scripting - Worth 5 points.     - NOT using editors, create a sh file named: program2.sh     - Set this *.sh file to: chmod 755 *.sh     - NOT using editors, append your name, current date and time to a NEW file called results.txt     - NOT using editors, append to the *.sh file, all the commands needed so that when you execute your sh file,      this file will produce the following:          How:     - Ask the user to enter a number.    - Verify that the number is between 1 and 100.    - If the number is not between 1 and 100, then keep asking the user to enter a number until it is valid.    - Use a for loop from 1 to the value entered by the user     - Sum the results of calculating each number mod 5    - After the for loop ends, display the sum of all previous results and      append it to the results.txt file as follows       Sum of numbers is xxx   Zip your files into one file named FirstnameLastnameA2.zip submit them in the Assignment 2 dropbox.     Only include the folowing files:    - MemoryCalculator.java    - program2.sh 

Explanation / Answer

File MemoryCalculator.java

import java.io.BufferedReader;
import java.io.FileReader;

public class MemoryCalculator {

   public static void main(String[] args) {
       BufferedReader bf = null;
       try {
           // define reader for reading from file
           bf = new BufferedReader(new FileReader("RAMerrors"));
           String address = null;
           // read each record in file
           while ((address = bf.readLine()) != null) {
               // convert the address into binary array
               int[] binaryArray = convertToBinay(address);
               // convert binary value into decimal value
               long decimalLocation = convertToDecimal(binaryArray);
               // perform comparison to find out chip
               if (decimalLocation >= 0 && decimalLocation <= 1073741823l) {
                   System.out.println("RAM chip 0 - Address: " + address + "("
                           + decimalLocation + ")");
               } else if (decimalLocation >= 1073741824l
                       && decimalLocation <= 2147483648l) {
                   System.out.println("RAM chip 1 - Address: " + address + "("
                           + decimalLocation + ")");
               } else if (decimalLocation >= 2147483647l
                       && decimalLocation <= 3221225471l) {
                   System.out.println("RAM chip 2 - Address: " + address + "("
                           + decimalLocation + ")");
               } else if (decimalLocation >= 3221225472l
                       && decimalLocation <= 4294967296l) {
                   System.out.println("RAM chip 3 - Address: " + address + "("
                           + decimalLocation + ")");
               } else {
                   // invalid address location
                   System.out.println("Invalid address Location: " + address
                           + "(" + decimalLocation + ")");
               }
           }
           bf.close();
       } catch (Exception e) {
           e.printStackTrace();
       }

   }

   // Method to convert binary values into decimal
   private static long convertToDecimal(int[] binaryArray) {
       long returnValue = 0;
       for (int i = binaryArray.length - 1; i >= 0; i--) {
           returnValue += binaryArray[i]
                   * Math.pow(2, binaryArray.length - i - 1);
       }
       return returnValue;
   }

   // Method to convert hex value into binary array
   private static int[] convertToBinay(String address) {
       int[] returnArray = new int[address.length() * 4];
       int i = address.length() * 4;
       // process each character in input
       for (int k = address.length() - 1; k >= 0; k--) {
           char ch = address.charAt(k);
           int val = 0;
           // Convert characters into integer of range 0 - 15
           if (ch >= 'A' && ch <= 'F')
               val = 10 + ch - 'A';
           else if (ch >= 'a' && ch <= 'f')
               val = 10 + ch - 'a';
           else if (ch >= '0' && ch <= '9')
               val = ch - '0';
           else
               // invalid character
               break;
           // add each bit value to array
           for (int j = 0; j < 4; j++) {
               returnArray[--i] = val % 2;
               val = val / 2;
           }
       }
       return returnArray;
   }

}