You are on page 1of 11

Laboratory Activity 2 for OOP

Lee, Jennefer Brazil


IT21S3
October 26, 2021
Integrative Programming and Technologies

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

-------------------------Actual codes ---------------------------

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;

/*Implementing the setter for the name */


public void setName(String name){
this.name=name;
}

/*Implementing the getter for the name */


public String getName(){
return name;
}
}

FullTimeEmployee.java

package ACT2_OOP;

/*FullTimeEmployee is the child class inherited from Employee class*/


public class FullTimeEmployee extends Employee{

/*Declaring all the variables based on the given information on the activity */
private double monthlySalary;

/*Implementing the setter for the monthlySalary */


public void setMonthlySalary(double monthlySalary){
this.monthlySalary=monthlySalary;
}

/*Implementing the getter for the monthlySalary */


public double getMonthlySalary(){
return monthlySalary;
}
}
PartTimeEmployee.java

package ACT2_OOP;

/*PartTimeEmployee is the child class inherited from Employee class*/


public class PartTimeEmployee extends Employee{
/*Declaring all the variables based on the given information on the activity */
private double ratePerHour;
private int hoursWorked;
private double wage;
/*Implementing the setter for the ratePerHour */
public void setRatePerHour(double ratePerHour){
this.ratePerHour=ratePerHour;
}
/*Implementing the getter for the ratePerHour */
public double getRatePerHour(){
return ratePerHour;
}
/*Implementing the setter for the hoursWorked */
public void setHoursWorked(int hoursWorked){
this.hoursWorked=hoursWorked;
}
/*Implementing the getter for the hoursWorked */
public int getHoursWorked(){
return hoursWorked;
}
/*Implementing the setter for the wage */
public void setWage(double wage){
this.wage=wage;
}
/*Implementing the getter for the wage */
public double getWage(){
return wage;
}
}
------------------------Screenshot of Output ---------------------------

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.

After making the parent class, FullTimeEmployee.java is made


as the first child class which will inherit the data of the
parent class. Similar to the parent class, a setter and getter
are also used for monthlySalary a data type of double is used so
that decimals on the output are valid. The next class made is the
PartTimeEmployee.java. PartTimeEmployee contains three private
variables, ratePerHour, hoursWorked, wage then uses setters to
store data and getters to capture the value of the specific
variable.

The last class I made is LabExer2.java which contains the


main and executable file. After the user chooses between P
(partTime) or F (fullTime) the conditional statement will be
processed. The function char c=s.nect().chatAt(0); reads the user
input and chooses the appropriate statement to run. If P is
entered by the user the user will be asked to enter their rate
per hour and the number of hours worked separated by space. The
function input.split(“ ”) makes it possible for the program to
read the next input from the user. Data type double is used for
the two variables (ratePerHour, and hoursWorked) so the final
output can show decimals. Additionally, we parsed the variable to
be able to find a double data type on the output. Lastly, on the
execution part, I used %.2f so two decimals are shown after the
whole number in the output.

Honor Pledge for Graded Assignments


“I affirm that I have not given or received any unauthorized help on
this assignment and that this work is my own.”
-Lee, Jennefer Brazil

You might also like