资源简介
基于构造函数,继承和多态的实现,包含梯形,平行四边形,矩形,正方形
代码片段和文件信息
package duotai1;
package duotai1;
public class Parallelogram extends Quadrilateral
{
private float Width;
private float Height;
private float Area;
public Parallelogram(float x1 float y1 float x2 float y2 float x3
float y3 float x4 float y4)
{
super(x1 y1 x2 y2 x3 y3 x4 y4);
System.out.print(“\nCoordinate of Parallelogram are:\n“);
System.out.print(“(“+x1+““+y1+“)(“+x2+““+y2+“)(“+x3+““+y3+“)(“+x4+““+y4+“)\n“);
Width=x2-x1;
Height=y4-y1;
Area=Height*Width;
}
public void show()
{
System.out.print(“Width is: “+Width+“\nHeight is: “+Height+“\nArea is: “+Area+“\n“);
}
}
package duotai1;
public class Quadrilateral
{
private float x1y1;
private float x2y2;
private float x3y3;
private float x4y4;
public Quadrilateral(float x1float y1float x2float y2
float x3float y3float x4float y4)
{
this.x1=x1;this.y1=y1;
this.x2=x2;this.y2=y2;
this.x3=x3;this.y3=y3;
this.x4=x4;this.y4=y4;
}
public void show()
{
System.out.print(“Coordinate of Quadrilateral are:\n“);
System.out.print(“(“+x1+““+y1+“)(“+x2+““+y2+“)(“+x3+““+y3+“)(“+x4+““+y4+“)\n“);
}
}
package duotai1;
public class Rectangle extends Quadrilateral
{
private float Width;
private float Height;
private float Area;
public Rectangle(float x1 float y1 float x2 float y2 float x3
float y3 float x4 float y4)
{
super(x1 y1 x2 y2 x3 y3 x4 y4);
System.out.print(“\nCoordinate of Rectangle are:\n“);
System.out.print(“(“+x1+““+y1+“)(“+x2+““+y2+“)(“+x3+““+y3+“)(“+x4+““+y4+“)\n“);
Width=x2-x1;
Height=y4-y1;
Area=Height*Width;
}
public void show()
{
System.out.print(“Width is: “+Width+“\nHeight is: “+Height+“\nArea is: “+Area+“\n“);
}
}
评论
共有 条评论