You are on page 1of 2

Problem 1: Create an Interface Computation and modify Employee and Salaried classes so that now they are part

of com.mycompany.payroll package. Create an interface Computation with two methods computeSalary and showPayslip i n the package com.mycompany.payroll. Also modify Salaried class so that it imple ments Computation and defines its methods. Solution: Step 1: Create an interface Computation in Computation.java file. This interface has two methods as given below. Package com.mycompany.payroll; public interface Computation { double computeSalary( ); void showPaySlip( ); } // end of interface Computation Step 2: Compile the code with the d switch on the command line. The dot specifies that the directories of the package must be created under current directory. javac d . Computation.java

Step 3: Observe the path in which the Computation.class file is created. If you are working in <USER_DIR>, then Computation.class file will be created under <US ER_DIR>\com\mycompany\payroll\ subdirectory. Step 4: Modify Employee.java file and make it a part of com.mycompany.payroll pa ckage. Step 5: Compile and create Employee.class file, which must be created in the sam e directory as Computation.class file. Step 6: Modify Salaried.java file and make it a part of com.mycompany.payroll pa ckage. Also make the change as specified in problem statement, and save the file . Step 7: Compile and create Salaried.class file, which must be created in the sam e directory as Computation.class file. Note: Copy the files Employee.java and Salaried.java from the current directory into s ome other directory before you test the package. This step is required here beca use if the UseSalaried.java file which we will use to test the package, is in th e same folder as that of Employee and Salaried source/class files then the compi ler will pick the class file from the current directory rather than taking it fr om the package. This happens because the classpath variable contains the local d irectory (dot) value set. Check: All of the above mentioned class files must have been created under <USER_DIR>\c om\mycompany\payroll\ folder. Step 8: Set the classpath variable to the current directory so that it can refer

to the class files created in the package. To do this follow the steps mentione d in Setting Classpath Environment Variable section and append the path given belo w. <USER_DIR>; Step 9: Test the Interface and Package: Test the Salaried class by creating the following (UseSalaried.java) java file. This class belongs to the default packag e. // To Do: Import the required files class UseSalaried { public static void main(String[] args) { Salaried s1=new Salaried(102,"BB","Accountant",53,10,50); s1.showPaySlip(); } } Step 10: Compile and execute the file and write the results here.