(1)编写一个Java程序,该程序中有5个类:Trangle、Lader、Circle、AreaAndLength和TrangleException,AreaAndLength类来调用其他3个类。
(2)Trangle类具有类型为double的三个边,以及周长、面积属性,具有有参构造函数、返回周长、返回面积以及修改三条边的功能。
(3)Lader类具有类型为double的上底、下底、高、面积属性,具有有参构造函数、返回周长、返回面积的功能。
(4)Circle类具有类型为double的半径、周长和面积属性,具有有参构造函数、返回周长、返回面积的功能。
(5)AreaAndLength类有且仅有一个主函数,在主函数中实例化Trangle、Lader、Circle这3个类、输出3个类的周长以及面积。
(6)TrangleException类完成三角形构成的判断,如果不能构成三角形,输出提示信息。
(7)在Trangle类中主动抛出TrangleException异常,在AreaAndLength类中处理。
import java.io.*;
class Trangle
{
double A,B,C,area,length;
boolean boo;
public void TrangleException(){}
public Trangle(double a,double b,double c)throws IOException
{
A=a;
B=b;
C=c; //参数a,b,c分别赋值给sideA,sideB,sideC
}
double getLength()
{
double length;
length=A+B+C;
return length; //方法体,要求计算出length的值并返回
}
public double getArea()
{
if(boo)
{
double p=(A+B+C)/2.0;
area=Math.sqrt(p*(p-A)*(p-B)*(p-C)) ;
return area;
}
else
{
System.out.println("不是一个三角形,不能计算面积");
return 0;
}
}
public void setABC(double a,double b,double c)
{
A=a;B=b;C=c; //参数a,b,c分别赋值给sideA,sideB,sideC
}
}
class Lader
{
double above,bottom,height,area;
Lader(double a,double b,double h)
{
above=a;bottom=b;height=h; //方法体,将参数a,b,c分别赋值给above,bottom,height
}
double getArea()
{
area=(above+bottom)*height/2;
return area; //方法体,,要求计算出area返回
}
}
class Circle
{
double radius,area;
Circle(double r)
{ radius=r;
//方法体
}
double getArea()
{
return 3.14*radius*radius;
}
double getLength()
{
return 2*3.14*radius;
}
void setRadius(double newRadius)
{
radius=newRadius;
}
double getRadius()
{
return radius;
}
}
class TrangleException extends Trangle{
public TrangleException(double a, double b, double c) throws IOException {
super(a, b, c);
// TODO Auto-generated constructor stub
try
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
a = Integer.valueOf(stdin.readLine());
b = Integer.valueOf(stdin.readLine());
c = Integer.valueOf(stdin.readLine());
if(a+b>c&&b+c>a&&a+c>b) //a,b,c构成三角形的条件表达式
{ boo=true;//给boo赋值。
System.out.println("能构成三角形。");
}
else
{
boo=false; //给boo赋值。
}
}
catch (IOException e) {
boo=false;
System.out.println("不能构成三角形。");
}
}
}
public class AreaAndLength
{
public static void main(String args[]) throws IOException
{ double length,area;
Circle circle;
Trangle trangle;
Lader lader;
circle=new Circle(10); //创建对象circle //创建对象trangle。
lader=new Lader(4,5,8); //创建对象lader
trangle= new Trangle(5,6,5); //创建对象trangle。
trangle.TrangleException();
length=circle.getLength(); // circle调用方法返回周长并赋值给length
System.out.println("圆的周长:"+length);
area=circle.getArea(); // circle调用方法返回面积并赋值给area
System.out.println("圆的面积:"+area);
length=trangle.getLength(); // trangle调用方法返回周长并赋值给length
System.out.println("三角形的周长:"+length);
area=trangle.getArea ();// trangle调用方法返回面积并赋值给area
System.out.println("三角形的面积:"+area);
area=lader.getArea(); // lader调用方法返回面积并赋值给area
System.out.println("梯形的面积:"+area);
}
}
但是运行结果出错了,还有我想问一下怎样修改才能使它的数字是从键盘输入而不是代码固定边长??
Scanner 类能实现
Scanner scan = new Scanner();
int length, width, height;
//键盘输入长宽高
length = scan.nextInt();
width = scan.nextInt();
height = scan.nextInt();
//下面是你的类 lader
lader=new lader(length, width, height);
也可以是用BufferedReader br=new BufferedReader(new InputStreamReader(System.in))实现输入。
package hello;
interface Area {
double getArea();
}
interface Length {
double getLength();
}
public class Trangle implements Area,Length {
double sideA;
double sideB;
double sideC;
double area;
double length;
public double getLength()
{
length=sideA+sideB+sideC;
return length; //计算出周长length的值并返回
}
public double getArea()//重写接口是public方法
{
double p = ((sideA+sideB+sideC)/2);
area = Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ;
return area;//计算出面积area的值并返回
}
public void setABC(double a,double b,double c) throws TrangleException{
sideA=a;sideB=b;sideC=c; //修改边长
throw new TrangleException(sideA,sideB,sideC);
}
public Trangle() {
// TODO Auto-generated constructor stub
}
}
class Lader implements Length,Area {
double above;
double bottom;
double height;
double area;
double length;
double y1;
double y2;
public double getArea()
{
return (above+bottom)*height/2 ;//计算出面积area的值并返回
}
public double getLength()
{
length=above+bottom+y1+y2;
return length; //计算出周长length的值并返回
}
public void setABhy(double la,double lb,double h,double y3,double y4){
above = la;
bottom = lb;
height = h; //修改边长
y1 = y3;
y2 = y4;
}
}
class Circle implements Area,Length {
double radius;
double area;
double length;
public double getArea()
{
// area = 3.14*radius*radius;
return 3.14*radius*radius;
}
public double getLength()
{
return 2*3.14*radius;
}
public void setR(double r){
radius=r;
}
}
class TrangleException extends Exception {
String message;
public TrangleException(double sideA,double sideB,double sideC){
if(sideA+sideB<=sideC||sideA+sideC<=sideB||sideB+sideC<=sideA){
message="no trangle";
}
}
public String warnMess(){
return message;
}
}
package hello;
import java.util.Scanner;
public class AreaAndLength {
@SuppressWarnings("resource")
public static void main(String args[]) throws Exception{
Scanner in= new Scanner(System.in);
System.out.println("依次输入边长");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
Trangle trangle = new Trangle();
try
{
trangle.setABC(a, b, c);
System.out.println("三角形的面积:"+trangle.getArea());
System.out.println("三角形的周长:"+trangle.getLength());
}
catch (TrangleException e) {
System.out.println(e.warnMess());
}
// Scanner in= new Scanner(System.in);
Lader lader= new Lader();
lader.setABhy(1, 2, 3, 4, 5);
System.out.println("梯形的面积:"+lader.getArea());
System.out.println("梯形的周长:"+lader.getLength());
Circle circle = new Circle();
circle.setR(10);
System.out.println("圆的周长:"+circle.getLength());
System.out.println("圆的面积:"+circle.getArea());
}
}