You are on page 1of 3

CAT 2 EDINAH GETANGE

Java programming

(a) Advantages of encapsulation


Encapsulation protects an object from unwanted access by clients.
Encapsulation allows access to a level without revealing the complex details below that level.
It reduces human errors.
Simplifies the maintenance of the application
Makes the application easier to understand
(b)
Employee.java

Public class Employee{

Private int employeeId;


Private String employeeName;
Private double salary;
Private double netSalary;

//setters
Public void setEmployeeId(int employeeId){
This.employeeId=employeeId;
}
Public void setEmployeeName(String employeeName){
This.employeeName=employeeName;
}
Public void setSalary(double salary){
This.salary=salary;
}
Public void netSalary(double netSalary){
This.netSalary=netSalary;
}

//getters
Public int getEmployeeId(){
Return employeeId;
}
Public String getEmployeeName(){
Return employeeName;
}
Public double getSalary(){
Return salary;
}
Public double getNetSalary(){
Return netSalary;
}

Public void calculateNetSalary(int pfpercentage){

Pfamount=salary*pfpercentage;
netSalary=salary-pfamount;
}
}

Public void main(String[] args){


Scanner sc = new Scanner(System.in);
Employee emp = new Employee();

Emp.setEmployeeId(sc.nextInt());
Emp.setEmployeeName(sc.next()) ;
Emp.setSalary(sc.nextDouble());

System.out.println(“Enter PF percentage:”);
Double pfpercentage = sc.nextDouble();
Emp.calculateNetSalary(pfpercentage);
System.out.println(“Salay is “ + emp.getNetSalary());
}

(C)

Import java.util.Scanner;
Public class Biggest_Number
{
Public static void main(String[] args)
{
Int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print(“Enter the first number:”);
X = s.nextInt();
System.out.print(“Enter the second number:”);
Y = s.nextInt();
System.out.print(“Enter the third number:”);
Z = s.nextInt();
If(x > y && x > z)
{
System.out.println(“Largest number is:”+x);
}
Else if(y > z)
{
System.out.println(“Largest number is:”+y);
}
Else
{
System.out.println(“Largest number is:”+z);
}

}
}

You might also like