声明三角形类,继承图形抽象类,计算三角形的面积和周长public abstract class closedFigure
{
protected String shape;
protected closedFigure(String shape)
{this.shape=shape;
}protected closedFigure()
{this("未知");
}
public abstract double area();
public abstract double perimeter();
public void print()
{System.out.println("一个"+this.shape+","+this.toString()+",周长为"+this.perimeter()+",面积为"+this.area());
}
}
public class Sjx extends closedFigure
{protected double a;
protected double b;
protected double c;
public Sjx(double a,double b,double c)
{super("三角形");
this.a=a;
this.b=b;
this.c=c;
}
public Sjx()
{this(0,0,0);
}public String toString()
{return "a边长"+this.a+",b边长"+this.b+",c边长"+this.c;
}
public double area()
{return (sqrt ((a+b+c)/2.0)* (((a+b+c)/2.0)-a) *(((a+b+c)/2.0)-b)*(((a+b+c)/2.0)-c));
}
public double perimeter()
{
return (a+b+c);
}
}
怎么在jdk中编译,而且他告诉我有两处错误。

解决方案 »

  1.   

    唉,楼主的标题……以为真有什么好玩的事情……
    2处错误是:
    1、一个java文件中,最多只能有一个类(或接口)用public修饰,并且该类(或接口)的名称
    和文件名相同,故将public abstract class closeFigure中的public去掉;
    2、sqrt不能直接拿来用,改为Math.sqrt()就行了。PS:类/接口的名称首字母要大写!
      

  2.   

    唉,楼主的标题……以为真有什么好玩的事情……
    2处错误是:
    1、一个java文件中,最多只能有一个类(或接口)用public修饰,并且该类(或接口)的名称
    和文件名相同,故将public abstract class closeFigure中的public去掉;
    2、sqrt不能直接拿来用,改为Math.sqrt()就行了。PS:类/接口的名称首字母要大写!
      

  3.   

    1 类名请大写字母开头
    2 Sjx这种类名怎么看怎么疼   Triangle不行吗?
      

  4.   

    在我这里远远不止两个错误,你那里起码也有:
    1、同一个文件最多只能有一个public修饰的类,所以把closeFigure、Sjx中的一个public去掉
    2、换行的时候,一个字符串被拆分成多个字符串,字符串两端要用双引号包住,字符串之间要用+号连接,也就是说:
    public void print()
    {System.out.println("一个"+this.shape+","+this.toString()+",周长为"+this.perimeter()+",面积为"+this.area());
    }
    应该改成:
    public void print()
    {System.out.println("一个"+this.shape+","+this.toString()+",周长为"+this.perimeter()+",面积"+"为"+this.area());
    }总体来说,你的代码比较让人蛋疼,下面给个稍微让人淡定一点的,供参考import java.util.*; abstract class Figure
    {
    protected String shape; protected Figure(String shape)
    {
    this.shape = shape;
    } protected Figure()
    {
    this("未知");
    } public abstract double area(); public abstract double perimeter(); public void print()
    {
    System.out.println("一个" + this.shape + "," + this.toString() + ",周长为"
    + this.perimeter() + ",面积为" + this.area());
    }
    }class Triangle extends Figure
    {
    protected double a;
    protected double b;
    protected double c; public Triangle(double a, double b, double c)
    {
    super("三角形");
    this.a = a;
    this.b = b;
    this.c = c;
    } public void print()
    {System.out.println("一个"+this.shape+","+this.toString()+",周长为"+this.perimeter()+",面积" +"为"+this.area());
    } public Triangle()
    {
    this(0, 0, 0);
    } public String toString()
    {
    return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c;
    } public double area()
    {
    return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a)
    * (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c));
    } public double perimeter()
    {
    return (a + b + c);
    }
    }
      

  5.   

    不好意思,刚才在搞测试,多粘了一块代码,修正一下:import java.util.*; abstract class Figure
    {
    protected String shape; protected Figure(String shape)
    {
    this.shape = shape;
    } protected Figure()
    {
    this("未知");
    } public abstract double area(); public abstract double perimeter(); public void print()
    {
    System.out.println("一个" + this.shape + "," + this.toString() + ",周长为"
    + this.perimeter() + ",面积为" + this.area());
    }
    }class Triangle extends Figure
    {
    protected double a;
    protected double b;
    protected double c; public Triangle(double a, double b, double c)
    {
    super("三角形");
    this.a = a;
    this.b = b;
    this.c = c;
    } public Triangle()
    {
    this(0, 0, 0);
    } public String toString()
    {
    return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c;
    } public double area()
    {
    return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a)
    * (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c));
    } public double perimeter()
    {
    return (a + b + c);
    }
    }
      

  6.   

    更有意思的在这里!!!!!
    http://007ej.com/user.asp
    http://happyran.zbpifa.com