AssignmentS2
This commit is contained in:
parent
2c4a3a53b9
commit
c6f3123b73
BIN
assignments/AssigmentS2/exercise1/Point2D.class
Normal file
BIN
assignments/AssigmentS2/exercise1/Point2D.class
Normal file
Binary file not shown.
37
assignments/AssigmentS2/exercise1/Point2D.java
Normal file
37
assignments/AssigmentS2/exercise1/Point2D.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
public class Point2D {
|
||||||
|
|
||||||
|
private double x, y;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Point2D (double x, double y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double distance(double x, double y) {
|
||||||
|
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public double distance(Point2D p) {
|
||||||
|
return Math.sqrt(Math.pow(this.x - p.getX(), 2) + Math.pow(this.y - p.getY(), 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double distance(Point2D p1, Point2D p2) {
|
||||||
|
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(), 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Point2D midpoint(Point2D p) {
|
||||||
|
double midx = (this.x + p.getX()) / 2;
|
||||||
|
double midy = (this.y + p.getY()) / 2;
|
||||||
|
return new Point2D (midx, midy);
|
||||||
|
}
|
||||||
|
}
|
18
assignments/AssigmentS2/exercise1/demo.java
Normal file
18
assignments/AssigmentS2/exercise1/demo.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
public class demo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Point2D p1 = new Point2D(3.0, 4.0);
|
||||||
|
Point2D p2 = new Point2D(6.0, 8.0);
|
||||||
|
|
||||||
|
System.out.println("Coordinates of p1: (" + p1.getX() + ", " + p1.getY() + ")");
|
||||||
|
System.out.println("Coordinates of p2: (" + p2.getX() + ", " + p2.getY() + ")");
|
||||||
|
|
||||||
|
System.out.println("Distance between p1 and (0, 0): " + p1.distance(0, 0));
|
||||||
|
|
||||||
|
System.out.println("Distance between p1 and p2: " + p1.distance(p2));
|
||||||
|
|
||||||
|
Point2D midpoint = p1.midpoint(p2);
|
||||||
|
System.out.println("Midpoint between p1 and p2: (" + midpoint.getX() + ", " + midpoint.getY() + ")");
|
||||||
|
|
||||||
|
System.out.println("Distance between p1 and p2 :" + Point2D.distance(p1, p2));
|
||||||
|
}
|
||||||
|
}
|
18
assignments/AssigmentS2/exercise2-1/Circle.java
Normal file
18
assignments/AssigmentS2/exercise2-1/Circle.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
public class Circle implements Shape {
|
||||||
|
|
||||||
|
// private double n;
|
||||||
|
|
||||||
|
/* public Circle(double n) {
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public double calculateArea(double n) {
|
||||||
|
return 3.14*n*n;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculatePerimeter(double n) {
|
||||||
|
return 2*3.14*n;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
19
assignments/AssigmentS2/exercise2-1/Rectangle.java
Normal file
19
assignments/AssigmentS2/exercise2-1/Rectangle.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
public class Rectangle implements Shape {
|
||||||
|
|
||||||
|
// private double n;
|
||||||
|
|
||||||
|
/* public Circle(double n) {
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
public double calculateArea(double n, double m) {
|
||||||
|
return m*n;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculatePerimeter(double n, double m) {
|
||||||
|
return 2*m + 2*n;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
BIN
assignments/AssigmentS2/exercise2-1/Shape.class
Normal file
BIN
assignments/AssigmentS2/exercise2-1/Shape.class
Normal file
Binary file not shown.
10
assignments/AssigmentS2/exercise2-1/Shape.java
Normal file
10
assignments/AssigmentS2/exercise2-1/Shape.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
interface Shape {
|
||||||
|
|
||||||
|
public double calculateArea(double n);
|
||||||
|
public double calculatePerimeter(double n);
|
||||||
|
|
||||||
|
|
||||||
|
public double calculateArea(double n, double m);
|
||||||
|
public double calculatePerimeter(double n, double m);
|
||||||
|
|
||||||
|
}
|
12
assignments/AssigmentS2/exercise2-1/demo.java
Normal file
12
assignments/AssigmentS2/exercise2-1/demo.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
public class demo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape circle = new Circle(5.0);
|
||||||
|
Shape rectangle = new Rectangle(5.0, 6.0);
|
||||||
|
|
||||||
|
System.out.println("Rectangle one side 5, other side 6");
|
||||||
|
System.out.println("Rectangle area: "+ rectangle.calculateArea() + ", Rectangle perimeter: "+ rectangle.calculatePerimeter());
|
||||||
|
System.out.println("Circle radius: 5");
|
||||||
|
System.out.println("Circle area: "+ circle.calculateArea() + ", Circle perimeter: "+ circle.calculatePerimeter());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
assignments/AssigmentS2/exercise2/Circle.java
Normal file
18
assignments/AssigmentS2/exercise2/Circle.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
class Circle implements Shape {
|
||||||
|
|
||||||
|
private double n;
|
||||||
|
|
||||||
|
public Circle(double n) {
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculateArea() {
|
||||||
|
return 3*n*n;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculatePerimeter() {
|
||||||
|
return 2*3*n;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
assignments/AssigmentS2/exercise2/Rectangle.java
Normal file
17
assignments/AssigmentS2/exercise2/Rectangle.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
class Rectangle implements Shape {
|
||||||
|
|
||||||
|
private double n, m;
|
||||||
|
|
||||||
|
public Rectangle(double n, double m) {
|
||||||
|
this.n = n;
|
||||||
|
this.m = m;
|
||||||
|
}
|
||||||
|
public double calculateArea() {
|
||||||
|
return n*m;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculatePerimeter() {
|
||||||
|
return 2*n+2*m;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
assignments/AssigmentS2/exercise2/Shape.java
Normal file
6
assignments/AssigmentS2/exercise2/Shape.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
interface Shape {
|
||||||
|
|
||||||
|
double calculateArea();
|
||||||
|
double calculatePerimeter();
|
||||||
|
|
||||||
|
}
|
12
assignments/AssigmentS2/exercise2/demo.java
Normal file
12
assignments/AssigmentS2/exercise2/demo.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
public class demo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape circle = new Circle(5.0);
|
||||||
|
Shape rectangle = new Rectangle(5.0, 6.0);
|
||||||
|
|
||||||
|
System.out.println("Rectangle one side 5, other side 6");
|
||||||
|
System.out.println("Rectangle area: "+ rectangle.calculateArea() + ", Rectangle perimeter: "+ rectangle.calculatePerimeter());
|
||||||
|
System.out.println("Circle radius: 5");
|
||||||
|
System.out.println("Circle area: "+ circle.calculateArea() + ", Circle perimeter: "+ circle.calculatePerimeter());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user