LAB EXERCISE 21: Employee Data 1
In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2. Import the Library Classes which allow data to be written to an external text file
a. import java.io.File
b. import java.io.FileWriter
c. import java.io.BufferedWriter
d import java.io.IOException
3. Create a Superclass
4. Create a Subclass
5. Declare Public Variables that can be accessed by a Subclass
6. Create a Constructors
7. Create a Void Method
8. Use a Try-Catch block statement for error file handling and exceptions
9. Create the Object from the Class
10. Call the Object’s Variables
This program prompts the user to the input employee information and then write and store the data into a text file - employee.txt.
1. Create a folder on your desktop Exercise-21
2. Launch Java EE- Eclipse
Note: You will need to use the Java Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-21
a. Select File-> Switch Workspace
b. Browse and select your Exercie-21 folder as your Workspace.
1. Create a Java Project and name it as EmployeeDataPart1
2. Create the first Class that will have the Main Method
a. Name the Class as MainApp
b. Choose the main method to insert into the class
a class called Employee.
This will be the superclass (parent)
4. Create a class called Data. This subclass that will inherit the methods and variables of the Employee superclass
C. Requirements for the Employee Class:
the import java.util.Scanner class
which will allow data to be inputted from the console.
the public variables using String data type that will be used in the subclass
fName, lName, employeeID, department, phone and emailAddress
the Constructor called Employee that will prompt the
user to input the data
Below is the code for the Constructor.
declare the scanner object that will be used to input the employee's first
declare the scanner object that will be used to input the employee's
declare the scanner object that will be used to input the employee's phone
D. Requirements for the Data Class:
the following Library Classes which will write and store the data to an
external text file:
a Void Method
called writeData () that will execute the code to write the data to an
external file – employee.txt
a. Create instance of the object filename from the File library class & pass the file name “employee.txt” as a parameter
File filename = new File("employee.txt");
b. Use Try-Catch
block to perform error handling if the program cannot write to the external
b1. Create the instance of the object "fw" from the FileWriter library class
& pass value of "filename" object as the parameter
FileWriter fw = new FileWriter(filename);
b2. Create instance of the object "br" from the BufferredWriter library class
& pass the value of the "fw" object as the parameter
BufferedWriter br = new BufferedWriter(fw);
b3. Write the data to the employee.txt file using "br" object write method
b4. Close the text file - employee.txt
c. In the Catch-block
parameter, add (IOException e)
Insert the code below to display the error handling message
System.out.println("Unable to write to file " + filename.toString());
E. Requirements for the MainApp Class:
comments (documentation)– Program Description, Author and Date
an instance of the object - employeeInfo from the
Data employeeInfo = new Data()
3. Call the object's method- writeData:
4. Display the output message to the console
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time errors.
4. Use the following set of test data to determine if the application is running properly
5. Expand the Project Folder. Press F5 to Refresh. Verify the employee.txt is in the root of the project folder.
G. Submit your exercise in the Canvas Lab Exercise #21 Drop Box.
1. Submit the screen shot of the Eclipse
Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2. Submit the employee.txt data file.
3. Zip up and submit the compressed EmployeeDataPart1 subfolder that is in the Exercise-21
NOTE: Right click on the subfolder and select Send to “Compress Folder”. The file will have a file extension of .zip.
NOTE: You will need to upload the 3 files above separately.