Examlex

Solved

Import Java.nio.file.*; Import Java.io.*;

question 62

Essay

import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class CreateEmployeesRandomFile
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Path file =
Paths.get("C: \\Java\\Chapter.13\\RandomEmployees.txt");
String s = "000, ,00.00" +
System.getProperty("line.separator");
final int RECSIZE = s.length();
FileChannel fc = null;
String delimiter = ",";
String idString;
int id;
String name;
String payRate;
final String QUIT = "999";
try
{
fc = (FileChannel)Files.newByteChannel(file, READ, WRITE);
System.out.print("Enter employee ID number >> ");
idString = input.nextLine();
while(!(idString.equals(QUIT)))
{
_____________________________
System.out.print("Enter name for employee #" +
id + " >> ");
name = input.nextLine();
System.out.print("Enter pay rate >> ");
payRate = input.nextLine();
s = idString + delimiter + name + delimiter +
payRate + System.getProperty("line.separator");
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
_____________________________
fc.write(buffer);
System.out.print("Enter next ID number or " +
QUIT + " to quit >> ");
idString = input.nextLine();
}
fc.close();
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
The above program will accept any number of employee records as user input and write the records to a file in a loop. In the first shaded line, create the statement to accept the employee data value from the keyboard as a String and convert it to an integer using the parseInt() method. In the second shaded line, create the statement to compute the record's desired position by multiplying the ID number value by the record size.


Definitions:

Ordinal Responses

Data that represent categories with a meaningful order but not necessarily equidistant intervals between categories.

Interval Responses

Responses or data that range within a specific interval, often used in surveys or statistical analysis to understand distribution or preference.

Interval Data

Refers to a type of numerical data that has meaningful distances between measurements but no true zero point, allowing for addition and subtraction operations but not true ratio comparisons.

Bar Chart

A graphical display that uses bars of different heights or lengths to represent data values of various categories for comparison.

Related Questions