java编写图形抽象类(Shape) 5
编写图形抽象类(Shape),其中有存储周长(c)、面积(s)的成员变量,还有计算周长(girth())、面积(area())的抽象方法,在此基础上,声明圆类(Circl...
编写图形抽象类(Shape),其中有存储周长(c)、面积(s)的成员变量,还有计算周长(girth( ))、面积(area( ))的抽象方法,在此基础上,声明圆类(Circle)、长方形类(Rectangle)和三角形类(Triangle)继承图形类,实现抽象方法,并增加自己的成员变量、成员方法,最后声明测试类进行测试。
展开
4个回答
展开全部
我来写一下吧:
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB < lengthC) || (lengthA + lengthC < lengthB) || (lengthB+lengthC < lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB < lengthC) || (lengthA + lengthC < lengthB) || (lengthB+lengthC < lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}
2019-06-26
展开全部
这个好简单,百度一下,一大堆,就是一个abstract,然后2个扩展,收工
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我可以帮你写这个,私聊我吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第63回 寿怡红群芳开夜宴 死金丹独艳理亲丧 第64回 幽淑女悲题五美吟 浪荡子情遗九龙佩
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询