Created
April 23, 2015 13:52
-
-
Save Sergaav/b9158bbda137e7098108 to your computer and use it in GitHub Desktop.
Наследование фигуры
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foursquare extends Shape { | |
private Point A; | |
private Point B; | |
private Point C; | |
private Point D; | |
public Foursquare(Point a, Point b, Point c, Point d) { | |
super(); | |
A = a; | |
B = b; | |
C = c; | |
D = d; | |
} | |
public Point getA() { | |
return A; | |
} | |
public void setA(Point a) { | |
A = a; | |
} | |
public Point getB() { | |
return B; | |
} | |
public void setB(Point b) { | |
B = b; | |
} | |
public Point getC() { | |
return C; | |
} | |
public void setC(Point c) { | |
C = c; | |
} | |
public Point getD() { | |
return D; | |
} | |
public void setD(Point d) { | |
D = d; | |
} | |
@Override | |
double getPerimetr() { | |
return A.getDistance(B)+B.getDistance(C)+C.getDistance(D)+D.getDistance(A); | |
} | |
@Override | |
double getArea() { | |
return A.getDistance(B)*B.getDistance(C); | |
} | |
@Override | |
String getInfo() { | |
return "Foursquare: Area = "+this.getArea()+" Perimetr = " + this.getPerimetr(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
Point A=new Point(0,0); | |
Point B=new Point(0,4); | |
Point C=new Point(3,0); | |
Point D=new Point(0,5); | |
Point E=new Point(5,3); | |
Triangular tr=new Triangular(A, B, C); | |
calc(tr); | |
Foursquare fs=new Foursquare(A, B, C, D); | |
calc(fs); | |
Pentagon pt = new Pentagon(A, B, C, D, E); | |
calc(pt); | |
} | |
static void calc (Shape a){ | |
System.out.println(a.getInfo()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pentagon extends Shape { | |
private Point A; | |
private Point B; | |
private Point C; | |
private Point D; | |
private Point E; | |
public Point getA() { | |
return A; | |
} | |
public void setA(Point a) { | |
A = a; | |
} | |
public Point getB() { | |
return B; | |
} | |
public void setB(Point b) { | |
B = b; | |
} | |
public Point getC() { | |
return C; | |
} | |
public void setC(Point c) { | |
C = c; | |
} | |
public Point getD() { | |
return D; | |
} | |
public void setD(Point d) { | |
D = d; | |
} | |
public Point getE() { | |
return E; | |
} | |
public void setE(Point e) { | |
E = e; | |
} | |
public Pentagon(Point a, Point b, Point c, Point d, Point e) { | |
super(); | |
A = a; | |
B = b; | |
C = c; | |
D = d; | |
E = e; | |
} | |
@Override | |
double getPerimetr() { | |
return A.getDistance(B) + B.getDistance(C) + C.getDistance(D) | |
+ D.getDistance(E) + E.getDistance(A); | |
} | |
@Override | |
double getArea() { | |
Triangular tr1 = new Triangular(A,B,C); | |
double p1 = tr1.getPerimetr() / 2.0 ; | |
Triangular tr2 = new Triangular(A,C,E); | |
double p2 = tr2.getPerimetr() / 2.0 ; | |
Triangular tr3 = new Triangular(C,E,D); | |
double p3 = tr3.getPerimetr() / 2.0 ; | |
double s1 = Math.sqrt(p1 * (p1 - A.getDistance(B)) * (p1 - B.getDistance(C)) * (p1 - C.getDistance(A))); | |
double s2 = Math.sqrt(p2 * (p2 - A.getDistance(C)) * (p2 - C.getDistance(E)) * (p2 - E.getDistance(A))); | |
double s3 = Math.sqrt(p3 * (p3 - C.getDistance(D)) * (p3 - D.getDistance(E)) * (p3 - E.getDistance(C))); | |
return s1+s2+s3; | |
} | |
@Override | |
String getInfo() { | |
return "Pentagon : Area = "+ this.getArea()+ " Perimetr = " + this.getPerimetr(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Point { | |
private double x; | |
private double 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 Point(double x, double y) { | |
super(); | |
this.x = x; | |
this.y = y; | |
} | |
public double getDistance(Point a) { | |
double x=(this.x-a.getX())*(this.x-a.getX()); | |
double y=(this.y-a.getY())*(this.y-a.getY()); | |
return Math.sqrt(x+y); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Shape { | |
abstract double getPerimetr(); | |
abstract double getArea(); | |
abstract String getInfo(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Triangular extends Shape { | |
public Triangular(Point a, Point b, Point c) { | |
super(); | |
A = a; | |
B = b; | |
C = c; | |
} | |
private Point A; | |
private Point B; | |
private Point C; | |
public Point getA() { | |
return A; | |
} | |
public void setA(Point a) { | |
A = a; | |
} | |
public Point getB() { | |
return B; | |
} | |
public void setB(Point b) { | |
B = b; | |
} | |
public Point getC() { | |
return C; | |
} | |
public void setC(Point c) { | |
C = c; | |
} | |
@Override | |
double getPerimetr() { | |
return A.getDistance(B) + B.getDistance(C) + C.getDistance(A); | |
} | |
@Override | |
double getArea() { | |
double a = A.getDistance(B); | |
double b = B.getDistance(C); | |
double c = C.getDistance(A); | |
double p = this.getPerimetr() / 2.0; | |
return Math.sqrt(p * (p - a) * (p - b) * (p - c)); | |
} | |
@Override | |
String getInfo() { | |
return "Triangular : Area = " + this.getArea() + " Perimetr = " | |
+ this.getPerimetr(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment