OOP lab 7
SOURCE
CODE:
public class Employee {
private String
Firstname;
private String
Lastname;
private String
Cnic;
Employee()
{
}
Employee(String
Firstname,String Lastname,String Cnic)
{
this.Firstname=Firstname;
this.Lastname=Lastname;
this.Cnic=Cnic;
}
public void
setFirstname(String Firstname) {
this.Firstname
= Firstname;
}
public void
setLastname(String Lastname) {
this.Lastname
= Lastname;
}
public void
setCnic(String Cnic) {
this.Cnic =
Cnic;
}
public String
getFirstname() {
return
Firstname;
}
public String
getLastname() {
return
Lastname;
}
public String
getCnic() {
return Cnic;
}
@Override
public String
toString()
{
return
"First Name : "+getFirstname()+"
Last Name: "+getLastname()+" Cnic: "+getCnic();
}
public double
earning()
{
return 0.0;
}
}
ublic class SalariedEmployeeclass extends Employee {
private double
weeklysalary;
public
SalariedEmployeeclass() {
}
public
SalariedEmployeeclass(String Firstname,String Lastname,String Cnic,double
weeklysalary) {
super(Firstname,Lastname,Cnic);
this.weeklysalary = weeklysalary;
}
public double
getWeeklysalary() {
return
weeklysalary;
}
public void
setWeeklysalary(double weeklysalary) {
if(weeklysalary>0)
this.weeklysalary = weeklysalary;
}
public class hourlyemployee extends Employee{
private double
wage;
private double
hour;
public
hourlyemployee() {
}
public
hourlyemployee(String Firstname,String Lastname,String Cnic,double wage, double
hour) {
this.wage =
wage;
this.hour =
hour;
}
public double
getWage() {
return wage;
}
public void
setWage(double wage) {
if(wage>0)
this.wage =
wage;
}
public double
getHour() {
return hour;
}
public void
setHour(double hour) {
if(hour>0)
this.hour =
hour;
}
@Override
public String
toString() {
super.toString();
return
"hourlyemployee{" + "wage=" + wage + ", hour=" +
hour + '}';
}
@Override
public double
earning() {
if(hour<=40)
{
return wage*hour;
}
else
{
return
40*wage+(hour-40)*wage*1.5;
}
}
}
public class commisionemployee extends Employee{
private double
grosssales;
private double
comissionrate;
public
commisionemployee(String Firstname,String Lastname,String Cnic,double
grosssales, double comissionrate) {
super(Firstname,Lastname,Cnic);
this.grosssales = grosssales;
this.comissionrate = comissionrate;
}
public
commisionemployee() {
}
public double
getGrosssales() {
return
grosssales;
}
public void
setGrosssales(double grosssales) {
this.grosssales = grosssales;
}
public double
getComissionrate() {
return
comissionrate;
}
public void
setComissionrate(double comissionrate) {
this.comissionrate = comissionrate;
}
@Override
public String
toString() {
super.toString();
return
"Commision Employee:";
}
@Override
public double earning()
{
return
grosssales*comissionrate;
}
}
public class commisionbase extends commisionemployee
{
private double
baseSalary;
public
commisionbase() {
}
public
commisionbase(String Firstname,String Lastname,String Cnic,double grosssales,
double comissionrate,double baseSalary) {
super(Firstname,Lastname,Cnic,grosssales,comissionrate);
this.baseSalary = baseSalary;
}
public double
getBaseSalary() {
return
baseSalary;
}
public void
setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
@Override
public double
earning() {
return getBaseSalary()+ super.earning(); //To change
body of generated methods, choose Tools | Templates.
}
@Override
public String
toString() {
return
"Baseplus commision: "+getBaseSalary() +super.toString(); //To change
body of generated methods, choose Tools | Templates.
}
}
public class test {
public static void
main(String[] args) {
Employee
firstEmployee = new SalariedEmployeeclass("Usman"
,"Ali","111-11-1111", 800.00 );
Employee secondEmployee = new
commisionemployee("Atif" ,"Aslam","222-22-2222",
10000, 0.06 );
Employee thirdEmployee = new
commisionbase("Rana","Naseeb", "333-33-3333",
5000 , 0.04 , 300 );
Employee fourthEmployee = new hourlyemployee(
"Renson" , "Isaac","444-44-4444" , 16.75 , 40 );
System.out.println(firstEmployee);
System.out.println(firstEmployee.earning());
System.out.println(secondEmployee);
System.out.println(secondEmployee.earning());
System.out.println(thirdEmployee);
// performing downcasting to access & raise base salary
commisionbase currentEmployee =(commisionbase)
thirdEmployee;
double oldBaseSalary = currentEmployee.getBaseSalary();
System.out.println( "old base salary: " +
oldBaseSalary) ;
currentEmployee.setBaseSalary(1.10 * oldBaseSalary);
System.out.println("new base salary with 10% increase
is:"+
currentEmployee.getBaseSalary());
System.out.println(thirdEmployee.earning() );
System.out.println(fourthEmployee);
System.out.println(fourthEmployee.earning() );
}
}
SOURCE
CODE:
public class Shape {
private point p;
Shape(point p)
{
this.p=p;
}
public point
getcentre()
{
return p;
}
public boolean contain(point p)
{
boolean
temp=true;
return temp;
}
}
public class ShapeArray {
private Shape
[]ShapeList;
private int nbShape;
public
ShapeArray(int size) {
this.ShapeList=new
Shape[size];
nbShape=0;
}
public boolean
addShape(Shape s){
boolean
temp=false;
if(nbShape<ShapeList.length){
ShapeList[nbShape]=s;
nbShape++;
temp=true;
}
return temp;
}
public int
CircleCounter(){
int count=0;
for(int
i=0;i<ShapeList.length;i++){
if(ShapeList[i]
instanceof circle){
count++;
}
}
return count;
}
public double
AvgeAreas(){
double result=0;
int count=0;
for(int
i=0;i<ShapeList.length;i++){
if(ShapeList[i]
instanceof circle){
circle
c=(circle) ShapeList[i];
result+=(3.14*(c.getRadius()*c.getRadius()));
count++;
}
else
if(ShapeList[i] instanceof rectangle){
rectangle r
=(rectangle) ShapeList[i];
result+=(r.getLenght()*r.getWidth());
count++;
}
}
return
result/count;
}
public void
RemoveRect(){
for(int
i=0;i<ShapeList.length;i++){
if(ShapeList[i]
instanceof rectangle){
ShapeList[i]=null;
}
}
}
public void
DisplayRect(){
for(int
i=0;i<ShapeList.length;i++){
if(ShapeList[i]
instanceof rectangle){
rectangle r
=(rectangle) ShapeList[i];
System.out.println("Length Of Rectangle: "+r.getLenght());
System.out.println("Weight Of Rectangle: "+r.getWidth());
System.out.println("Area Of Rectangle:
"+(r.getWidth()*r.getLenght()));
}
}
}
}
public class circle extends Shape{
private int
radius;
public circle(int
radius,point p) {
super(p);
this.radius=radius;
}
public int
getRadius() {
return radius;
}
public String
toString()
{
return
"Radius: "+getRadius()+"
" +super.toString();
}
}
public class rectangle extends Shape {
private int
lenght;
private int width;
public
rectangle(point p,int lenght,int width) {
super(p);
this.lenght=lenght;
this.width=width;
}
public void
setLenght(int lenght) {
this.lenght =
lenght;
}
public void
setWidth(int width) {
this.width =
width;
}
public int
getLenght() {
return lenght;
}
public int
getWidth() {
return width;
}
@Override
public String
toString() {
super.toString();
return
"rectangle" + "lenght=" + getLenght() + ",
width=" + getWidth() ;
}
}
public class point {
private int x;
private int y;
public point(int
x, int y) {
this.x = x;
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public double
todistance(point p)
{ double dist= (p.x*p.x)+(p.y*p.y);
return
Math.sqrt(dist);
}
@Override
public String
toString() {
return
"point{" + "x=" + x + ", y=" + y + '}';
} }





Comments
Post a Comment