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.
How do I get Main-java to compile- the program is incomplete - Need.docx
1. 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:
3. 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");
}
4. 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.
*
5. * @author
*/
public class EmployeeManagerFileImpl implements EmployeeManager {
private List<Employee> 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<Employee> readAllEmployees() {
return employees;
}
6. /**
* 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();
}
}