Change Code 1 to write to a text file using a PrintWriter chained to a FileOutpu
ID: 3847885 • Letter: C
Question
Change Code 1 to write to a text file using a PrintWriter chained to a FileOutputStream (instead of System.out), and DON'T use any encoding (write code similar to the above example). Instantiate a FileOutputStream in a try block, and the catch clause, display an error message. Make sure you don't try to call any PrintWriter methods if the exception was caught.
Code 1: Using the example code for the abstract Employee class, write a subclass of the abstract Employee class called Commissioned, which includes private instance variables for amount sold and commission rate, and a constructor that includes parameters to initialize those as well as the superclass variables. Don't forget to override the computePay method (just multiply the amount sold times the commission rate).
Employee [] empArray= new Employee[] {
new Commissioned("Donald Duck", "123-45-6789", 5000.0, 0.22),
new Salaried("Mickey Mouse", "987-65-4321", 100000.),
new Salaried("Bugs Bunny", "121-21-2121", 90000.),
new Commissioned("Elmer Fudd", "343-43-4343", 3000., 0.25) };
ANSWERS to Exercise 7.2:
public class Commissioned extends Employee {
private double amountSold;
private double commRate;
public Commissioned( String name, String ssn,
double sold, double hr){
super(name, ssn);
setAmountSold(sold);
setCommRate(hr);
}
public boolean setAmountSold( double sold ){
if( sold>=0 ){
amountSold = sold;
return true;
}
return false;
}// end setAmountSold
public boolean setCommRate( double hr ){
if( hr>=0 ){
commRate=hr;
return true;
}
return false;
}// end setcommRate
public double getAmountSold(){ return amountSold; }
public double getCommRate(){ return commRate; }
// please see textbook about Override annotation
@Override
public double computePay(){
double pay;
if( amountSold <= 40)
pay= amountSold*commRate;
else // overtime pay is optional in the exercise
pay = 40*commRate + (amountSold-40)
*commRate*1.5;
return Math.round(pay*100.)/100.;
}
public String toString(){
return "Commissioned: name=" + name +
", SSN=" + ssn + ", Hours Worked=" +
amountSold + ", Commissioned Rate = " +
commRate;
}
} // end class Commissioned
//driver class:
public class TryAbstractEmployees {
public static void main( String [] args ){
Employee[] empArray;
empArray = getEmployeeArray();
// DON'T USE AN IF STATEMENT TO DETERMINE IF
// THE ELEMENT IS Salaried OR Commissioned
for( int i=0; i < empArray.length; ++i ){
System.out.printf("%s, pay = %.2f ",
empArray[i].toString(),
empArray[i].computePay());
} // end for
// enhanced for loop:
for( Employee emp: empArray ){
System.out.println(emp + ", pay = "+
emp.computePay());
} // end for
// The following is a call to a method which may help in
// Prog. HW#5:
int numSalOver = countOverSal(empArray);
System.out.println("There are " + numSalOver +
" Salaried Employees that have a salary > 100000");
} // end
public static Employee [] getEmployeeArray(){
return new Employee[] {
new Commissioned("Donald Duck", "123-45-6789", 5000.0, 0.22),
new Salaried("Mickey Mouse", "987-65-4321", 100000.),
new Salaried("Bugs Bunny", "121-21-2121", 90000.),
new Commissioned("Elmer Fudd", "343-43-4343", 3000., 0.25) };
} // end getEmployeeArray
/* EXAMPLE OF A METHOD THAT RETURNS HOW MANY SALARIED EMPLOYEES HAVE A SALARY >= 100000 (this may help in Prog. HW#5) */
public static int countOverSal(Employee [] empArray){
int count=0;
for(int i=0; i < empArray.length ; ++i){
if(empArray[i] instanceof Salaried ){
Salaried salEmp = (Salaried)empArray[i];
if( salEmp.getSalary() >= 100000 )
++count;
} // end if
} // end for i
// OR ANOTHER WAY:
for(int i=0; i < empArray.length ; ++i){
if(empArray[i] instanceof Salaried ){
// CAN'T DO: empArray[i].getSalary()
if(( (Salaried)empArray[i]).getSalary()
>= 100000 )
++count;
} // end if
} // end for i
return count;
} // end countOverSal
}
Explanation / Answer
TryAbstractEmployee.java:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class TryAbstractEmployees {
public static void main(String[] args) {
PrintWriter output;
try {
output = new PrintWriter(new FileOutputStream("output.txt"));
} catch (FileNotFoundException e) {
System.out.println("Unable to open output file.");
return;
}
Employee[] empArray;
empArray = getEmployeeArray();
for (int i = 0; i < empArray.length; ++i) {
output.printf("%s, pay = %.2f ", empArray[i].toString(),
empArray[i].computePay());
}
output.println();
for (Employee emp : empArray) {
output.println(emp + ", pay = " + emp.computePay());
}
output.println();
int numSalOver = countOverSal(empArray);
output.println("There are " + numSalOver
+ " Salaried Employees that have a salary > 100000");
output.close();
System.out.println("Program completed Successfully.");
}
public static Employee[] getEmployeeArray() {
return new Employee[] {
new Commissioned("Donald Duck", "123-45-6789", 5000.0, 0.22),
new Salaried("Mickey Mouse", "987-65-4321", 100000.0),
new Salaried("Bugs Bunny", "121-21-2121", 90000.0),
new Commissioned("Elmer Fudd", "343-43-4343", 3000., 0.25) };
} // end getEmployeeArray
/*
* EXAMPLE OF A METHOD THAT RETURNS HOW MANY SALARIED EMPLOYEES HAVE A
* SALARY >= 100000
*/
public static int countOverSal(Employee[] empArray) {
int count = 0;
for (int i = 0; i < empArray.length; ++i) {
if (empArray[i] instanceof Salaried) {
Salaried salEmp = (Salaried) empArray[i];
if (salEmp.getSalary() >= 100000)
++count;
} // end if
} // end for i
return count;
} // end countOverSal
}
output.txt: (created in same folder):
Commissioned: name=Donald Duck, SSN=123-45-6789, Hours Worked=5000.0, Commissioned Rate = 0.22, pay = 1645.60
Salaried: name=Mickey Mouse, SSN=987-65-4321, Salary=100000.0, pay = 1200000.00
Salaried: name=Bugs Bunny, SSN=121-21-2121, Salary=90000.0, pay = 1080000.00
Commissioned: name=Elmer Fudd, SSN=343-43-4343, Hours Worked=3000.0, Commissioned Rate = 0.25, pay = 1120.00
Commissioned: name=Donald Duck, SSN=123-45-6789, Hours Worked=5000.0, Commissioned Rate = 0.22, pay = 1645.6
Salaried: name=Mickey Mouse, SSN=987-65-4321, Salary=100000.0, pay = 1200000.0
Salaried: name=Bugs Bunny, SSN=121-21-2121, Salary=90000.0, pay = 1080000.0
Commissioned: name=Elmer Fudd, SSN=343-43-4343, Hours Worked=3000.0, Commissioned Rate = 0.25, pay = 1120.0
There are 1 Salaried Employees that have a salary > 100000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.