Free Version
Difficult

Construction Worker Class: Calculate Cost of Job

APCSA-K41EP5

A housing contractor knows he will use the following types of construction workers to repair a house: carpenters, roofers, and electricians. However, he may need other types of construction workers as he learns more about the repairs.

He asks a programmer to design a program that will be able to store the worker’s names, their hourly.rates, and expected hours on the job. There are some methods that are unique to each type of construction worker and must be coded differently.

Consider the following class to define the different construction workers.

public abstract class ConstructionWorker
{
private String name;
private double hourlyWage;
private int numHours;

public ConstructionWorker(String nam, double wage, int hrs)
{
name = nam;
hourlyWage = wage;
numHours = hrs;
}

public double getNumHoursOnJob()
{
return numHours;
}

public double getHourlyWage()
{
return hourlyWage;
}

public String getWorkerName()
{
return name;
}

public abstract ArrayList<String> determineValidJobs();

// other data and methods not shown
}

public class Carpenter extends ConstructionWorker
{
public Carpenter(String nam, double wage, int hrs)
{
super(nam, wage, hrs);
}
// other data and methods not shown
}

public class Roofer extends ConstructionWorker
{
public Roofer(String nam, double wage, int hrs)
{
super(nam, wage, hrs);
}
// other data and methods not shown
}

public class Electrician extends ConstructionWorker
{
public Electrician(String nam, double wage, int hrs)
{
super(nam, wage, hrs);
}
// other data and methods not shown
}


Which segments of code below would be BEST design for determining and printing the total cost of the job?

A
public static void main(String[] args)
{
Carpenter c1 = new Carpenter("Jake", 15.00, 100);
Carpenter c2 = new Carpenter("Billy", 12.00, 40);
Roofer r1 = new Roofer("Janet", 10.00, 20);
Electrician e1 = new Electrician("Pat", 14.00, 80);
System.out.println(getTotalCost(c1, c2, r1, e1));
}

public static double getTotalCost(Carpenter c1, Carpenter c2, Roofer r1, Electrician e1)
{
double totCost = 0;
totCost += c1.getHourlyWage() * c1.getNumHoursOnJob();
totCost += c2.getHourlyWage() * c2.getNumHoursOnJob();
totCost += r1.getHourlyWage() * r1.getNumHoursOnJob();
totCost += e1.getHourlyWage() * e1.getNumHoursOnJob();
}

B
public static void main(String[] args)
{
ArrayList<ConstructionWorker> workers = new ArrayList< ConstructionWorker >();
System.out.println(getTotalCost(workers));
}

public static double getTotalCost(ArrayList< ConstructionWorker > workers)
{
double totCost = 0;
for (int i=0; i<workers.size(); i++)
{
if (workers.get(i) instanceof Carpenter)
totCost +=
((Carpenter) workers.get(i)).getHourlyWage() *
((Carpenter) workers.get(i)).getNumHoursOnJob();
else
if (workers.get(i) instanceof Roofer)
totCost +=
((Roofer) workers.get(i)).getHourlyWage() *
((Roofer) workers.get(i)).getNumHoursOnJob();
else
totCost +=
((Electrician) workers.get(i)).getHourlyWage() *
((Electrician) workers.get(i)).getNumHoursOnJob();
}
}

C
public static void main(String[] args)
{
ArrayList<ConstructionWorker> workers = new ArrayList< ConstructionWorker >();
System.out.println(getTotalCost(workers));
}

public static double getTotalCost(ArrayList< ConstructionWorker > workers)
{
double totCost = 0;
for (int i=0; i<workers.size(); i++)
{
totCost += workers.get(i).getHourlyWage() * workers.get(i).getNumHoursOnJob();
}
}

D
public static void main(String[] args)
{
ArrayList< ConstructionWorker > workers = new ArrayList< ConstructionWorker >();
System.out.println(getTotalCost(workers));
}

public static double getTotalCost(ArrayList< ConstructionWorker > workers)
{
double totCost = 0;
for (Carpenter c : workers)
{
totCost += c.getHourlyWage() * c.getNumHoursOnJob();
}
for (Roofer r : workers)
{
totCost += r.getHourlyWage() * r.getNumHoursOnJob();
}
for (Electrician e : workers)
{
totCost += e.getHourlyWage() * e.getNumHoursOnJob();
}
}

E
public static void main(String[] args)
{
ArrayList< Carpenter > carpenters = new ArrayList< Carpenter >();
ArrayList< Roofer > roofers = new ArrayList< Roofer >();
ArrayList< Electrician > electricians = new ArrayList< Electrician >();

System.out.println(getTotalCost(carpenters) +
getTotalCost(roofers) +
getTotalCost(electricians) );
}

public static double getTotalCost(ArrayList< ConstructionWorker > workers)
{
double totCost = 0;
for (int i=0; i<workers.size(); i++)
{
totCost += workers.get(i).getHourlyWage() *
workers.get(i).getNumHoursOnJob();
}