// 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
Binary files The binary file data.dat contains characters and numbers- it has 16 characters, foll...
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; 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" 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 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 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 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 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 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 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 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...