Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How do I get Main.java to compile, the program is incomplete ? Need a complete program in Java. Thanks! Class 3 Chapter 17 (Store data

How do I get Main.java to compile, the program is incomplete ? Need a complete program in Java. Thanks!

Class 3 Chapter 17 (Store data in a file)

Due Wednesday by 8pm Points 10 Submitting a file upload Available Feb 8 at 7pm - Feb 15 at 8pm

Program 3.1 Requirements

Write/Read Test Program using RandomAccessFile and Objects

*Note you should be using methods for the code, everything should not just be dumped in the main method.

Create a new binary data file using RandomAccessFile populate it with integers from 0 to 100 Read the integer binary file and print it to the screen with 10 numbers on each line Update the integer binary file so that all of the even numbers are 2 Read the integer binary file and print it to the screen with 10 numbers on each line

Create a new binary data file using RandomAccessFile populate it with doubles from 0 to 100 Read the double binary file and print it to the screen with 10 numbers on each line Update the double binary file so that all of the odd numbers are 1 Read the double binary file and print it to the screen with 10 numbers on each line

Create a new binary data file using RandomAccessFile populate it with Strings "Hello", "Good Morning", "Good Afternoon", "Good Evening" Note: make all of the Strings the same size. Read the String binary file and print each String on it's own line Update the String binary file so that the last String says "Good Bye" Read the String binary file and print each String on it's own line

Create a new binary data file using ObjectOutputStream Use the Employee object from EmployeeManager Interface Part 1 to populate the file with the data below: 1, 100, "John", "Wayne", Employee.EmployeeType.CONTRACTOR) 2, 1000.0, "Selina", "Kyle", Employee.EmployeeType.CONTRACTOR

Create a new binary data file using RandomAccessFile Use the Employee object from EmployeeManager Interface Part 1 to populate the file with the data below: 1, 100, "John", "Wayne", Employee.EmployeeType.CONTRACTOR) 2, 1000.0, "Selina", "Kyle", Employee.EmployeeType.CONTRACTOR

Example output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,

2, 1, 2, 3, 2, 5, 2, 7, 2, 9, 2, 11, 2, 13, 2, 15, 2, 17, 2, 19, 2, 21, 2, 23, 2, 25, 2, 27, 2, 29, 2, 31, 2, 33, 2, 35, 2, 37, 2, 39, 2, 41, 2, 43, 2, 45, 2, 47, 2, 49, 2, 51, 2, 53, 2, 55, 2, 57, 2, 59, 2, 61, 2, 63, 2, 65, 2, 67, 2, 69, 2, 71, 2, 73, 2, 75, 2, 77, 2, 79, 2, 81, 2, 83, 2, 85, 2, 87, 2, 89, 2, 91, 2, 93, 2, 95, 2, 97, 2, 99, 2,

0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0,

0.0, 1.0, 2.0, 1.0, 4.0, 1.0, 6.0, 1.0, 8.0, 1.0, 10.0, 1.0, 12.0, 1.0, 14.0, 1.0, 16.0, 1.0, 18.0, 1.0, 20.0, 1.0, 22.0, 1.0, 24.0, 1.0, 26.0, 1.0, 28.0, 1.0, 30.0, 1.0, 32.0, 1.0, 34.0, 1.0, 36.0, 1.0, 38.0, 1.0, 40.0, 1.0, 42.0, 1.0, 44.0, 1.0, 46.0, 1.0, 48.0, 1.0, 50.0, 1.0, 52.0, 1.0, 54.0, 1.0, 56.0, 1.0, 58.0, 1.0, 60.0, 1.0, 62.0, 1.0, 64.0, 1.0, 66.0, 1.0, 68.0, 1.0, 70.0, 1.0, 72.0, 1.0, 74.0, 1.0, 76.0, 1.0, 78.0, 1.0, 80.0, 1.0, 82.0, 1.0, 84.0, 1.0, 86.0, 1.0, 88.0, 1.0, 90.0, 1.0, 92.0, 1.0, 94.0, 1.0, 96.0, 1.0, 98.0, 1.0, 100.0,

Hello Good Morning Good Afternoon Good Evening

//Store Data in a file import java.io.*;

public class Main { private static final int NUM_ELEMENTS = 100; private static final int NUM_ELEMENTS_PER_LINE = 10; private static final int STRING_LENGTH = 15;

public static void main(String[] args) throws IOException { // Populate the integer binary file and print it to the screen writeIntsToFile("ints.bin"); readIntsFromFile("ints.bin");

// Update the integer binary file and print it to the screen updateIntsInFile("ints.bin"); readIntsFromFile("ints.bin");

// Populate the double binary file and print it to the screen writeDoublesToFile("doubles.bin"); readDoublesFromFile("doubles.bin");

// Update the double binary file and print it to the screen updateDoublesInFile("doubles.bin"); readDoublesFromFile("doubles.bin");

// Populate the String binary file and print it to the screen writeStringsToFile("strings.bin"); readStringsFromFile("strings.bin");

// Update the String binary file and print it to the screen updateStringsInFile("strings.bin"); readStringsFromFile("strings.bin");

// Populate the Object binary file using ObjectOutputStream and print it to the screen writeEmployeesToFileUsingObjectOutputStream("employees_oos.bin"); readEmployeesFromFileUsingObjectInputStream("employees_oos.bin");

// Populate the Object binary file using RandomAccessFile and print it to the screen writeEmployeesToFileUsingRandomAccessFile("employees_raf.bin"); readEmployeesFromFileUsingRandomAccessFile("employees_raf.bin"); }

private static void writeIntsToFile(String fileName) throws IOException { try (RandomAccessFile file = new RandomAccessFile(fileName, "rw")) { for (int i = 0; i < NUM_ELEMENTS; i++) { file.writeInt(i); } } }

private static void readIntsFromFile(String fileName) throws IOException { try (RandomAccessFile file = new RandomAccessFile(fileName, "r")) { for (int i = 0; i < NUM_ELEMENTS; i++) { if (i % NUM_ELEMENTS_PER_LINE == 0) { System.out.println(); } System.out.print(file.readInt() + ", "); } System.out.println(); } }

private static void updateIntsInFile(String fileName) throws IOException { try (RandomAccessFile file = new RandomAccessFile(fileName, "rw")) { for (int i = 0; i < NUM_ELEMENTS; i++) { file.seek(i * Integer.BYTES); int value = file.readInt(); if (value % 2 == 0) { file.seek(i * Integer.BYTES); file.writeInt(2) // incomplete program above , how do I fix? package examplesweek2;

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List;

/** * Program employee implements Employee Manager File Implementation. * * @author */ public class EmployeeManagerFileImpl implements EmployeeManager {

private List employees = new ArrayList<>(); private final String fileName = "employees.csv";

/** * Method create employee, creates employees. */ @Override public Employee createEmployee(Employee employee) { employee.setId(employees.size() + 1); employees.add(employee); writeToFile(); return employee; }

/** * Method update employee, updates an employee. */ @Override public Employee updateEmployee(Employee employee) { int index = employee.getId() - 1; employees.set(index, employee); writeToFile(); return employee; }

/** * Method read employee, reads an employee. */ @Override public Employee readEmployee(int id) { int index = id - 1; return employees.get(index); }

/** * Method read all employees, reads all employees. */ @Override public List readAllEmployees() { return employees; }

/** * Method delete employee, deletes an employee. */ @Override public void deleteEmployee(int id) { int index = id - 1; employees.remove(index); writeToFile(); }

/** * Method write to file, writes to file. */ private void writeToFile() { try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) { for (Employee employee : employees) { writer.write(employee.getId() + "," + employee.getFirstName() + "," + employee.getLastName() + "," + employee.getPayPerHour() + "," + employee.getEmployeeType()); writer.newLine(); } } catch (IOException e) { e.printStackTrace(); } }

/** * Method reads from a file. */ private void readFromFile() { try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { String[] values = line.split(","); int id = Integer.parseInt(values[0]); String firstName = values[1]; String lastName = values[2]; double payPerHour = Double.parseDouble(values[3]); EmployeeType employeeType = EmployeeType.valueOf(values[4]); Employee employee = new Employee(id, firstName, lastName, payPerHour, employeeType); employees.add(employee); } } catch (IOException e) { e.printStackTrace(); } }

}

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

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions