java三⾓形类Triangle 三⾓形类Triangle
设计⼀个名为 Triangle 的类来扩展 GeometricObject 类。该类包括:
1,三个名为 side1,side2,side3 的double 数据域表⽰这个三⾓形的三条边,它们的默认值是1.0.
2,⼀个⽆参构造⽅法创建的默认的三⾓形
3,⼀个能创建带指定side1,side2,side3的三⾓形的构造⽅法
4,所有三个数据域的访问器⽅法
5,⼀个名为getArea()的⽅法返回这个三⾓形的⾯积
6,⼀个名为getPerimeter()的⽅法返回这个三⾓形的周长
7,⼀个名为toString()的⽅法返回这个三⾓形的字符串描述
......
问题分析:
按部就班,根据要求,从数据域,到构造⽅法(⽆参,有参),到⽅法,到测试类
学会使⽤继承以及继承的⽅法就好
代码演⽰(已验证):
1,创建的类:
// 创建的类
package java_testquestions;
class GeometricObject{
private String color ="white";
private boolean filled;
private java.util.Date dateCreated;
public GeometricObject(){
dateCreated =new java.util.Date();
}
public GeometricObject(String color,boolean filled){
dateCreated =new java.util.Date();
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
}
public boolean filled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public String toString(){
return"created on "+dateCreated +"\n color: "+color +" and filled: "+filled;
}
}
public class Triangle extends GeometricObject
{
private double side1;
private double side2;
private double side3;
String color;
Triangle(){
side1 =1.0;
side2 =1.0;
side3 =1.0;
}
public Triangle(double side1,double side2,double side3){
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1(){
return side1;
}
public double getSide2(){
return side2;
}
public double getSide3(){
return side3;
}
// ⽗类中已有该⽅法
public void setColor(String Color){
color = Color;
}
public String getColor(){
return color;
}
public double getArea(){
double s =(side1+side2+side3)*1.0/2;
double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
public double getPerimeter(){
return side1+side2+side3;
}
java的tostring方法
public String toString(){
return"Triangle: side1 = "+side1 +"side2 = "+side2 +" side3 = "+side3; }
}
2,测试类,Triangle_Test
// 测试类
package java_testquestions;
import java.util.Scanner;
public class Triangle_Test
{
public static void main(String[] args)
{
Scanner input =new Scanner(System.in);
System.out.println("请输⼊三⾓形的三条边:");// 需要⽤字符串类型输⼊,这样在下⾯输⼊颜⾊时就不会报错
String side11 = Line();
double side1 =  Double.parseDouble(side11);
String side22 = Line();
double side2 =  Double.parseDouble(side22);
String side33 = Line();
double side3 =  Double.parseDouble(side33);
//  double side1 = Double();
//  double side2 = Double();
//  double side3 = Double();
Triangle triangle =new Triangle(side1,side2,side3);
System.out.println("请输⼊三⾓形的颜⾊: ");
String color = Line();
triangle.setColor(color);
System.out.println("\n");
System.out.println("请输⼊是否填充 true or false:");
boolean filled = Boolean();
triangle.setFilled(filled);
System.out.println("三⾓形的⾯积为:"+Area());
System.out.println("三⾓形的周长为:"+Perimeter());
System.out.println("三⾓形的颜⾊为:"+Color());
System.out.println("三⾓形是否填充:"+triangle.filled());
}
}
泡泡:
创建类的话认真点应该没什么,在测试类⾥⾯有⼀个细节需要注意:在输⼊ double 类型 的边长时,要先使⽤字符串类型输⼊,然后在转化为double类型数据,转化⽅式如下:
String side11 = Line();
double side1 =  Double.parseDouble(side11);
String side22 = Line();
double side2 =  Double.parseDouble(side22);
String side33 = Line();
double side3 =  Double.parseDouble(side33);
嗯,ok,收⼯。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。