博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

10年6月17日Java实验测试题目

Posted on 2010-06-17 15:50  小往  阅读(224)  评论(0编辑  收藏  举报

/Files/wangs/10年6月17日测试.doc

 

1

class fushu

{

 int a;//实部

 int b;//虚部

    public fushu(int x,int y)

    {

        this.a=x;

        this.b=y;

    }

    public   fushu add(fushu add1)

    {

       

        int x=add1.a+this.a;

        int y=add1.b+this.b;

        return new fushu(x,y);

    }

    public   fushu plus(fushu sub1)

    {

        int x=this.a-sub1.a;

        int y=this.b-sub1.b;

        return new fushu(x,y);

 

    }

 

public String toString() {

 String rt = "";

 if (b > 0)

   rt = "(" + a + "+" + b + "i" + ")";

 if (b == 0)

   rt = "(" +a + ")";

 if (b < 0)

   rt = "(" + a + b + "i" + ")";

 return rt;

 }

 

    public static void main(String [] args)

    {

       fushu fushu1=new fushu(3,4);

        fushu fushu2=new fushu(2,-13);      

        fushu result=fushu1.add(fushu2);

        System.out.println(result);

        //System.out.println("result="+result.a+"+"+result.b+"i");

       

    }

}  

 

2. //InterfaceTest.java

//主类InterfaceTest

public class InterfaceTest{

       public static void main(String args[]){

              MyLine line=new MyLine( 15);

              MyCircle circle=new MyCircle(10);

              MyRect rect = new MyRect(10,20);

              Shape shapes[]=new Shape[3];

              shapes[0]=line;

              shapes[1]=circle;

              shapes[2]=rect;

              String output=

                      line.toString() + "\n"

                     +circle.toString() + "\n" + rect.toString();

              for (int i = 0; i < shapes.length; i++){

                     output += "\n\n"   +

                            " " + shapes[i].toString() +

                            "\nArea = " +

                            shapes[i].length();

              }

              System.out.println();

              System.out.println(output);

        

       }

}

//定义接口Shape

interface Shape{

       //计算长度

       public abstract double length();

 

}

//定义类Point

class MyLine

    implements Shape{

 private int length;

 public MyLine(int length) {

          this.length=length;

 }

 

 public String toString(){

        return "this is MyLine" ; 

 }

 public double length(){

        return length;

 }

 

}

class MyCircle

       implements Shape{

 protected double radius;

 

 public MyCircle(double circleRadius ){

        

       this.radius=circleRadius;

 }

 

 public double length(){

        return 2*Math.PI * radius;

 }

 public String toString(){

        return "this is a Circle";

 }

 

}

class MyRect

       implements Shape{

       protected int height;

    protected int width;

 

       public MyRect(int height,int width){

               

              this.height=height;

              this.width=width;

 

       }

    

       public double length(){

              return 2 *(height+width);

       }

 

       public String toString(){

              return "this is a rectangle";

       }

        

}