Professional Documents
Culture Documents
Write a program to print the area of a rectangle by creating a class named 'Area' having two
methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and
the second method named as 'getArea' returns the area of the rectangle. Length and breadth of
rectangle for two cases are (4,5) and (8,12). Find for a square having only parameter side length 5.
Source Code-
public class Area
{
int length,breadth;
Area(int l, int b)
{
length = l;
breadth = b;
}
int side;
Area(int s)
{
side=s;
}
public int setDim()
{
int results = length * breadth;
return results;
}
public int setdem()
{
int sarea = side*side;
return sarea;
}
public void getArea(){
System.out.println("Area = " + setDim());
System.out.println("Area=" + setdem());
}
public static void main(String []args){
Area x =new Area(4,5);
Area y =new Area(8,12);
Area z =new Area(5);
x.getArea();
y.getArea();
z.getArea();
}
}
Q 2.Write a program by creating an 'Employee' class having the following methods and print the
final salary.
1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as parameter
2 - 'AddSal()' which adds $10 to salary of the employee if it is less than $500.
3 - 'AddWork()' which adds $5 to salary of employee if the number of hours of work per day is more
than 6 hours.
Source Code-
import java.util.Scanner;
class Employee
getInfo()
AddSal()
double sal;
}
AddWork()
double sal; //local variable this will be limited to this AddWork function
int hours;
if(hours > 6)
sal =sal + 5;
obj.getInfo();
obj.AddSal();
obj.AddWork();