Java语言程序设计D实验——类与对象实验

发布于 2020-10-23  1473 次阅读


Java语言程序设计D实验——类与对象实验

一、实验内容描述(问题域描述)

【实验题目】类与对象
【实验目的】使用类来封装对象的属性和功能;掌握类变量与实例变量,以及类方法与实例方法的区别;掌握使用package和import语句。
【实验内容】编写一个Java应用程序,该程序中有3个类:Trangle、Leder和Circle,分别用来刻画“三角形”、“梯形”和“圆形”。至少要实现如下基本要求:
a) Trangle类具有类型为double的三个边,以及周长、面积属性,Trangle类具有返回周长、面积以及修改三个边的功能。另外,Trangle类还具有一个boolean型的属性,该属性用来判断三个属能否构成一个三角形。
b) Lader类具有类型double的上底、下底、高、面积属性,具有返回面积的功能。
c) Circle类具有类型为double的半径、周长和面积属性,具有返回周长、面积的功能。
d) 实现以上功能的必须同时利用Java的继承、成员变量的继承与隐藏、方法的继承与重写、import与package语句、类变量与实例变量、抽象类、接口、异常等语法元素。

二、类

package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :所有类的公共父类
 *2019年12月17日  下午5:14:34
 */
public abstract class Shape implements ShapeInterface{

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :图形的接口
 *2019年12月17日  下午5:18:42
 */
public interface ShapeInterface {
  /**
 * @return 返回图形的面积
 */
public double getS();
/**
 * @return 返回图形的周长
 */
public double getLen();

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :圆
 *2019年12月17日  下午5:17:14
 */
public class Circle  extends Shape{
 private double r;

    @SuppressWarnings("javadoc")
    public Circle(double r) {
    this.r = r;
}

    @Override
    public double getS() {
        return Math.PI*this.r*this.r;
    }

    @Override
    public double getLen() {
        return Math.PI*this.r*2;
    }

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :梯形
 *2019年12月17日  下午5:16:44
 */
public class Lader  extends Shape{
  //上底长
  private double topLen ;
  //下底长
  private double belowLen ;
  //高
  private double high;
@Override
public double getS() {

    return (this.topLen+this.belowLen)*this.high/2;
}
@SuppressWarnings("javadoc")
public Lader(double topLen, double belowLen, double high) {
    this.topLen = topLen;
    this.belowLen = belowLen;
    this.high = high;
}
@Override
public double getLen() {
    try {
        throw new LaderException("梯形不能计算周长!!");
    } catch (LaderException e) {
        e.printStackTrace();
    }
    return 0;
}

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :三角形类
 *2019年12月17日  下午5:16:18
 */
public class Trangle extends Shape{
  private double x;
  private double y;
  private double z;
    @SuppressWarnings("javadoc")
    public Trangle(double x, double y, double z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

    /**
     * @return
     */
    public double getX() {
        return x;
    }

    /**
     * @param x
     */
    public void setX(double x) {
        this.x = x;
    }

    /**
     * @return
     */
    public double getY() {
        return y;
    }

    /**
     * @param y
     */
    public void setY(double y) {
        this.y = y;
    }

    /**
     * @return
     */
    public double getZ() {
        return z;
    }

    /**
     * @param z
     */
    public void setZ(double z) {
        this.z = z;
    }

    @Override
    public double getS() {
        if(isTrangle()){
            double p = this.getLen()/2;
            return  Math.sqrt(p*(p-this.x)*(p-this.y)*(p-this.z));
        };
        try {
            throw new TrangleException("该三边不构成三角形!");
        } catch (TrangleException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public double getLen() {
        if (isTrangle()) {
            return this.x + this.y + this.z;
        } 
        try {
            throw new TrangleException("该三边不构成三角形!");
        } catch (TrangleException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
   /**
 * @return 返回是否能构成三角形
 */
public boolean isTrangle(){
    boolean fla1 = this.x+this.y >this.z;
    boolean fla2 = this.x+this.z >this.y;
    boolean fla3 = this.z+this.y >this.x;
    if(fla1 && fla2 &&fla3){
        return true;
    }else{
        return false;
    }

   }
}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :图形的异常公共父类
 *2020年10月23日  下午7:14:52
 */
public abstract class ShapeException extends Exception{

    private static final long serialVersionUID = 1L;
    @SuppressWarnings("javadoc")
    public ShapeException() {

    }
    @SuppressWarnings("javadoc")
    public ShapeException(String message) {
        super(message);

    }

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :圆的异常类
 *2020年10月23日  下午8:13:41
 */
public class CircleException  extends ShapeException{

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("javadoc")
    public CircleException(String message) {
        super(message);
    }

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :梯形异常类
 *2020年10月23日  下午8:20:58
 */
public class LaderException extends ShapeException {
    private static final long serialVersionUID = 1L;

    @SuppressWarnings("javadoc")
    public LaderException(String message) {
        super(message);
    }

}
package com.moyisuiying.three;

/**
 * @author 陌意随影
 TODO :
 *2020年10月23日  下午8:25:00
 */
public class TrangleException extends ShapeException {
    private static final long serialVersionUID = 1L;

    @SuppressWarnings("javadoc")
    public TrangleException() {
        // TODO Auto-generated constructor stub
    }

    @SuppressWarnings("javadoc")
    public TrangleException(String message) {
        super(message);
    }

}

三、源代码

源代码已经上传到个人服务器,如有需要请自行下载:http://moyisuiying.com/wp-content/uploads/2020/10/three.rar


繁华落尽,雪花漫天飞舞。