zoukankan      html  css  js  c++  java
  • 10年6月17日Java实验测试题目

    /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";

           }

            

    }

  • 相关阅读:
    【爬虫】python+urllib+beautifusoup爬取花瓣网美女图片
    Django urls.py报错: raise TypeError('view must be a callable or a list/tuple in the case of include()
    python国内豆瓣源
    python pip使用报错:Fatal error in launcher: Unable to create process using '"'
    warning: refname 'HEAD' is ambiguous解决方法
    twisted.internet.error.DNSLookupError: DNS lookup failed: address "'http:" not found: [Errno 11001] getaddrinfo failed.解决办法
    react + iscroll上拉加载下拉刷新分享
    盘点网页图标几种解决方案
    宽度自适应布局
    jqueryUI
  • 原文地址:https://www.cnblogs.com/wangs/p/1759604.html
Copyright © 2011-2022 走看看