Oop 2
Oop 2
HITEC
UNIVERSITY
TAXILA
OBJECT ORIENTED
PROGRAMMING
[ ASSIGNMENT NO ]# 2
SUBMITTED TO:
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 {
{ try
catch(IllegalArgumentException e)
}
if(baseSalary < 0.0)
this.baseSalary = baseSalary;
this.baseSalary = baseSalary;
return baseSalary;
return E.getFirstName();
return E.getLastName();
return E.getSocialSecurityNumber();
}
E.setGrossSales(grossSales);
return E.getGrossSales();
E.setCommissionRate();
return E.getCommissionRate();
}}
Explanation:
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)
);
}
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)
);
}
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
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...
Creating a Square...
Creating a Parallelogram...
Creating a Trapezoid...
---THE END---