Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Employee(String dob, String name, Stringboss) //(constructor) //dob should be mm

ID: 3612783 • Letter: E

Question


     Employee(String dob, String name, Stringboss)   //(constructor)
    //dob should be mm/dd/yyyy, and boss should belength 0 to indicate no boss.

    int    getAge()
   String getName()
   String getBoss() //returns length0 string ifnone
   String getDob()

    class EmployeeFilerepresents an employee file -- it knows how to read a properlyformatted employee file, and how to write one in the properformat.

    EmployeeFile( String fName)     //can be the name of an existing file toread from, or the name of a file to write to.

    Employee[] readAll( );   //returns an array of all the Employees from its file.
    boolean writeAll( Employee[] a );  //replaces its file with properly formatted data for theEmployees
1) Suppose that empList is anArrayList<Employee>. You have 3 Strings, name, boss, and dob holding the data forsome employee. Write code that creates an Employee from yourStrings and then adds it to the empList. (Hint: classEmployee is specified above. You can use one of the add methods ofArrayList to add it after you have constructed it.)


2) Suppose that the String line holds the data for anemployee in the following format:   name height dob boss   where name, height, dob,and boss don't have any internal spaces, but boss may bemissing. Create an Employee from line. (Hint: attach aScanner to line and use next() and hasNext())

3) I have obtained a File oldF from a user (througha JFileChooser). I want to write output to a different file,one that has the same name but has the extension .edat instead ofwhatever extension your original File had. ("Extension" meansthe part after the dot at the end of the name of a file). Write Java code to create the new File object. (Hint: File hasmethods to obtain various versions of it name. You can usemyString.lastIndexOf(".") to find where the extension begins. Youcan use myString.substring( ) to get rid of the oldextension.

4) Suppose that list is an ArrayList<String>. Write code to create and populate a String[] of just the rightsize. (Hint: create the array based on the size of thelist, and then use a loop to copy the Strings from the listto the array.)

Explanation / Answer

1. empList.add(new Employee(dob, name,boss)); 2. Scanner chopper=newScanner(line); String name=chopper.next(); Stringheight=chopper.next(); String dob=chopper.next(); Stringboss=chopper.nextLine().trim(); empList.add(new Employee(dob, name,boss)); 3. try {    Stringname=oldF.getCanonicalPath();    // remove the extension    name=name.substring(0,name.lastIndexOf("."));    name+=".edat";    // rename file    // rename file    oldF.renameTo(newFile(name)); }catch(IOException e){} 4. // createarray String[] array=newString[list.size()]; // loop through listcontents for(int i=0; i<array.length;i++) {    // assign values in list to array    array[i]=list.get(i); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote