Write a program that reads in an input file with one or more

Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexadecimal, and octal numbers.

Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexadecimal, and octal numbers. Your program should take file name as input from the user. The binary numbers must be read in as strings. It is up to you to choose the variable types needed otherwise. Additionally, your program should define at least one function (likely, you\'ll end up with at least 3). If your program does NOT have at least one function, you will not get credit for this part of the assignment, even if your program passes submit.cs grading. You have to utilize the following devices in your program (required): string.length() returns the length of a string, i so if string = \"code\", string.length() = 4. string[n] returns the nth character in the string (indexing starts at 0), so string[0] = \"c\", string[1] = \"o\", etc... int(char c) converts a character into its ASCII code, so if c = \'a\', then int(c) = 97, and if c = \'c\', the int(c) = 99, etc... to_string(int i) converts an integer into a string. Function found in library. Example, if i = 73, then to_string(i) = \"73\" For example, given a file called bin, which contains the following: 11011 1111 10111111 10 A session should then look like the following example, including whitespace (no whitespaces at the end of the lines) and formatting: Enter filename: bin.txt The binary number 11011 equals 27 in decimal, 1B in hexadecimal, and 33 in octal The binary number 1111 equals 15 in decimal, F in hexadecimal, and 17 in octal The binary number 10111111 equals 191 in decimal, BF in hexadecimal, and 277 in octal The binary number 10 equals 2 in decimal, 2 in hexadecimal, and 2 in octal

Solution

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class NumberConversions {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in).useDelimiter(\"\\\ \");

       //String filename = \"/Users/Akhilaperumalla/Desktop/readme.txt\";

       System.out.println(\"Please enter filename with full path\");

       String filename = in.next();

       try {

           BufferedReader reader = new BufferedReader(new FileReader(filename));

           String line;

           while ((line = reader.readLine()) != null) {

               //System.out.println(line);

               //System.out.println(getDecimalFromBinary(Integer.parseInt(line)));

               //System.out.println(getOctalFromDecimal(getDecimalFromBinary(Integer.parseInt(line))));

              

               //System.out.println(getHexaFromDecimal(getDecimalFromBinary(Integer.parseInt(line))));

               System.out.println(\"The binary number \"+line+\" equals \"+getDecimalFromBinary(Integer.parseInt(line))+\" in decimal, \"+getHexaFromDecimal(getDecimalFromBinary(Integer.parseInt(line)))+\" in hexadecimal, and \"+getOctalFromDecimal(getDecimalFromBinary(Integer.parseInt(line)))+\" in octal\");

           }

           reader.close();

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

   }

   public static int getDecimalFromBinary(int binary) {

       int decimal = 0;

       int power = 0;

       while (true) {

           if (binary == 0) {

               break;

           } else {

               int tmp = binary % 10;

               decimal += tmp * Math.pow(2, power);

               binary = binary / 10;

               power++;

           }

       }

       return decimal;

   }

   public static String getOctalFromDecimal(int quot) {

       int i = 0, j;

       String temp =\"\";

       int octnum[] = new int[100];

       while (quot != 0) {

           octnum[i++] = quot % 8;

           quot = quot / 8;

       }

       for (j = i - 1; j >= 0; j--) {

           //System.out.print(octnum[j]);

           temp+=octnum[j];

       }

       return temp;

      

   }

   public static String getHexaFromDecimal(int num) {

   int rem;

  

// For storing result

String str2=\"\";

// Digits in hexadecimal number system

char hex[]={\'0\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'A\',\'B\',\'C\',\'D\',\'E\',\'F\'};

while(num>0)

{

rem=num%16;

str2=hex[rem]+str2;

num=num/16;

}

return str2;

   }

}

Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexad
Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexad
Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexad

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site