Skip to content

Commit 49cca1f

Browse files
author
Sarah Akinkunmi
committed
9.8
1 parent df13387 commit 49cca1f

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ChapterNine.QuadrilateralInheritanceHierarchy;
2+
3+
public class Parallelogram extends Quadrilateral{
4+
private int height;
5+
6+
public Parallelogram(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint, int height) {
7+
super(firstPoint, secondPoint, thirdPoint, fourthPoint);
8+
this.height = height;
9+
}
10+
11+
public int getHeight() {
12+
return height;
13+
}
14+
15+
public void setHeight(int height) {
16+
if(height < 0.0) {
17+
throw new IllegalArgumentException("Enter a height greater than 0!");
18+
}
19+
this.height = height;
20+
}
21+
22+
public double getArea(){
23+
return (getFourthPoint().getXCoordinate() - getThirdPoint().getXCoordinate() * height);
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("%s%.4f", "The area of a rectangle is", getArea());
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ChapterNine.QuadrilateralInheritanceHierarchy;
2+
3+
public class Point {
4+
private int xCoordinate;
5+
private int yCoordinate;
6+
7+
public Point(int xCoordinate, int yCoordinate) {
8+
this.xCoordinate = xCoordinate;
9+
this.yCoordinate = yCoordinate;
10+
}
11+
12+
public int getXCoordinate() {
13+
return xCoordinate;
14+
}
15+
16+
public void setXCoordinate(int xCoordinate) {
17+
this.xCoordinate = xCoordinate;
18+
}
19+
20+
public int getYCoordinate() {
21+
return yCoordinate;
22+
}
23+
24+
public void setYCoordinate(int yCoordinate) {
25+
this.yCoordinate = yCoordinate;
26+
}
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
9.8 (Quadrilateral Inheritance Hierarchy) Write an inheritance hierarchy for classes Quadrilateral,
3+
Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy.
4+
Create and use a Point class to represent the points in each shape. Make the hierarchy as deep (i.e., as many
5+
levels) as possible. Specify the instance variables and methods for each class. The private instance variables
6+
of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral.
7+
Write a program that instantiates objects of your classes and outputs each object’s area (except Quadrilateral).
8+
*/
9+
10+
package ChapterNine.QuadrilateralInheritanceHierarchy;
11+
12+
public class Quadrilateral {
13+
private Point firstPoint;
14+
private Point secondPoint;
15+
private Point thirdPoint;
16+
private Point fourthPoint;
17+
18+
public Quadrilateral() {}
19+
20+
public Quadrilateral(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint) {
21+
this.firstPoint = firstPoint;
22+
this.secondPoint = secondPoint;
23+
this.thirdPoint = thirdPoint;
24+
this.fourthPoint = fourthPoint;
25+
}
26+
27+
public Point getFirstPoint() {
28+
return firstPoint;
29+
}
30+
31+
public void setFirstPoint(Point firstPoint) {
32+
this.firstPoint = firstPoint;
33+
}
34+
35+
public Point getSecondPoint() {
36+
return secondPoint;
37+
}
38+
39+
public void setSecondPoint(Point secondPoint) {
40+
this.secondPoint = secondPoint;
41+
}
42+
43+
public Point getThirdPoint() {
44+
return thirdPoint;
45+
}
46+
47+
public void setThirdPoint(Point thirdPoint) {
48+
this.thirdPoint = thirdPoint;
49+
}
50+
51+
public Point getFourthPoint() {
52+
return fourthPoint;
53+
}
54+
55+
public void setFourthPoint(Point fourthPoint) {
56+
this.fourthPoint = fourthPoint;
57+
}
58+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ChapterNine.QuadrilateralInheritanceHierarchy;
2+
3+
public class Rectangle extends Quadrilateral{
4+
public Rectangle(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint) {
5+
super(firstPoint, secondPoint, thirdPoint, fourthPoint);
6+
}
7+
8+
public double getArea(){
9+
return (getSecondPoint().getXCoordinate() - getFirstPoint().getXCoordinate()) * (getThirdPoint().getXCoordinate() - getSecondPoint().getXCoordinate());
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return String.format("%s%.4f", "The area of a rectangle is", getArea());
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ChapterNine.QuadrilateralInheritanceHierarchy;
2+
3+
public class Square extends Quadrilateral{
4+
public Square() {}
5+
6+
public Square(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint) {
7+
super(firstPoint, secondPoint, thirdPoint, fourthPoint);
8+
}
9+
10+
public double getArea() {
11+
return Math.pow((getSecondPoint().getXCoordinate()) - (getFirstPoint().getXCoordinate()), 2);
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return String.format("%s%.4f", "The area of a square is", getArea());
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ChapterNine.QuadrilateralInheritanceHierarchy;
2+
3+
public class Trapezoid extends Quadrilateral{
4+
private int height;
5+
6+
public Trapezoid(Point firstPoint, Point secondPoint, Point thirdPoint, Point fourthPoint, int height) {
7+
super(firstPoint, secondPoint, thirdPoint, fourthPoint);
8+
this.height = height;
9+
}
10+
11+
public int getHeight() {
12+
return height;
13+
}
14+
15+
public void setHeight(int height) {
16+
if(height < 0.0) {
17+
throw new IllegalArgumentException("Enter a height greater than 0!");
18+
}
19+
this.height = height;
20+
}
21+
22+
public double getArea() {
23+
return (0.5 * ((getSecondPoint().getXCoordinate()) - (getFirstPoint().getXCoordinate())) + (getFourthPoint().getXCoordinate() - getThirdPoint().getXCoordinate())) * height;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("%s%.4f", "The area of a trapezoid is", getArea());
29+
}
30+
}

0 commit comments

Comments
 (0)