0% found this document useful (0 votes)
3 views5 pages

Lab - 3 - Classes and Objects

The document outlines the implementation of three classes: Employee, Circle, and Shipment, each with specific attributes and methods. The Employee class manages employee information and calculates earnings based on job type, while the Circle class computes the area of a circle based on its radius. The Shipment class is designed to handle shipment details, including delivery options and price calculations with tax, and includes methods for setting and getting attributes.

Uploaded by

danhm8108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Lab - 3 - Classes and Objects

The document outlines the implementation of three classes: Employee, Circle, and Shipment, each with specific attributes and methods. The Employee class manages employee information and calculates earnings based on job type, while the Circle class computes the area of a circle based on its radius. The Shipment class is designed to handle shipment details, including delivery options and price calculations with tax, and includes methods for setting and getting attributes.

Uploaded by

danhm8108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1- Class Employee

1. Write a class called Employee with the following private attributes: name, job (Teaching
assistance, Lecturer), salary, overTime hours, earns.

2. Write a constructor that initialize the previous attributes.

3. A get and set method for name, job, salary, overTime hours.

4. Write a method which calculate the earns as following:

If the Job is “Teaching assistance” :

Earns = Salary + OverTime*150;

If the Job is “Lecturer” :

Earns = Salary + OverTime*200;

5. Write a method called print which print employee information.

6. Test your program.

Answer:

class Employee {
private String name;
private String job ;
private double salary;
private int overTime;
private double earns ;

public Employee(String n,String j,double s, int h){


setName(n);
setJob(j);
setSalary(s);
setOverTime(h);
}
public void setName(String name){
this.name = name;
}
public void setJob(String job){
this.job = ((job=="Teaching assistance")||(job=="Lecturer"))?job:"Lecturer";
}
public void setSalary(double salary){
this.salary = (salary)>0?salary:1000;
}
public void setOverTime(int hours){
overTime = (hours)>1?hours:1;
}
public String getName(){ return name;}
public String getJob(){ return job;}
public double getSalary(){ return salary;}
public int getOvetTime(){ return overTime;}
public void calculateEarns(){
if (job=="Teaching assistance")
earns = salary + overTime*150;
else
earns = salary + overTime*200;
}

public void print(){


System.out.println("Employee Name: "+name);
System.out.println("Employee Job: "+job);
System.out.println("Employee Salary: "+salary);
System.out.println("Employee Over Time: "+overTime);
calculateEarns();
System.out.println("Employee Net Salary: "+earns);
}
}
public class Test {
public static void main(String[] args) {
Employee e1=new Employee("Wala","Teaching assistance",1000,10);
e1.print();

System.out.println("");
e1.setJob("Lecturer");
e1.setSalary(10000);
e1.print();
}
}

2- Class Circle
1. Write a class named Circle with the following private instance attributes: point (x,y), radius

2. Write a constructor which initialize these properties (x,y and radius)

3. A get and set method for each of the three variables

4. Write a method called getArea which returns the area of the circle (PI * radius 2)

5. Write a method called Print which prints circle information.

6. Write an application to test your class.

Answer:

class Circle {

private int x;

private int y;

private double radius;

public Circle(int x,int y,double radius){

setX(x);

setY(y);

setRadius(radius);

public void setX(int x){

this.x = (x>0)?x:1;

public void setY(int y){

this.y = (y>0)?y:1;

public void setRadius(double radius){


this.radius = (radius>0)?radius:1;

public int getX(){ return x;}

public int getY(){ return y;}

public double getRadius(){ return radius;}

public double getArea(){

return (3.14*radius*radius);

public void print(){

System.out.println("Circle Point:( "+x+","+y+" )");

System.out.println("Circle Raduis: "+radius);

System.out.println("Circle Area: "+ getArea());

public class Test {

public static void main(String[] args) {

Circle c1=new Circle(5,10,-5.0);

c1.print();

System.out.println("");

c1.setRadius(2.0);

c1.print();

}
Lab work

1) Write a class called ”Shipment” that has the following attributes:

 A variable with type int and its name ”Number”.


 A variable with type String and its name ”Destination”.
 A private variable with type String and its name ”DeliveryOptions”.
 A variable with type double and its name ”DeliveryPrice”.
Create the following constructor:
 A constructor for the class which takes the four variables you defined above.
Create the following methods:
 A get and set method for each of the four variables. The variable ”DeliveryOptions” must
take either ”PickUp” or ”HomeDelivery”. You may do that in the set method for
”DeliveryOptions”
 A method called ”CalPriceTax” of type double that calculates the delivery price with the
tax as follows:

PriceWithTax=(DeliveryPrice*0.15)+DeliveryPrice

2) Write an application to test your class.

Answer:

You might also like