用java编写一个图形类,该类具有长和高属性,具有求面积的方法
发布网友
发布时间:2022-05-20 20:19
我来回答
共3个回答
热心网友
时间:2023-11-22 13:09
//长方形
class Retangle extends Graph {
@Override
public int square() {
return getHeight() * getWidth();
}
}
//三角
//这里默认长度是底的长度
class Triangle extends Graph {
@Override
public int square() {
return (getHeight() * getWidth()) /2;
}
}
//图形
abstract class Graph {
private int height;
private int width;
public abstract int square();
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
public class Test {
public static void main(String[] args){
Graph g = new Retangle();
g.setHeight(2);
g.setWidth(3);
System.out.println("长方形的面积是:" + g.square());
Graph g1 = new Triangle();
g1.setHeight(2);
g1.setWidth(3);
System.out.println("三角形的面积是:" + g1.square());
}
}
追问
追答你的JAVA版本问题,你把@voerride删除了吧
热心网友
时间:2023-11-22 13:10
package .shape;
abstract class Shape {//定义抽象父类Shape
public double width;
public double height;
abstract double getArea(); //定义求解面积的方法
}
package .shape;
public class Rect extends Shape {
@Override
double getArea() {
return width*height;
}
}
package .shape;
public class Trangle extends Shape {
double sideA;
double sideB;
double sideC;
boolean isTrangel;
public Trangle(double a,double b,double c)
{
sideA=a;sideB=b;sideC=c;
if(a+b>c&&a+c>b&&b+c>a)
{
System.out.println("我是一个三角形");
isTrangel = true;
}
else
{
System.out.println("我不是一个三角形");
isTrangel = false;
}
}
@Override
double getArea() {
double area = 0d;
if(isTrangel)
{
double p=(sideA+sideB+sideC)/2.0;
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
System.out.println("三角形面积是:"+area);
}
else
{
System.out.println("不是一个三角形,不能计算面积");
}
return area;
}
}
package .shape;
public class TestArea {
public static void main(String[] args) {
Shape rectangle = new Rect();
rectangle.height=1.1;
rectangle.width=5.0;
System.out.println("矩形的面积是:"+rectangle.getArea());
//三角形
Shape tran = new Trangle(2.3,4.5,5.6);
System.out.println("三角形的面积是:"+tran.getArea());
}
}
运行结果:
矩形的面积是:5.5
我是一个三角形
三角形面积是:4.966246067202064
三角形的面积是:4.966246067202064
热心网友
时间:2023-11-22 13:10
/**
* 定义一个图形类
*
*/
public class Graph {
//图形的长高属性
public float length;
public float high;
public Graph(float length,float high) {
this.high = high;
this.length = length;
}
public float getArea(){
return 0;
}
}
/**
* 矩形的类
*
*/
public class Rectangle extends Graph{
public Rectangle(float length, float high) {
super(length, high);
}
public float getArea(){
float area = this.high * this.length;
return area;
}
}
/**
* 三角形的类
*
*/
public class Triangle extends Graph{
public Triangle(float length, float high) {
super(length, high);
}
public float getArea(){
float area = (float) (this.high * this.length * 0.5);
return area;
}
}/**
* 主测试类
*
*/
public class MainTest {
public static void main(String[] args) {
Graph rectangle = new Rectangle(10, 5);
Graph triangle = new Triangle(10, 5);
System.out.println("矩形的面积是:" + rectangle.getArea());
System.out.println("三角形的面积是:" + triangle.getArea());
}
}