Professional Documents
Culture Documents
Instruction:
Use word document to submit your answer. Document must have full
screenshot of syntax and its output. Answer the activity below.
-------------------------Screenshot of codes ---------------------------
LabExer2.java
Employee.java
*All the description for the code is included as a comment on the IDE
FullTimeEmployee.java
PartTimeEmployee.java
*All the description for the code is included as a comment on the IDE
LabExer2
package ACT2_OOP;
import java.util.Scanner; //Importing Scanner to take inputs
//This contains the main class and all the things to be executed
public class LabExer2
{
public static void main(String[] args) {
//Instantiating scanner class object
Scanner s=new Scanner(System.in);
//Prompting the user to enter their name
System.out.println("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
System.out.println(" SIMPLE PAYROLL CONSOLE PROGRAM"
);
System.out.println("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
System.out.println("Enter name: ");
System.out.print("> ");
String name=s.nextLine();
//Prompting the user to choose between FullTimeEmployee or PartTimeEmployee
System.out.println("Press F for Full Time or P for Part Time");
System.out.print("> ");
//This gets the first character of the next input.
char c=s.next().charAt(0);
//Using conditional statements in order to choose between P and F
if(c=='P' || c=='p'){ //using or statement so both lower and uppercase can
be used by the user
//Prompting the user for ratePerHour and noOfHours worked
System.out.println(System.lineSeparator()+"Enter rate per hour and no.
of hours worked seperated by space:");
System.out.println("*******************Sample: 107.50
13***************************");
System.out.print("> ");
s.nextLine();
//Taking the input separated by spaces by splitting the values by space
String input=s.nextLine();
String str[]=input.split(" ");
/*Parsing ratePerHour to double, to convert the string representation of
a number to its 32-bit
signed integer equivalent*/
double ratePerHour=Double.parseDouble(str[0]);
//Parsing noOfHours to int
int noOfHours=Integer.parseInt(str[1]);
//Creating an object of PartTimeEmployee by calling PartTimeEmployee as
p
PartTimeEmployee p=new PartTimeEmployee();
//Calling setter for name from the parent class
p.setName(name);
//Calling setter for ratePerHour from the child class PartTimeEmployee
p.setRatePerHour(ratePerHour);
//Calling setter for noOfHours from the child class PartTimeEmployee
p.setHoursWorked(noOfHours);
//Calling setter for wage and passing ratePerHour*noOfHours from the
child class PartTimeEmployee
p.setWage(ratePerHour*noOfHours);
//Printing name and wage using getter methods
System.out.println(System.lineSeparator()+"Name: "+p.getName());
System.out.printf("Wage: %.2f",p.getWage());
System.out.println(System.lineSeparator()+"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
}
//Output and process if the user choose F
else if(c=='F' || c=='f'){
//Prompting the user to put monthly salary
System.out.println("Enter the monthly salary: ");
System.out.print("> ");
double salary=s.nextDouble();
//Creating an object of FullTimeEmployee
FullTimeEmployee f=new FullTimeEmployee();
//Calling setter for monthly salary from the child class
FullTimeEmployee
f.setMonthlySalary(salary);
//Calling setter for name from the parent class
f.setName(name);
//Printing name and salary using getter methods
System.out.println(System.lineSeparator()+"Name: "+f.getName());
System.out.printf("Salary: %.2f",f.getMonthlySalary());
System.out.println(System.lineSeparator()+"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄");
s.close();
}
}
}
Employee.java
package ACT2_OOP;
/*Employee class acts as the parent class where the initial variable will come*/
public class Employee{
/*Declaring all the variables based on the given information on the activity */
private String name;
FullTimeEmployee.java
package ACT2_OOP;
/*Declaring all the variables based on the given information on the activity */
private double monthlySalary;
package ACT2_OOP;
FullTimeEmployee.java
PartTimeEmployee.java
Explanation
The first thing I did was to study the UML class diagram,
and from there I knew that Inheritance is needed to complete the
process. The first class I made was the Employee class which
serves as the parent class of FullTimeEmployee and
PartTimeEmployee. This class contains the private variable name,
where the inputs of the user for the name will be stored and
retrieved by the setter and getter.