This Java class, Record, defines a record for storing data in a random access file. It has two attributes: `name` (a string with a maximum length of 16 characters) and `id` (a string with exactly 4 digits). The class provides methods to read and write records to/from the file, as well as getter methods for retrieving the `id` and `name` values.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
This class defines a recordwhichisspecificallyforstoringin a Java random access file. SeeAPIofRandomAccessFileclassinJava. Attributes: name - String, the max lengthis16chars id - String, the numberofdigitsinidis4. * */ import java.util.*; import java.io.*; public class Record { private String name; private String id; private int NAMELENGTH=16; private int IDLENGTH=4; public Record() { id = " "; name = " "; } public Record(String newId,StringnewName) { id = newId; name = newName; } //Read a 'name' and therelated'id'fromthecurrentpositionof //the given file to theinstancevariables'name'and'id' public void read(RandomAccessFilefile)throwsIOException { id = readString(file,IDLENGTH); name = readString(file,NAMELENGTH); } //Write the value of 'this'objecttothegivenfile public void write(RandomAccessFilefile)throwsIOException { writeStr(file, id,IDLENGTH); writeStr(file, name,NAMELENGTH); } public String getId() { return id; }
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.