0% found this document useful (0 votes)
31 views9 pages

Oop 2

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)
31 views9 pages

Oop 2

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/ 9

2024

HITEC
UNIVERSITY
TAXILA

OBJECT ORIENTED
PROGRAMMING

[ ASSIGNMENT NO ]# 2

SUBMITTED TO:

SIR MUHAMMAD KHALID

SUBMITTED BY:

MUHAMMAD FARHAN

23-SE-038

“A”
EXERCISE QUESTION ANSWER:

9.3:
Inheritance) Many programs written with inheritance could be written with composition
instead, and vice versa. Rewrite class BasePlusCommissionEmployee (Fig. 9.11) of the
CommissionEmployee–BasePlusCommissionEmployee hierarchy to use composition rather than
inheritance

Answer:
Explanation:

In the code given below, class BasePlusCommissionEmployee is declared in such a way that it
shows a has-a relationship with the CommissionEmployee class. This can be observed by
looking at the private instance variables of the BasePlusCommissionEmployee class. It has a
private CommissionEmployee variable E.

It is important to note that since this is a has-a relationship, all the getter and setter methods of
the CommissionEmployee class need to be re-written in some sense.

Code:
public class BasePlusCommissionEmployee {

private CommissionEmployee E; private double baseSalary;

public BasePlusCommissionEmployee (String firstName, String lastName, String socialSecurityNumber,


double grossSales, double commissionRate, double baseSalary)

{ try

E = new CommissionEmployee(firstName, lastName, socialSecurityNumber, grossSales,


commissionRate)

catch(IllegalArgumentException e)

System.out.printf("Illegal Argument Error: %s", e.getMessage());

}
if(baseSalary < 0.0)

throw new IllegalArgumentException("baseSalary must be >= 0.0");

this.baseSalary = baseSalary;

public void setBaseSalary()

if(baseSalary < 0.0)

throw new IllegalArgumentException("baseSalary must be >= 0.0");

this.baseSalary = baseSalary;

public double getBaseSalary()

return baseSalary;

public String getFirstName()

return E.getFirstName();

public String getLastName()

return E.getLastName();

public String getSocialSecurityNumber()

return E.getSocialSecurityNumber();
}

public void setGrossSales(double grossSales)

E.setGrossSales(grossSales);

public double getGrossSales()

return E.getGrossSales();

public void setCommissionRate()

E.setCommissionRate();

public double getCommissionRate()

return E.getCommissionRate();

public double earnings()

return getBaseSalary() + E.earnings();

public String toString()

return String.format("%s %s%n%s: %.2f", "base-salaried", E.toString(), "base salary",


getBaseSalary());

}}
Explanation:

 The class `BasePlusCommissionEmployee` has private instance variables `E` of type


`CommissionEmployee` and `baseSalary` of type `double`.
 The constructor `BasePlusCommissionEmployee` initializes the `E` instance variable with
a new `CommissionEmployee` object and initializes the `baseSalary` variable with the
provided value. It also performs validation to ensure that the `baseSalary` is not
negative.
 The method `setBaseSalary` sets the `baseSalary` instance variable after validating that
the provided value is not negative.
 The method `getBaseSalary` returns the value of the `baseSalary` instance variable.
 Methods `getFirstName`, `getLastName`, and `getSocialSecurityNumber` delegate to
corresponding methods of the `CommissionEmployee` object `E`.
 Methods `setGrossSales` and `getGrossSales` delegate to corresponding methods of the
`CommissionEmployee` object `E`.
 Methods `setCommissionRate` and `getCommissionRate` delegate to corresponding
methods of the `CommissionEmployee` object `E`.
 The method `earnings` calculates the total earnings of the
BasePlusCommissionEmployee` by adding the base salary to the earnings calculated by
the `CommissionEmployee` object `E`.
 The `toString` method returns a string representation of the
`BasePlusCommissionEmployee` object, including the details of the
`CommissionEmployee` object `E` and the base salary.

9.8:
Hierarchy) Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram,
Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a
Point class to represent the points in each shape. Make the hierarchy as deep (i.e., as many
levels) as possible. Specify the instance variables and methods for each class. The private
instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of
the Quadrilateral. Write a program that instantiates objects of your classes and outputs each
object’s area (except Quadrilateral).

Answer:

Code:
Step 1:
// Point.java
public class Point {
private double x;
private double y;
public Point(){
this.x = 0;
this.y = 0;
}
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double distance(Point p){
return Math.sqrt(
(this.x - p.x)*(this.x - p.x) + (this.y - p.y)*(this.y - p.y)
);
}
public double slope(Point p){
return ((this.y - p.y))/(this.x - p.x);
}
public double horizontalDistance(Point p){
return Math.abs(this.x - p.x);
}
public double verticalDistance(Point p){
return Math.abs(this.y - p.y);
}
}

Step 2:
// Quadrilateral.java
public class Quadrilateral {
private Point pointA;
private Point pointB;
private Point pointC;
private Point pointD;
public Quadrilateral(){
this.pointA = new Point(0, 0);
this.pointB = new Point(0, 0);
this.pointC = new Point(0, 0);
this.pointD = new Point(0, 0);
}
public Quadrilateral(Point pointA, Point pointB,
Point pointC, Point pointD) {
this.pointA = pointA;
this.pointB = pointB;
this.pointC = pointC;
this.pointD = pointD;
}
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = pointA;
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
}

Step 3:
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = pointC;
}
public Point getPointD() {
return pointD;
}
public void setPointD(Point pointD) {
this.pointD = pointD;
}
public double lengthAB(){
return pointA.distance(pointB);
}
public double lengthBC(){
return pointB.distance(pointC);
}
public double lengthCD(){
return pointC.distance(pointD);
}
public double lengthDA(){
return pointD.distance(pointA);
}
public double angleD(){
return Math.acos(
((pointA.horizontalDistance(pointD)*pointD.horizontalDistance(pointC))
+
(pointA.verticalDistance(pointD)*pointD.verticalDistance(pointC)))/
Math.abs(lengthDA()*lengthCD())
);
}

Step 4:
// Trapezoid.java
public class Trapezoid extends Quadrilateral {

public Trapezoid(){
super(
new Point(1, 3),
new Point(3, 3),
new Point(4, 0),
new Point(0, 0)
);
}

public double area(){


return 0.5*(lengthAB()+lengthCD())*lengthDA()*Math.sin(angleD());
}
}

Step 5:
// Parallelogram.java
public class Parallelogram extends Quadrilateral{

public Parallelogram(){
super(
new Point(1, 3),
new Point(3, 3),
new Point(2, 0),
new Point(0, 0)
);
}

public double area(){


return lengthDA()*lengthCD()*Math.sin(angleD());
}
}

Step 6:
// Rectangle.java
public class Rectangle extends Parallelogram {

public Rectangle(){
super();
setPointA(new Point(0, 3));
setPointB(new Point(2, 3));
setPointC(new Point(2, 0));
setPointD(new Point(0, 0));
}

@Override
public double area() {
return lengthAB()*lengthBC();
}
}

Step 7:
// Square.java
public class Square extends Rectangle {

public Square(){
super();
setPointA(new Point(0, 4));
setPointB(new Point(4, 4));
setPointC(new Point(4, 0));
setPointD(new Point(0, 0));
}

@Override
public double area(){
return lengthAB()*lengthAB();
}
}

Step 8:
// QuadrilateralTest.java

public class QuadrilateralTest {


public static void main(String[] args) {
System.out.printf("Creating a Rectangle...\n\n");
Rectangle rec = new Rectangle();
System.out.printf("Area of rectangle: %.2f\n\n", rec.area());

System.out.printf("Creating a Square...\n\n");
Square sq = new Square();
System.out.printf("Area of Square: %.2f\n\n", sq.area());

System.out.printf("Creating a Parallelogram...\n\n");
Parallelogram pr = new Parallelogram();
System.out.printf("Area of parallelogram: %.2f\n\n", pr.area());

System.out.printf("Creating a Trapezoid...\n\n");
Trapezoid tr = new Trapezoid();
System.out.printf("Area of trapezoid: %.2f\n", tr.area());
}
}

Step 9:
$ javac QuadrilateralTest.java
$ java QuadrilateralTest
Creating a Rectangle...

Area of rectangle: 6.00

Creating a Square...

Area of Square: 16.00

Creating a Parallelogram...

Area of parallelogram: 6.00

Creating a Trapezoid...

Area of trapezoid: 9.00

---THE END---

You might also like