编java代码求椭圆和长方形的面积和周长.要求:写两个class:ellipse 和 rectangle,含有以下的state variables:a location point(x,y),a height h and a width w.每个class里面的要求:1.至少一个constructor method 2.一个方法函数去计算给定图形(长方形和椭圆)的周长C 长方形C=2(h+w) 椭
网友回答
没明白isLargeThan是什么意思,能说得详细点儿么?
先把满足前四个条件的程序发给你,你看看行不行.
注:一个类一个java文件,运行Test3类执行.
public class Point {
\x05private double x;
\x05private double y;
\x05public Point() {
\x05\x05x=0;
\x05\x05y=0;
\x05}\x05public Point(double x,double y){
\x05\x05this.x=x;
\x05\x05this.y=y;
\x05}\x05public double getX(){
\x05\x05return this.x;
\x05}\x05public double getY(){
\x05\x05return this.y;
\x05}\x05public void setX(double x){
\x05\x05this.x=x;
\x05}\x05public void setY(double y){
\x05\x05this.y=y;
\x05}\x05public Point translate(double u,double v){
\x05\x05this.x=this.x+u;
\x05\x05this.y=this.y+v;
\x05\x05return new Point (this.x,this.y);
\x05}}public class Rectangle extends Point {
private double height;
private double wideth;
\x05public Rectangle() {
\x05\x05super();
\x05}\x05public Rectangle(Point p,double h,double w){
\x05\x05super(p.getX(),p.getY());
\x05\x05this.height=h;
\x05\x05this.wideth=w;
\x05}public double getPerimeter(){\x05return 2*(height+wideth);}public double getArea(){\x05return height*wideth;}}public class Ellipse extends Point{\x05 private double height;\x05 private double wideth;\x05\x05public Ellipse() {\x05\x05\x05super();\x05\x05}\x05\x05public Ellipse(Point p,double h,double w){\x05\x05\x05super(p.getX(),p.getY());\x05\x05\x05this.height=h;\x05\x05\x05this.wideth=w;\x05\x05}\x05 public double getPerimeter(){\x05 \x05return 2*3.14*Math.sqrt((height*height+wideth*wideth)/2);\x05 }\x05 public double getArea(){\x05 \x05return 3.14*height*wideth;\x05 }}public class Test3 {\x05public static void main(String[] args) {\x05\x05Point p=new Point(1.2,4.6);\x05\x05Rectangle r=new Rectangle(p,9.2,8.7);