Java编程问题。有兴趣的人帮忙写一下。
(1)定义一个抽象类Shape,包含求面积的抽象方法Area(),利用继承产生子类三角形类Trangle类,圆Circle类,矩形Rectangle类。并分别实现计算面积...
(1)定义一个抽象类Shape,包含求面积的抽象方法Area(),利用继承产生子类三角形类Trangle类,圆Circle类,矩形Rectangle类。并分别实现计算面积的方法计算相应图形的面积。对于Trangle类要求能够实现修改三边,判断三边是否能够构成三角形,根据三边长计算面积的方法。
(2)对于上题目中的Shape类,改用接口来实现同样的功能。
直接用接口写一下吧。分儿不多了。 展开
(2)对于上题目中的Shape类,改用接口来实现同样的功能。
直接用接口写一下吧。分儿不多了。 展开
1个回答
展开全部
public abstract class Shape {
/**
* 计算图形面积
*
* @return
*/
public abstract double area();
}
public class Circle extends Shape {
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle() {
super();
// TODO Auto-generated constructor stub
}
public double radius;
@Override
public double area() {
// TODO Auto-generated method stub
return Math.PI * radius * radius;
}
}
public class Rectangle extends Shape {
public double sideA;
public double sideB;
public Rectangle() {
super();
// TODO Auto-generated constructor stub
}
public Rectangle(double sideA, double sideB) {
super();
this.sideA = sideA;
this.sideB = sideB;
}
@Override
public double area() {
// TODO Auto-generated method stub
return sideA * sideB;
}
}
public class Trangle extends Shape {
public Trangle() {
super();
// TODO Auto-generated constructor stub
}
public Trangle(double sideA, double sideB, double sideC) {
super();
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public double sideA;
public double sideB;
public double sideC;
@Override
public double area() {
// TODO Auto-generated method stub
double p = (sideA + sideB + sideC) / 2;
return Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
}
}
public class TestShape {
public static void display(Shape shape) {
System.out.println(shape.getClass().getSimpleName() + " area: " + shape.area());
}
public static void main(String[] args){
Circle cc = new Circle(3);
display(cc);
Rectangle rl = new Rectangle(3,4);
display(rl);
Trangle tl = new Trangle(3,4,5);
display(tl);
}
}
//接口实现
public interface Shape {
public double area();
}
public class Circle implements Shape {
public Circle(double radius) {
super();
this.radius = radius;
}
public Circle() {
super();
// TODO Auto-generated constructor stub
}
public double radius;
@Override
public double area() {
// TODO Auto-generated method stub
return Math.PI * radius * radius;
}
}
public class Trangle implements Shape {
public Trangle() {
super();
// TODO Auto-generated constructor stub
}
public Trangle(double sideA, double sideB, double sideC) {
super();
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public double sideA;
public double sideB;
public double sideC;
@Override
public double area() {
// TODO Auto-generated method stub
double p = (sideA + sideB + sideC) / 2;
return Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
}
}
public class Rectangle implements Shape {
public double sideA;
public double sideB;
public Rectangle() {
super();
// TODO Auto-generated constructor stub
}
public Rectangle(double sideA, double sideB) {
super();
this.sideA = sideA;
this.sideB = sideB;
}
@Override
public double area() {
// TODO Auto-generated method stub
return sideA * sideB;
}
}
public class TestShape {
public static void display(Shape shape) {
System.out.println(shape.getClass().getSimpleName() + " area: " + shape.area());
}
public static void main(String[] args){
Circle cc = new Circle(3);
display(cc);
Rectangle rl = new Rectangle(3,4);
display(rl);
Trangle tl = new Trangle(3,4,5);
display(tl);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |