您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > 第四章Java章节习题
1.分别编写两个方法,一个方法的功能是求一个double型数值的向右取整,另一个方法求double型数值的向左取整。数d的向右取整是大于等于d的最小整数,d的向左取整是小于等于d的最大整数。例如:5.3的向右取整是6而向左取整是5.答:publicclassOne{staticintright(doublex){if(x!=5)x=x+1;return(int)x;}staticintleft(doublex){return(int)x;}publicstaticvoidmain(Stringargs[]){System.out.println(5.3向左取整为:+left(5.3));System.out.println(5.3向右取整为:+right(5.3));}}程序运行结果为:2.写一个方法计算一个整数各位数字的和。使用下述的方法说明:publicstaticintsumDigits(longn)例如:sumDigits(276)返回2+7+6=15。提示:用求余运算符%分解数字,用除号/分离位数。例如334%10=4而334/10=33.用循环反复分解和分离每位数字,直到所有的位数都被分解。答:publicclassTwo{publicstaticintsumDigits(longn){longsum=0,t;while(n!=0)4747{t=n%10;n=n/10;sum+=t;}return(int)sum;}publicstaticvoidmain(Stringargs[]){System.out.println(334各个位数字之和为:+sumDigits(334));}}程序运行结果为:3.编写程序,用Math类中的sqrt方法打印下表:NumberSquareRoot....................................00.000021.414242.000062.449582.8284103.1623123.4641143.7417164.0000184.2426204.4721答:publicclassThree{publicstaticvoidmain(Stringargs[]){System.out.println(NumberSquareRoot);System.out.println(..................);for(inti=0;i=20;)4848{System.out.printf(%d\t%.4f\n,i,Math.sqrt(i));i=i+2;}}}程序运行结果为:4.请定义一个名为Rectangle的矩形类。数据域为宽(width)、高(height)和颜色(color),width和height是double型的,color时String型的。假定所有矩形的颜色相同。用一个类变量表示颜色。要求提供访问其方法和计算矩形面积的FindArea()方法。答:importjava.util.Scanner;classRectangle{Rectangle(doublew,doubleh){width=w;height=h;}publicdoubleFindArea(){returnwidth*height;}doublewidth,height;staticStringcolor;4949}publicclassFour{publicstaticvoidmain(String[]args){doublewidth,height;Scanners=newScanner(System.in);System.out.print(请输入矩形的宽:);width=s.nextDouble();System.out.print(请输入矩形的高:);height=s.nextDouble();System.out.print(请输入矩形的颜色:);Rectangle.color=s.next();Rectanglejuxing=newRectangle(width,height);System.out.println(矩形的面积为:+juxing.FindArea());}}程序运行结果为:5.请定义一个名为Fan的类模拟风扇,属性为speed、on、radius和color。要求为每个属性提供访问器方法,并提供方法toString,返回包含类中所有属性值的字符串。假设风扇有三个固定的速度,用常数1、3、9表示慢、中、快速。然后定义一个测试类,测试Fan类,在测试类中创建一个Fan对象,设置最大速度、半径为10、黄色、打开状态。调用toString方法显示该对象。答:classFan{privateintspeed;privatebooleanon;privatedoubleradius;privateStringcolor;publicFan(ints,booleano,doubler,Stringc){speed=s;on=o;5050radius=r;color=c;}publicintgetSpeed(){returnspeed;}publicbooleangetOn(){returnon;}publicdoublegetRadius(){returnradius;}publicStringgetColor(){returncolor;}publicvoidtoStrings(){if(on)System.out.print(风扇已打开并且);elseSystem.out.print(风扇已关闭);if(speed==1)System.out.println(风速状态为慢);elseif(speed==3)System.out.println(风速状态为中);elseif(speed==9)System.out.println(风速状态为快);elseSystem.out.println(无风速);System.out.println(半径为+radius);System.out.println(颜色为+color);}}5151publicclassFive{publicstaticvoidmain(Stringargs[]){Fanfs=newFan(9,true,10,黄色);fs.toStrings();}}程序运行结果为:6.请定义一个名为Account的类模拟帐户、该类的属性和方法如下图所示,利息安月累计。AccountprivateintidprivatedoublebalanceprivatedoubleannuallnterestRatepublicAccount()publicAccount(intid,doublebalance,doubleannuallnterestRate)publicintgetId()publicdoublegetBalance()publicdoublegetAnnuallnterestRate()publicvoidsetId(intid)publicvoidsetBalance(doublebalance)publicvoidsetAnnuallnterestRate(doubleannuallnterestRate)publicvoidwithdraw(doubleamount)publicvoiddeposit(doubleamount)写一个用户程序测试Account类。在用户程序中,创建一个账号为1234、余额10000、年利率2.5%的Account对象。使用withdraw方法提款3000元,使用deposit方法存款3000元,并打印余额和月利率。答:classAccount{publicAccount(){5252id=0;balance=0.0;annuallnterestRate=0;}publicAccount(intid,doublebalance,doubleannuallnterestRate){this.id=id;this.balance=balance;this.annuallnterestRate=annuallnterestRate;}publicintgetId(){returnid;}publicdoublegetBalance(){returnbalance;}publicdoublegetAnnuallnterestRate(){returnannuallnterestRate;}publicvoidsetId(intid){this.id=id;}publicvoidsetBalance(doublebalance){this.balance=balance;}publicvoidsetAnnuallnterestRate(doubleannuallnterestRate){this.annuallnterestRate=annuallnterestRate;}publicvoidwithdraw(doubleamount){this.balance-=amount;5353}publicvoiddeposit(doubleamount){this.balance+=amount;}privateintid;privatedoublebalance;privatedoubleannuallnterestRate;}publicclassMoney{publicstaticvoidmain(String[]args){Accounta=newAccount(1234,10000,0.0025);System.out.println(当前账户ID:+a.getId());System.out.println(当前账户余额:+a.getBalance());System.out.println(当前利率:+a.getAnnuallnterestRate());System.out.println(取款3000后:);a.withdraw(3000);System.out.println(当前账户余额:+a.getBalance());System.out.println(存款3000后:);a.deposit(3000);System.out.println(当前账户余额:+a.getBalance());}}程序运行结果为:7.分别定义人类(People)、狗类(Dog)和羊类(Sheep).要求为每个属性提供访问器方法。答:classPeople{People(Stringn,inta,Strings)5454{name=n;age=a;sex=s;}publicStringgetName(){returnname;}publicintgetAge(){returnage;}publicStringgetSex(){returnsex;}privateStringname;privateintage;privateStringsex;}classDog{Dog(Stringr,inta,Strings){race=r;age=a;sex=s;}publicStringgetRace(){returnrace;}publicintgetAge(){returnage;}5555publicStringgetSex(){returnsex;}privateStringrace;privateintage;privateStringsex;}classSheep{Sheep(Stringr,inta,Strings){race=r;age=a;sex=s;}publicStringgetRace(){returnrace;}publicintgetAge(){returnage;}publicStringgetSex(){returnsex;}privateStringrace;privateintage;privateStringsex;}p
本文标题:第四章Java章节习题
链接地址:https://www.777doc.com/doc-2167627 .html