日期:2014-05-20  浏览次数:20868 次

初学者都快把头发挠没了 向众人请教
//若有一个接口A说明一个面积方法area(),

    //     interface   A   {

        //       public   double   area();

        //}

//请设计一个三角形类GCLass实现接口A中的area()方法,GClass构造函数的参数为三边长s1、s2、s3,均为整数。


//上面是题目要求,下面是写的一段程序   可是老不对   请CSDN帮忙

import   java.math;

interface   A
{
public   double   area();
}

class   GClass1   implements   A
{
public   double   area(int   a,int   b,int   c)
{
double   a1,s;
a1   =   (a   +   b   +   c)   /   2;
s=Sqr(s   *   (s   -   a)   *   (s   -   b)   *   (s   -   c));
System.out.println( "面积是: "+s);  
}

public   static   void   main(String   args[])
{
GClass   s0=new   GClass();
s0.area(3,5,7);
}

}

------解决方案--------------------
package com;

interface A {

public double area(int a, int b, int c);
}

class GClass1 implements A {

public double area(int a, int b, int c) {

double a1 = 0, s = 0;
a1 = (a + b + c);
s = Math.sqrt((a1 * (a1 - a) * (a1 - b) * (a1 - c)));
System.out.println( "面积是: " + s);
return s;
}

public static void main(String args[]) {

GClass1 s0 = new GClass1();
s0.area(3, 5, 7);
}

}

------解决方案--------------------
刚才的程序有点问题:

package com;

interface A {

public double area(int a, int b, int c);
}

class GClass1 implements A {

public double area(int a, int b, int c) {

double a1 = 0, s = 0;
a1 = (double) (a + b + c) / 2;
s = Math.sqrt(a1 * (a1 - a) * (a1 - b) * (a1 - c));
System.out.println( "面积是: " + s);
return s;
}

public static void main(String args[]) {

GClass1 s0 = new GClass1();
s0.area(3, 5, 7);
}

}

------解决方案--------------------
import java.math;

interface A
{
public double area();
}

class GClass1 implements A
{
int s1,s2,s3;
GCLass1(int a,int b,int c)
{
s1=a;
s2=b;s3=c;
}
public double area()
{
double a1,s;
a1=s=0.0;
a1 = (a + b + c);
s = Math.sqrt((a1 * (a1 - s1) * (a1 - s2) * (a1 - s3)));
System.out.println( "面积是: "+s);
}

public static void main(String args[])
{
GClass s0=new GClass(3,5,7);
s0.area();
}

}

//计算三角形的面积我不记得了......不知道对不对
------解决方案--------------------

如果算法不对,就改一下area()里的算法.好像是要/2的吧

double a1,s;
a1=s=0.0;
a1 = (a + b + c)/2;//========> 是不是要/2的呢
s = Math.sqrt((a1 * (a1 - s1) * (a1 - s2) * (a1 - s3)));
System.out.println( "面积是: "+s);

------解决方案--------------------
yes,是要/2的...
------解决方案--------------------
interface A {
public double area();
}