【三角形类Triangle】设计一个名为Triangle的类来扩展GeometricObject类。该类包括:
目标输出任务:
测试1.无参构造函数构造的三角形:
测试2.有参构造函数构造的三角形:
测试1调用无参构造函数创建的对象,所有数据都是默认值
测试2调用了有参构造函数和set函数给对象一些属性赋值
根据题目要求来,难度不大
GeometricObeject类:
package 实验4;
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject(){
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
and filled value */
public GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date() ;
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor(){
return color;
}
/** Set a new color */
public void setColor(String color){
this.color = color;
}
/** Return filled. Since filled is boolean,
* its getter method is named isFilled */
public boolean isFilled(){
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled){
this.filled=filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated(){
return dateCreated;
}
/** Return a string representation of this object */
public String toString(){
return "created on " + dateCreated + "\ncolor: " + color +"and filled:"+ filled;
}
}
Triangle类:
package 实验4;
import java.lang.Math;
public class Triangle extends GeometricObject{
private double side1,side2,side3;
//无参构造函数
Triangle(){
side1 = 1.0;
side2 = 1.0;
side3 = 1.0;
}
//有参构造函数
Triangle(double side1,double side2,double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
//三个数据的访问器
public void setSide1(double side1){
this.side1 = side1;
}
public double getSide1(){
return side1;
}
public void setSide2(double side2){
this.side2 = side2;
}
public double getSide2(){
return side2;
}
public void setSide3(double side3){
this.side3 = side3;
}
public double setSide3(){
return side3;
}
//面积
public double getArea(){
double area;
double s = (side1+side2+side3)/2;
area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
//周长
public double getPerimeter(){
return side1+side2+side3;
}
//返回三角形信息的方法
public String toString(){
return "三角形的三边长分别为:"+side1+" "+side2+" "+side3+" 周长为:"+getPerimeter()+" 面积为:"+getArea();
}
}
主函数:
package 实验4;
public class Program1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle triangle = new Triangle(1,1.5,1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle.toString());
System.out.print("三角形的颜色为:"+triangle.getColor());
if(triangle.isFilled()) System.out.print(" 且已被填充");
else System.out.print(" 且未被填充");
}
}
【Circle2D类】定义Circle2D类,包括:
周长为23.145.5=35.54
面积为3.145.55.5=94.985
点(3,3)与(2,2)距离为根2小于5.5,在圆内
圆心(4,5)与(2,2)距离为根13大于5.5-10.5,给定圆不在此圆内
圆心(3,5)与(2,2)距离为根5小于5.5-2.3,给定圆在圆内不重叠
一开始考虑不周,没有弄清题目意思。实际上在圆内是圆心距小于半径差(这里是小圆半径-大圆半径,实际是在判断大圆是否在小圆内),而两个圆重叠的意思就是相交和相切的情况。
Circle2D类:
package 实验4;
import java.lang.Math;
public class Circle2D {
//横坐标,纵坐标,半径
private double x;
private double y;
private double radius;
//无参构造函数
Circle2D(){
x = 0;
y = 0;
radius = 1;
}
//有参构造函数
Circle2D(double x,double y,double radius){
this.x = x;
this.y = y;
this.radius = radius;
}
//返回横坐标,纵坐标和半径的get函数
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getRadius(){
return radius;
}
//面积
public double getArea(){
return 3.14*radius*radius;
}
//周长
public double getPerimeter(){
return 2*3.14*radius;
}
//判断点是否在圆内
public boolean contains(double x1, double y1){
if(Math.sqrt(Math.pow(x-x1, 2)+Math.pow(y-y1, 2))<radius) return true;
return false;
}
//判断给定的圆是否在此圆内
public boolean contains(Circle2D circle){
if(Math.sqrt(Math.pow(x-circle.x, 2)+Math.pow(y-circle.y, 2))<radius-circle.getRadius()) return true;
return false;
}
//判断给定的圆是否与此圆重叠
public boolean overlaps(Circle2D circle){
if(Math.sqrt(Math.pow(x-circle.x, 2)+Math.pow(y-circle.y, 2))>=radius-circle.getRadius()&&Math.sqrt(Math.pow(x-circle.x, 2)+Math.pow(y-circle.y, 2))<=radius+circle.getRadius())return true;
return false;
}
}
主函数:
package 实验4;
public class Program2 {
public static void main(String[] args) {
Circle2D c1 = new Circle2D(2,2,5.5);
System.out.println("c1的周长为:"+c1.getPerimeter());
System.out.println("c1的面积为:"+c1.getArea());
System.out.println("c1.contains(3, 3)的结果为:"+c1.contains(3, 3));
System.out.println("c1.contains(new Circle2D(4, 5, 10.5))的结果为:"+c1.contains(new Circle2D(4, 5, 10.5)));
System.out.println("c1.overlaps(new Circle2D(3, 5, 2.3))的结果为:"+c1.overlaps(new Circle2D(3, 5, 2.3)));
}
}