Question
java

Binary files The binary file data.dat contains characters and numbers- it has 16 characters, followed by numbers of type int
To achieve the goals above, loop and read in 16 characters, then loop and read in an int followed by a double. For the numeri
) catch (IOException e) /l print error message about opening file or just e.getMessage0 End of d
Binary files The binary file data.dat contains characters and numbers- it has 16 characters, followed by numbers of type int alternating with numbers of type double -so after the first 16 characters, there will be an int, followed by a double, followed by an int, followed by a double, and so on. Write a program to open the file and until the end of file is reached, read in the values in the appropriate data types (that is, read the characters as characters, integers as integers and the doubles as doubles), process them, and output the following separately for each type of value: 1. The string that is all of the characters 2. The total number of integers in the file 3. The total number of doubles in the file 4. The sum of all of the integers 5. The sum of all of the doubles 6. The average, min, and max of the integers 7. The average, min, and max of the doubles To do this, envision what the contents of the file look like. 16 characters, followed by alternating ints and doubles, so for example: ch ch ch ch ch ch ch ch ch ch ch ch ch ch ch ch int double int double int double, and so on. Recall that type char represents individual characters. In the file, the characters will be separated by a space. Remember that in a binary file, data is stored using the number of bits that are used to represent a type. So if when you read your data, if your reading gets out of sync by reading the wrong type (or the wrong number of bits), your data will be compromised. That means it will not read what is really in the file.
To achieve the goals above, loop and read in 16 characters, then loop and read in an int followed by a double. For the numeric types, process as you go do not use an array or a collection to store your data. References Java documentation for ObjectinputStream class Using a try catch block with an EOFException is probably the easiest way to terminate the file read because you do not know how much to read. Here is some pseudocode for the main process. // initialize stuff try f Open the file and create the object stream, named myObjStream in this pseudocode /read the 16 characters and store in a string try f //read an int using myObjStream.readIntO; /I add int to sum / add 1 to counter // if it is the largest int, update the largest int //if it is the smallest int, update smallest //read a double using myObjStream.readDouble0; // perform the same actions for the double ; catch (EOFException e) //Close the file //Calculate averages //Display all the results
) catch (IOException e) /l print error message about opening file or just e.getMessage0 End of d
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java program to read a binary file containing 16 characters and alternating sequence of int and double till the end of file

import java.io.EOFException;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.ObjectInputStream;

public class ReadBinaryFile {

       public static void main(String[] args) {

            

             String inStr="";

             int countInt = 0, countDouble = 0;

             int sumInt = 0, minInt = 0, maxInt = 0, numInt;

             double sumDouble = 0 , minDouble = 0, maxDouble = 0, numDouble;

             ObjectInputStream myObjStream = null;

             FileInputStream fin = null;

             try {

                    fin = new FileInputStream("data.dat");

                    myObjStream = new ObjectInputStream(fin);

                    // loop to read the first 16 characters

                    for(int i=0;i<16;i++)

                           inStr += myObjStream.readChar();

                    // read an int and an double till the end of file

                    numInt = myObjStream.readInt();

                    countInt++;

                    sumInt += numInt;

                    if(countInt == 1)

                    {

                           minInt = numInt;

                           maxInt = numInt;

                    }else

                    {

                           if(maxInt < numInt)

                                 maxInt = numInt;

                           if(minInt > numInt)

                                 minInt = numInt;

                    }

                   

                    numDouble = myObjStream.readDouble();

                    countDouble++;

                    sumDouble += numDouble;

                    if(countDouble == 1)

                    {

                           minDouble = numDouble;

                           maxDouble = numDouble;

                    }else

                    {

                           if(maxDouble < numDouble)

                                 maxDouble = numDouble;

                           if(minDouble > numDouble)

                                 minDouble = numDouble;

                    }

                   

                   

             } catch (FileNotFoundException e) {

                   

                    System.out.println("File data.dat not found");

             }catch(EOFException e)

             {     

                   

                    try {

                           //     close the file

                           fin.close();

                           myObjStream.close();

                           // Display the results

                           System.out.println("Characters : "+inStr);

                           System.out.println("Total number of integers : "+countInt);

                           System.out.println("Total number of doubles : "+countDouble);

                           System.out.println("Sum of all integers : "+sumInt);

                           System.out.println("Sum of all doubles : "+sumDouble);

                           if(countInt > 0)

                           {

                                 System.out.println("Minimum Integer : "+minInt);

                                 System.out.println("Maximum Integer : "+maxInt);

                                 System.out.println("Average Integer : "+(sumInt/countInt));

                           }

                          

                           if(countDouble > 0)

                           {

                                 System.out.println("Minimum Double : "+minDouble);

                                 System.out.println("Maximum Double : "+maxDouble);

                                 System.out.println("Average Double : "+(sumDouble/countDouble));

                           }

                    } catch (IOException e1) {

                           System.out.println(e1.getMessage());

                    }

                   

                   

             }catch (IOException e) {

                   

                    System.out.println(e.getMessage());

             }

       }

}

//end of program

Add a comment
Know the answer?
Add Answer to:
Binary files The binary file data.dat contains characters and numbers- it has 16 characters, foll...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File;...

    Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...

  • I am told to create a fillArray method that will read integers from the file "data.txt"...

    I am told to create a fillArray method that will read integers from the file "data.txt" and store them in the array. I'm told to assume that the file has no more than 100 items in it. This is what I have so far: public void fillArray() { int curVal;    Scanner input = null; try { input = new Scanner(new File("data.txt")); // set the current number of items in the array to zero while (input.hasNextInt()) { curVal = input.nextInt();...

  • PLEASE HELP C++ some of the content in random.txt Program #2 This problem is a modification...

    PLEASE HELP C++ some of the content in random.txt Program #2 This problem is a modification of Problem 24 on page 302 of the Gaddis text with two small additions. (A scan of this problem can be found on the last page of the assignment.) In this assignment, your program will read in a list of random numbers from an input file, random. txt (found on eLearning in the "Homework Input Files" folder), and calculate the following statistics on those...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT