Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java Write a program which will: 1. Read the name of a file containing 16-bit integers from the keyboard. 2. Open the file as

In java Write a program which will:

1. Read the name of a file containing 16-bit integers from the keyboard.

2. Open the file as a binary file.

3. Read a 16-bit number from the file

4. Display the number on one line as:

a. a 16-bit binary number (16 bits must be printed -- i.e., include all the leading zeros)

b. A 4-digit hexadecimal number (all 4digits must be printed -- include leading zeros)

c. The 5-bit unsigned base-10 integer extracted from bits 15-11 of the number

d. The 1-bit value extracted from bit 10 of the number

e. The 2-bit unsigned base-10 integer extracted from bits 9-8 of the number

f. The 8-bit unsigned base-10 integer extracted from bits 7-0 of the number.

5. Repeat from step 3 until the end-of-file has been reached

6. Close the input file

This is what i have so far, but my while loop wont work.

import java.util.Scanner; import java.io.*; import java.lang.*;

public class ReadBin { public static void main(String[] args) throws IOException { int number; String numBin = ""; int num16 = 0; int num4 = 0; int num5 = 0; int num1 = 0; int num2 = 0; int num8 = 0; boolean endOfFile = false; int power; Scanner keyboard = new Scanner(System.in); System.out.print("Type Filename Here: "); String file = keyboard.next(); // String file = "numbers.bin"; FileInputStream fstream = new FileInputStream(file); DataInputStream inputFile = new DataInputStream(fstream); // System.out.println(); System.out.println(" Reading numbers from the file:"); System.out.println(" \t 16-bit Binary\t\t 4-digit hex \t 5-bit unsigned 1-bit 2-bit unsigned 8-bit unsigned"); while (!endOfFile) { try { number = inputFile.readInt(); numBin = Integer.toBinaryString(number); while(numBin.length() != 16) { numBin = "0" + numBin; power = 8; // for (int i = 0; i <= 7; i++) { // if (numBin.charAt(i) == '1') { num8 = num8 + (int)Math.pow(2, power); } power--; } // power = 5; // add the leading zeroes so that they're the same thing. // for (int i=11; i<=15; i++) { // if(numBin.charAt(i)=='1') { // num4 = num4 + (int)Math.pow(2, power); // } // power--; // } power = 4; // add the leading zeroes so that they're the same thing. for (int i=11; i<=15; i++) { if(numBin.charAt(i)=='1') { num5 = num5 + (int)Math.pow(2, power); } power--; } power = 1; // for (int i = 10; i <= 10; i++) { // if (numBin.charAt(i) == '1') { num1 = num1 + (int)Math.pow(2, power); } power--; } power = 2; // for (int i = 8; i <= 9; i++) { // if (numBin.charAt(i) == '1') { num2 = num2 + (int)Math.pow(2, power); } power--; }

System.out.printf(numBin+"\t"+"%04x \t\t"+num5+"\t\t"+num1+"\t\t"+num2+"\t\t"+num8+" ",number); num16 = 0; // num4 = 0; num5 = 0; num1 = 0; num2 = 0; num8 = 0; } } catch (Exception ex) { System.out.println(""); endOfFile = true; // ex.printStackTrace(); } } inputFile.close(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago