Computers and Technology

JAVA PROGRAMMING Lab - Chapter 10b , Abstract Classes (Interfaces)

Interfaces are also known as "contracts" because an Interface enforces certain behaviors for all subclasses in the hierarchy of the superclass - (i. e. all Classes that inherit from the super class which is an Abstract base Class). However, that behavior for all subclasses can be overridden and specialized for each subclass - POLYMORPHISM. Let's Learn more:

1. Read Chapter 10 sections 10.8 through 10.12

2. Chapter Review Questions and Exercises.

3. [10 points] submit updated FactoryWorker. java

[10 points] submit updated SalesWorker. java

FactoryWorker. java and SalesWorker. java implements our Interface:

Worker. javaPreview the document.

/* Worker Interface */

public interface Worker {

public double calculateOvertimePay(int hours);

public double calculateRegPay(int hours);

public void printPayStub(int hours);

public String toString();

}

We no longer need a specialized calculations for pay: calculateFacPay() and calculateSalPay()

INSTEAD - we will implement the Interface Worker. java and our calculations for Reg pay and Overtime pay will INCLUDE "implement" the requirements for the calculations as described in our last lab for each classification of worker: Factory or Sales. This will illustrate polymorphic behavior - where at run-time our program figures out which calculateRegPay() / calculateOvertimePay() to call and calls the correct one depending on the object instantiated.

Since Worker. java is an Interface, it is the "contract" on how I can use objects the implement Class Worker. I can write the test driver program before you have even written the implementation. Once you have completed your implementation, put all your .java files in the same directory, compile the test driver, and run the test driver. If all goes well you have successfully implemented our Worker Interface!

Test Driver: WorkerInterfaceT. javaPreview the document

import java. util. Scanner;

public class WorkerInterfaceT {

public static void main(String[]args){

Scanner input = new Scanner(System. in);

Worker w[] = new Worker[3];

for (int i = 0; i < w. length; i++)

{

System. out. println("Please enter S for Sales and F for Factory");

String wc = input. nextLine();

if(wc. equalsIgnoreCase("F")) {

w[i] = new FactoryWorker();

//get input and call your Factory setters()

}

else {

w[i] = new SalesWorker();

//get input and call your Sales setters()

}

System. out. println("Enter hours worked");

int hours = input. nextInt();

//polymorphic behavior

w[i].printPayStub(hours);

input. nextLine(); // clear input buffer

}

}

}

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 04:10, cvbnkifdfg
2pointswho was mikhail gorbachev? oa. a russian leader who opposed a coupob. a polish leader who founded the labor union "solidarityoc. a soviet leader who called for a closer relationship with the unitedstates, economic reform, and a more open societyd. a soviet leader who called for more oppression in the soviet union
Answers: 3
image
Computers and Technology, 23.06.2019 15:00, Siris420
Plz ( which is an example of a good url?
Answers: 1
image
Computers and Technology, 23.06.2019 18:30, janny48
How often does colleges update the cost of attendance on their website? . a)every two years b) every four years c) every year d) every semester
Answers: 1
image
Computers and Technology, 24.06.2019 01:00, kayranicole1
What shows the web address of the page that is currently displayed in the workspace? status window toolbar location bar internet box
Answers: 1
Do you know the correct answer?
JAVA PROGRAMMING Lab - Chapter 10b , Abstract Classes (Interfaces)

Interfaces are also k...

Questions in other subjects:

Konu
Computers and Technology, 24.07.2019 14:00