Objective how to define a package so it can be used by different applications. A
ID: 3842547 • Letter: O
Question
Objective how to define a package so it can be used by different applications. As well as writing / reading data to different types of files.
PLEASE READ AND COMPLETE EACH OF THE TASKS. AT THE BOTTOM, YOU WILL SEE WHICH DELIVERABLES ARE REQUESTED. The templates provided have requests in the comments for additional code, please complete.
Task 1:
-Create a Java Application called EmployeeLab.
-Before hitting Finish
-After you hit next, make EmployeeLab your project name
-Uncheck the box at the bottom that says Create Main Class
-Hit Finish
Task 2:
-Under the Project Editor, right click EmployeeLab, choose New Java Package
-Enter employeelab for Package Name, Finish
Task 3:
-Under Source Packages, right click Employee, choose New Java Class
-Enter Employee for Class Name Finish
-Use the Employee.java template below:
-Copy and Paste the source code into your newly created Class file.
Task 4:
-Under Source Packages, right click Employee, choose New Java Class
-Enter HourlyEmployee for Class Name (Make sure Package says employeelab) Finish
-Use the following HourlyEmployee.java template below:
-Copy and Paste the source code into your newly created Class file.
Task 5:
-Under Source Packages, right click Employee, choose New Java Class
-Enter SalariedEmployee for Class Name Finish
-Use the SalariedEmployee.java template below:
-Copy and Paste the source code into your newly created Class file.
Task 6:
-Under Source Packages, right click Employee, choose New Java Class
-Enter SalariedEmployee for Class Name Finish
-Use the following SalariedEmployee.java template below:
-Copy and Paste the source code into your newly created Class file.
Task 6:
-Under Source Packages, right click Employee, choose New Java Class
-Enter CommissionEmployee for Class Name Finish
-Use the CommissionEmployee.java template below:
-Copy and Paste the source code into your newly created Class file.
Task 7:
-Under Source Packages, right click Employee, choose New Java Class
-Enter BasePlusCommissionEmployee for Class Name Finish
-Use the BasePlusCommissionEmployee.java Template below:
-Copy and Paste the source code into your newly created Class file.
Task 8
-Under the Project Editor, right click EmployeeLab, choose New Java Main Class
-Enter OutputBinary for Class Name, Verify Package name says Employee Finish
-Use the OutputBinary.java template below:
-Copy and Paste the source code into your new create Class file
Task 9:
-In Employee.java, you must modify the class name to make the class serializable.
Task 10:
In OutputBinary.java,
-you must import the necessary java.io packages
-you must write code that opens the file EmployeeData.bin for outputting the object output, then write all of the elements of the array employees into the file. Use ObjectOutputStream
Sample Output 1:
After successfully running your OutputBinary [Shift+F6] application, your output should like something like the following:
Employees processed individually:
salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
earned: $800.00
hourly employee: Karen Price
social security number: 222-22-2222
hourly wage: $16.75; hours worked: 40.00
earned: $670.00
commission employee: Sue Jones
social security number: 333-33-3333
gross sales: $10,000.00; commission rate: 0.06
earned: $600.00
base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
earned: $500.00
Output the elements of the array:
Task 11:
-Under the Project Editor, right click EmployeeLab, choose New Java Main Class
-Enter InputBinary for Class Name, Verify Package name says Employee Finish
-Use the InputBinary.java template below:
-Copy and Paste the source code into your new create Class file
Task 12
In InputBinary.java,
-you must import the necessary java.io packages
-you must write code that opens the file EmployeeData.bin for inputting the object output, then write all of the elements of the array employees to display. Use ObjectInputStream
Sample Output 2:
After successfully running your InputBinary application [Shift+F6], your output should like something like the following:
Employees processed polymorphically:
salaried employee: John Smith
social security number: 111-11-1111
weekly salary: $800.00
earned $800.00
hourly employee: Karen Price
social security number: 222-22-2222
hourly wage: $16.75; hours worked: 40.00
earned $670.00
commission employee: Sue Jones
social security number: 333-33-3333
gross sales: $10,000.00; commission rate: 0.06
earned $600.00
base-salaried commission employee: Bob Lewis
social security number: 444-44-4444
gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
new base salary with 10% increase is: $330.00
earned $530.00
Employee 0 is a Employee.SalariedEmployee
Employee 1 is a Employee.HourlyEmployee
Employee 2 is a Employee.CommissionEmployee
Employee 3 is a Employee.BasePlusCommissionEmployee
Task 13:
-Create a new application, called TextOutput, which will use the same base code as BinaryOutput but output the data to EmployeeData.txt.
-Use PrintWriter should be used instead of ObjectOutputStream.
Task 14:
Create a new application, called TextInput, which will read the newly created EmployeeData.txt and display the information to the display.
Task 15:
Create a new application, called ArrayOutput, which will use the same base code as BinaryOutput but output the complete employee array as a single element to EmployeeData.ary, instead of outputting each element individually.
Task 16:
Create a new application, called ArrayInput, which will read the newly created EmployeeData.ary as a single element and display the information to the display.
Deliverables:
Follow the instructions, you must implement code which is indicated by /* */. For example, Employee.java, InputBinary.java, and OuputBinary.java all contain areas where directions are posted in the comments. Also post all additional application code requested in the last 4 tasks (TextOutput, TextInput, ArrayOutput, ArrayInput).
Explanation / Answer
Due to limitations of time and effort I am only pasting the contents of the classes where some code changes has been done. The rest of the Classes that are mentioned in the above desciption remains same . We as part of Chegg are only asked to answer 4 questions , so the rest of Tasks from 14 onwards have been skipped.
Making Employee Class Serializable Task 9
import java.io.Serializable;
public abstract class Employee implements Serializable //New Modification for Task 9
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString()
{
return String.format( "%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
OutputBinary Class for Task 10
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class OutputBinary
{
public static void main( String args[] ) throws IOException
{
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
System.out.println( "Employees processed individually: " );
System.out.printf( "%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings() );
Employee employees[] = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
System.out.println( "Output the elements of the array: " );
/* Write code here that opens the file EmployeeData.ser for object output then
writes all the elements of the array employees into the file */
/* Make sure you implement a try block to catch any IO Exceptions */
//Start for Task 10
FileOutputStream fout=null;
ObjectOutputStream oos=null;
File file = new File("D:\Java Output\EmployeeData.ser");
try {
fout = new FileOutputStream(file);
oos = new ObjectOutputStream(fout);
// create four-element Employee array
for(Employee e:employees)
oos.writeObject(e);
oos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ////End for Task 10
} // end main
} // end class OutputEmployees
InputBinary Class for Task 12
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.lang.ClassNotFoundException;
// Lab Exercise 1, Follow-Up 1: InputEmployees.java
// Employee hierarchy test program.
public class InputBinary
{
public static void main( String args[] ) throws ClassNotFoundException
{
// create four-element Employee array
Employee employees[] = new Employee[ 4 ];
/* Implement try block to catch possible IO Exceptions */
/* Open the ObjectInputStream from the FileInputStream */
/* Read each element from the object file */
//Start for Task 12
try {
ObjectInputStream op=new ObjectInputStream(new FileInputStream("D:\Java Output\EmployeeData.ser"));
for(int i=0;i<4;i++)
employees[i]=(Employee) op.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//End for Task 12
System.out.println( "Employees processed polymorphically: " );
// generically process each element in array employees
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
// determine whether element is a BasePlusCommissionEmployee
if ( currentEmployee instanceof BasePlusCommissionEmployee )
{
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
( BasePlusCommissionEmployee ) currentEmployee;
double oldBaseSalary = employee.getBaseSalary();
employee.setBaseSalary( 1.10 * oldBaseSalary );
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
employee.getBaseSalary() );
} // end if
System.out.printf(
"earned $%,.2f ", currentEmployee.earnings() );
} // end for
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++ )
{
//Task 12 Start
System.out.println("Employee "+j+" is a Employee."+employees[j].getClass().getSimpleName());
//Task 12 Start
/* Output each element Employee Type */
}
} // end main
} // end class InputEmployees
TextOutput class for Task 13
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
public class TextOutput {
public static void main( String args[] ) throws IOException
{
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
System.out.println( "Employees processed individually: " );
System.out.printf( "%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings() );
Employee employees[] = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
/* Write code here that opens the file EmployeeData.ser for object output then
writes all the elements of the array employees into the file */
/* Make sure you implement a try block to catch any IO Exceptions */
//Start for Task 13
//PrintWriter can't be used to write objects to a file, instead here we have used
// PrintStream which stream the output to the file.
FileOutputStream fout=null;
PrintWriter ps=null;
PrintStream pos=null;
File file = new File("D:\Java Output\EmployeeData.txt");
try {
fout = new FileOutputStream(file);
// ps = new PrintWriter(fout);
pos=new PrintStream(fout);
//System.setOut(ps);
// create four-element Employee array
System.setOut(pos);
System.out.println( "Output the elements of the array: " );
System.out.printf( "%s %s: $%,.2f ",
salariedEmployee, "earned", salariedEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
hourlyEmployee, "earned", hourlyEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
commissionEmployee, "earned", commissionEmployee.earnings() );
System.out.printf( "%s %s: $%,.2f ",
basePlusCommissionEmployee,
"earned", basePlusCommissionEmployee.earnings() );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ////End for Task 13
} // end main
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.