Java Problems I. Solve the following programming problems in Java language . Pro
ID: 3836759 • Letter: J
Question
Java Problems
I. Solve the following programming problems in Java language. Provide the code and a screenshot of a test run of the code.
1. Modify the payroll system of chapter 10 to include the following:
i. Include an additional Employee subclass named PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. The class has private variables wage (the employee’s wage per piece) and pieces (number of pieces produced). Implement the earnings method in the class that multiplies the wage per piece produced.
ii. Create a test program with an array list of type Employee that holds an instance of each type of Employee.
2. An interface is defined below. Define an abstract class named Philosopher that implements the setters, getters and only the sleep() method. Two classes, Mathematician and Physicist, extend the Philosopher class and provide the implementation for the unimplemented methods. Test the implementations.
public interface Person {
void setFirstName(String firstName);
void setLastName(String lastName);
String getFirstName();
String getLastName();
void eat();
void think();
void sleep();
}
3. Design a program that merges the content of two text files containing chemical elements sorted by atomic number and produces a sorted file of elements. The program should read the content from the two files, and outputs the data ordered by atomic number to the output file. Assume the name of the output file. Use the provided files. The format of each file will be as following:
ATOMIC_NUMBER
ELEMENT_NAME
ELEMENT_ABBREVIATION
ATOMIC_WEIGHT
chems:
I Fig. 10.4: Employee. java 2 Employee abstract superclass. 4 public abstract class Employee 5 private String firstName; private String lastName: private String socialSecurityNumber three-argument constructor 10 II public Employee String first, String last, String ssn 12 13 firstName first lastName last I4 15 social SecurityNumber Ssn 16 end three-argument Employee constructor 17 set first name 19 public void setFirstNameC String first 20 firstName first should validate 21 22 end method set FirstName 23 Fig. 10.4 I Employee abstract superclass. (Part I of 3.) Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.Explanation / Answer
1.) (i)
PieceWorker.java:
package payrollsystem;
public class PieceWorker extends Employee {
private double wage; //wage per piece
private double pieces; //number of pieces produced
// five-argument constructor
public PieceWorker( String first, String last, String ssn, Date DayOfBirth, double Wage, double Pieces )
{
super( first, last, ssn, DayOfBirth);
setWage( Wage ); // validate and store wage
setPieces( Pieces ); // validate and store pieces;
} // end five-argument HourlyEmployee constructor
// set wage
public final void setWage( double Wage )
{
wage = ( Wage < 0.0 ) ? 0.0 : Wage;
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set Pieces
public final void setPieces( double Pieces )
{
pieces = ( ( Pieces >= 0.0 ) && ( Pieces <= 168.0 ) ) ?
Pieces : 0.0;
} // end method setPieces
// return pieces
public double getPieces()
{
return pieces;
} // end method getPieces
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getWage() * getPieces();
} // end method earnings
// return String representation of PieceWorker object
@Override
public String toString()
{
return String.format( "Piece Worker: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "Wage per piece", getWage(),
"Number of pieces produced", getPieces() );
} // end method toString
}
1.) (ii)
PayrollSystemTest.java:
package payrollsystem;
public class PayrollSystemTest
{
public static void main( String args[] )
{
// create subclass objects
Date currentDate = new Date(6,20,2010);
System.out.printf( "Current Date is: %s ", currentDate.toString() );
System.out.println("###################################");
SalariedEmployee salariedEmployee =
new SalariedEmployee( "John", "Smith", "111-11-1111", new Date(5,11,1984),800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( "Karen", "Price", "222-22-2222", new Date(6,15,1988),16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
"Sue", "Jones", "333-33-3333", new Date(8,25,1974),10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", new Date(9,27,1978),5000, .04, 300 );
PieceWorker pieceWorker = new PieceWorker("Ralph", "Tang", "777-223-987",
new Date(6,25,1985), 213, 16);
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() );
System.out.printf( "%s %s: $%,.2f ",
pieceWorker, "earned", pieceWorker.earnings() );
// create four-element Employee array
Employee employees[] = new Employee[ 5 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
employees[ 4 ] = pieceWorker;
System.out.println("###################################");
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
if(currentDate.getMonth()==currentEmployee.getBirthday().getMonth())
{
System.out.printf("earned $%,.2f. %s ", currentEmployee.earnings() + 100.00,
"Note: added a $100 bonus to your payroll amount for birthday!!!" );
}else{
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++ )
System.out.printf( "Employee %d is a %s ", j,
employees[ j ].getClass().getSimpleName() );
} // end main
} // end class PayrollSystemTest
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.