Skip to content

Commit ee8fb67

Browse files
authored
Add files via upload
1 parent 7d9195d commit ee8fb67

File tree

11 files changed

+146
-0
lines changed

11 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Circle extends Shape {
3+
4+
private double radius;
5+
6+
public Circle(Point center, double radius) {
7+
this.radius = radius;
8+
this.position = center;
9+
}
10+
11+
@Override
12+
public double computeArea() {
13+
return (Math.PI * Math.pow(radius, 2));
14+
}
15+
16+
@Override
17+
public double computePerimeter()
18+
{
19+
return (2*Math.PI*radius);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
public class Point {
3+
4+
private double x;
5+
private double y;
6+
7+
public Point(double x, double y) {
8+
this.x = x;
9+
this.y = y;
10+
}
11+
12+
public double getX() {
13+
return x;
14+
}
15+
16+
public double getY() {
17+
return y;
18+
}
19+
20+
public void setX(double x) {
21+
this.x = x;
22+
}
23+
24+
public void setY(double y) {
25+
this.y = y;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class Rectangle extends Shape {
3+
4+
private double length, height;
5+
6+
Rectangle(Point upperLeft, double length, double height) {
7+
this.position = upperLeft;
8+
this.length = length;
9+
this.height = height;
10+
}
11+
12+
@Override
13+
public double computeArea() {
14+
return (length * height);
15+
}
16+
@Override
17+
public double computePerimeter()
18+
{
19+
return (2* (length + height));
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
public abstract class Shape {
3+
4+
protected Point position;
5+
6+
abstract double computeArea();
7+
8+
abstract double computePerimeter();
9+
10+
public Point getPosition() {
11+
return this.position;
12+
}
13+
14+
public void setPosition(Point position) {
15+
this.position = position;
16+
}
17+
18+
public void movePositionRelative(Point position) {
19+
double x = this.position.getX() + position.getX();
20+
double y = this.position.getY() + position.getY();
21+
22+
this.position.setX(x);
23+
this.position.setY(y);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
public class TestShapes {
3+
public static void main(String[] args) {
4+
Circle circle1 = new Circle(new Point(1.0, 1.0), 1.0);
5+
Circle circle2 = new Circle(new Point(1.0, 1.0), 2.0);
6+
7+
Shape rectangle = new Rectangle(new Point(0.0, 1.0), 1.0, 1.0);
8+
9+
// Print areas
10+
System.out.println("Area of circle 1 is: " + circle1.computeArea());
11+
System.out.println("Area of circle 2 is: " + circle2.computeArea());
12+
System.out.println("Area of rectangle is: " + rectangle.computeArea());
13+
System.out.println();
14+
15+
// Print positions
16+
System.out.println("Circle 1 is at: (" + circle1.getPosition().getX() +
17+
", " + circle1.getPosition().getY() + ")");
18+
19+
System.out.println("Rectangle is at: (" + rectangle.getPosition().getX() +
20+
", " + rectangle.getPosition().getY() + ")");
21+
System.out.println();
22+
23+
// Move shapes
24+
circle1.setPosition(new Point(3.0, 1.0));
25+
rectangle.movePositionRelative(new Point(1.0, 1.0));
26+
27+
// Print positions
28+
System.out.println("Circle 1 is at: (" + circle1.getPosition().getX() +
29+
", " + circle1.getPosition().getY() + ")");
30+
31+
System.out.println("Rectangle is at: (" + rectangle.getPosition().getX() +
32+
", " + rectangle.getPosition().getY() + ")");
33+
System.out.println();
34+
35+
//print perimetre
36+
System.out.println("Perimetre of circle 1 is: " + circle1.computePerimeter());
37+
System.out.println("Perimetre of circle 2 is: " + circle2.computePerimeter());
38+
System.out.println("Perimetre of rectangle is: " + rectangle.computePerimeter());
39+
System.out.println();
40+
}
41+
}

0 commit comments

Comments
 (0)