1、定义长方形类,含:
属性:宽、高(整型);
方法:求周长、面积;
构造方法3个:(1)无参——宽、高默认值为1;(2)1个参数——宽、高均为参数值;(3)2个参数——宽、高各为参数值。
要求:进行测试。
代码如下:
长方形的类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | packageTest1;publicclassRectangle {    //定义长宽属性    privateintiWidth;    privateintiHeight;    //构造器1    publicRectangle(){        this.iHeight = 1;        this.iWidth = 1;    }    //构造器2    publicRectangle(intiIndex){        this.iWidth = iIndex;        this.iHeight = iIndex;    }    //构造器3    publicRectangle(intiWidth, intiHeight){        this.iHeight = iHeight;        this.iWidth = iWidth;    }    //求周长    publicintgetLength(){        return2*(this.iHeight+this.iWidth);    }    //求面积    publicintgetSquare(){        returnthis.iHeight*this.iWidth;    }}测试类:packageTest1;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        Rectangle oRec1 = newRectangle();        System.out.println("默认长方形的周长为:"+oRec1.getLength());        System.out.println("默认长方形的面积为:"+oRec1.getSquare());        Rectangle oRec2 = newRectangle(2);        System.out.println("一个参数长方形的周长为:"+oRec2.getLength());        System.out.println("一个参数长方形的面积为:"+oRec2.getSquare());        Rectangle oRec3 = newRectangle(2,3);        System.out.println("两个参数长方形的周长为:"+oRec3.getLength());        System.out.println("两个参数长方形的面积为:"+oRec3.getSquare());    }} | 
运行结果:
默认长方形的周长为:4
默认长方形的面积为:1
一个参数长方形的周长为:8
一个参数长方形的面积为:4
两个参数长方形的周长为:10
两个参数长方形的面积为:6
2、定义圆类,它有一个变量radius(半径)。从键盘输入数据,通过构造方法传递给radius,编程计算并输出圆的周长和面积(确保输入的数据不为负数)。
要求:进行测试。
代码如下:
圆的类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | packageTest2;publicclassCircle {    privatedoubleradius;    publicCircle(doubledRadius){        this.radius = dRadius;    }    publicdoublegetLength(){        return2*Math.PI*this.radius;    }    publicdoublegetArea()    {        returnMath.PI*this.radius*this.radius;    }}测试类:packageTest2;importjava.util.Scanner;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        doubleiRadius;        Scanner sc = newScanner(System.in);        System.out.print("请输入圆的半径:");        iRadius = sc.nextDouble();        if(iRadius < 0){            System.out.println("你输入的半径有误!");        }        else{            Circle oCir = newCircle(iRadius);            System.out.println("圆的周长为:"+oCir.getLength());            System.out.println("圆的面积为:"+oCir.getArea());        }    }} | 
运行结果:
正数时:
请输入圆的半径:1
圆的周长为:6.283185307179586
圆的面积为:3.141592653589793
负数时:
请输入圆的半径:-1
你输入的半径有误!
3、定义长方体类,定义求底面积、体积的方法。(可利用上述定义的长方形类)
要求:进行测试。
代码如下:
长方体类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |         packageTest3;importTest1.Rectangle;publicclassRecV extendsRectangle{    intiLong;    publicRecV(intiWidth, intiHeight, intiLong){        super(iWidth,iHeight);        this.iLong = iLong;    }    publicintgetBtmArea(){        returnthis.getSquare();    }    publicintgetVolume(){        returnthis.getSquare()*this.iLong;    }}测试类:packageTest3;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        RecV oRecV = newRecV(1,2,3);        System.out.println("长方体的底面积为:"+oRecV.getBtmArea());        System.out.println("长方体的体积为:"+oRecV.getVolume());    }} | 
运行结果:
长方体的底面积为:2
长方体的体积为:6
4、定义球类,定义求体积的方法。(可利用上述定义的圆类)
要求:进行测试。
代码如下:
球的类:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | packageTest4;importTest2.Circle;publicclassBool extendsCircle{    publicBool(doubledRadius) {        super(dRadius);        // TODO Auto-generated constructor stub    }    publicdoublegetBoolVolume(){        return(4/3)*Math.PI*Math.pow(this.radius, 3.0);    }}测试类:    packageTest4;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        Bool oBool = newBool(1);        System.out.println("球的体积为:"+oBool.getBoolVolume());    }} | 
运行结果:
球的体积为:3.141592653589793
5、定义一个计算器类,包括加、减、乘、除运算。
要求:进行测试。
代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | packageTest5;publicclassCalculator {    privateintiFirstNum;    privateintiSecondNum;    publicCalculator(intiFirst, intiSecond){        this.iFirstNum = iFirst;        this.iSecondNum = iSecond;    }    publicintgetAdd(){        returnthis.iFirstNum + this.iSecondNum;    }    publicintgetSub(){        returnthis.iFirstNum - this.iSecondNum;    }    publicintgetMul(){        returnthis.iFirstNum * this.iSecondNum;    }    publicvoidgetDev(){        if(this.iSecondNum ==0){            System.out.print("分子不能为零!");        }        else        {            System.out.print(this.iFirstNum/this.iSecondNum);        }    }}测试类:packageTest5;publicclassTest {    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        Calculator oCal = newCalculator(4,2);        System.out.println("两数之和为 :"+oCal.getAdd());        System.out.println("两数之差为:"+oCal.getSub());        System.out.println("两数之积为:"+oCal.getMul());        System.out.print("两数之商为:");        oCal.getDev();    }} | 
运行结果:
两数之和为 :6
两数之差为:2
两数之积为:8
两数之商为:2
7、编写程序使用复数类Complex验证两个复数 2+2i 和3+3i 相加产生一个新的复数5+5i 。复数类Complex满足如下要求:
(1)属性:RealPart : int型,代表复数的实数部分;ImaginPart : int型,代表复数的虚数部分
(2)方法:
Complex( ) : 构造方法,将复数的实部和虚部都置0
Complex( int r , int i ) : 构造方法,形参 r 为实部的初值,i为虚部的初值。
Complex complexAdd(Complex a) : 将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者。
String toString( ) : 把当前复数对象的实部、虚部组合成 a+bi 的字符串形式,其中a 和 b分别为实部和虚部的数据。
(3)进行测试。
代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | packageTest7;publicclassComplex {    privateintiRealPart;    privateintiImaginPart;    publicComplex(){        iRealPart = 0;        iImaginPart = 0;    }    publicComplex(intr, inti){        iRealPart = r;        iImaginPart = i;    }    publicComplex complexAdd( Complex a ){        this.iRealPart += a.iRealPart;        this.iImaginPart += a.iImaginPart;        returnthis;    }    publicString toString(){        returnthis.iRealPart+"+"+this.iImaginPart+"i";    }}测试类:packageTest7;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        //复数实例化        Complex oFirstNum = newComplex();        Complex oFirstNum1 = newComplex(2,3);        Complex oSecondNum = newComplex(3,4);        oFirstNum.complexAdd(oSecondNum);        System.out.println("1。复数之和为:"+oFirstNum.toString());        oFirstNum1.complexAdd(oSecondNum);        System.out.println("2.复数之和为:"+oFirstNum1.toString());    }} | 
运行结果:
1。复数之和为:3+4i
2.复数之和为:5+7i
8、试编写Java代码实现一个计数器类Computer其中包括:
域value :用来保存计数器的当前值;
方法increment(): 计数器加一;
方法decrement() :计数器减一;
方法reset():计数器清零。
请编写并调试程序。
代码如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | packageTest8;publicclassComputer {    privateintvalue;    publicComputer(){        this.value = 0;    }    publicvoidincrement(){        this.value++;    }    publicvoiddecrement(){        this.value--;    }    publicvoidreset(){        this.value = 0;    }    publicintgetValue(){        returnthis.value;    }}测试类:packageTest8;publicclassTest {    /**     * @param args     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        Computer oCount = newComputer();        System.out.println("计数器的初始值为:"+ oCount.getValue());        oCount.increment();        oCount.increment();        oCount.increment();        System.out.println("加三后的值为:"+ oCount.getValue());        oCount.decrement();        System.out.println("减一后的值为:"+ oCount.getValue());        oCount.reset();        System.out.println("初始化后的值为:"+ oCount.getValue());    }} | 
运行结果:
计数器的初始值为:0
加三后的值为:3
减一后的值为:2
初始化后的值为:0
原文:http://www.cnblogs.com/zhangkeyu/p/6659404.html