java课后习题Word文档下载推荐.docx
- 文档编号:19045032
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:31
- 大小:29.15KB
java课后习题Word文档下载推荐.docx
《java课后习题Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java课后习题Word文档下载推荐.docx(31页珍藏版)》请在冰豆网上搜索。
publicAccount(intid,intbalance)
this.id=id;
this.balance=balance;
publicintgetId()
returnthis.id;
publicvoidsteId(intid)
publicdoublegetBalance()
returnthis.balance;
publicvoidsetBalance(doublebalance)
this.balance=balance;
publicdoublegetannualInterestRate()
returnthis.annualInterestRate;
publicvoidsetAnnualInterestRate(doubleannualInterestRate)
this.annualInterestRate=annualInterestRate;
publicjava.util.DategetDateCreated()
returnthis.dateCreated;
publicdoublegetMonthlyInterestRate()
returnthis.annualInterestRate/12;
publicdoublewithDraw(doublecount)
this.balance-=count;
publicdoubledeposit(doublecount)
this.balance+=count;
publicstaticvoidmain(String[]args)
Accountaccount=newAccount(1122,20000);
account.setAnnualInterestRate(0.045);
account.withDraw(2500);
account.deposit(3000);
System.out.println("
余额:
"
+account.getBalance());
月利息:
+account.getMonthlyInterestRate()*account.getBalance());
开户日期:
+account.getDateCreated().toString());
}
运行结果:
20500.0
76.875
MonApr1620:
54:
13CST2012
8.8设计一个名为Fan的类来表示一个风扇。
这个类包括:
三个名为SLOW、MEDIUM和FAST而值是1、2和3的常量表示风扇的速度。
一个名为speed的int类型私有数据域表示风扇的速度(默认值SLOW)。
一个人名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。
一个名为radius的double类型私有数据域表示风扇的半径(默认值5)。
一个名为color的string类型数据域表示风扇的颜色(默认值为blue)。
这四个数据域的访问器和修改器。
一个创建默认风扇的无参构造方法。
一个名为toString()方法返回描述风扇的字符串。
如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。
如果风扇没有打开,该方法就会返回一个由“fanisoff”和风扇颜色及半径组合成的字符串。
编写一个测试程序,创建两个Fan对象。
将第一个对象设置为最大速度、半径10、颜色为yellow、状态为打开。
将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。
通过调用它们的toSrting方法显示这些对象。
publicclassFan{
finalstaticintSLOW=1;
finalstaticintMEDIUM=2;
finalstaticintFAST=3;
privateintspeed=SLOW;
privatebooleanon=false;
privatedoubleradius=5;
privateStringcolor="
blue"
;
publicintgetSpeed(){
returnspeed;
publicvoidsetSpeed(intspeed){
this.speed=speed;
publicbooleanisOn(){
returnon;
publicvoidsetOn(booleanon){
this.on=on;
publicdoublegetRadius(){
returnradius;
publicvoidsetRadius(doubleradius){
this.radius=radius;
publicStringgetColor(){
returncolor;
publicvoidsetColor(Stringcolor){
this.color=color;
publicFan()
publicStringtoString()
if(on==true)
return"
风扇速度:
+this.getSpeed()+"
\n"
+"
风扇半径:
+this.getRadius()+"
风扇颜色:
+this.getColor()+"
else
fanisoff"
+"
Fanfan=newFan();
fan.setColor("
yello"
);
fan.setRadius(10.0);
fan.setSpeed(FAST);
fan.setOn(true);
Fanfan1=newFan();
fan1.setColor("
bule"
fan1.setOn(false);
fan1.setRadius(5.0);
System.out.println(fan.toString());
System.out.println(fan1.toString());
3
10.0
yello
fanisoff
5.0
bule
8.9在一个正n边形中,所以边的长度都相同,且所有角的度数都相同(即这个多边形是等边等角的)。
设计一个名为RegularPolygon的类,该类包括:
一个名为int型的私有数据域定义多边形的边数,默认值3。
一个名为side的double型私有数据域存储边的长度,默认值1。
一个名为x的double型私有数据域,它定义多边形中点的x坐标,默认值0。
一个名为y的double型私有数据域,它定义多边形中点的y坐标,默认值0。
一个创建带默认值的正多边形的无参构造方法。
一个能创建带指定边数和边长度、中心在(0,0)的正多边形的构造方法。
一个能创建带指定边数和边长度、中心在(x,y)的正多边形的构造方法。
所有数据域的访问器和修改器。
一个返回多边形周长的方法getPerimeter()。
一个返回多边形面积的方法getArea().计算多边形面积的公式是:
面积=(n*s*s)/(4*tan(p/n))
编写一个测试程序,分别使用无参构造方法、RegularPolygon(6,4)和RegularPolygon(10,4,5.6,7.8)创建三个RegularPolygon对象。
显示每个对象的周长和面积。
publicclassRegularPolygon{
privateintn=3;
privatedoubleside=1;
privatedoublex=0;
privatedoubley=0;
publicRegularPolygon()
publicRegularPolygon(intn,doubleside)
this.n=n;
this.side=side;
publicRegularPolygon(intn,doubleside,doublex,doubley)
this(n,side);
this.x=x;
this.y=y;
publicintgetN(){
returnn;
publicvoidsetN(intn){
publicdoublegetSide(){
returnside;
publicvoidsetSide(doubleside){
publicdoublegetX(){
returnx;
publicvoidsetX(doublex){
publicdoublegetY(){
returny;
publicvoidsetY(doubley){
publicdoublegetPerimeter()
returnn*side;
publicdoublegetArea()
return((n*side*side)/(4*Math.tan(Math.PI/n)));
publicstaticvoidmain(String[]args){
RegularPolygonreg1=newRegularPolygon();
reg1的周长为:
+reg1.getPerimeter()+"
"
+
"
reg1的面积为:
+reg1.getArea());
RegularPolygonreg2=newRegularPolygon(6,4);
+reg2.getPerimeter()+
reg2的面积为:
+reg2.getArea());
RegularPolygonreg3=newRegularPolygon(10,4,5.6,7.8);
reg3的周长为:
+reg3.getPerimeter()+
reg3的面积为:
+reg3.getArea());
3.0reg1的面积为:
0.43301270189221946
24.0reg2的面积为:
41.569219381653056
40.0reg3的面积为:
123.10734148701015
8.11为二次方程式ax2+bx+c=0设计一个名为QuadraticEquation的类。
代表三个系数的私有数据域a、b、c。
一个参数为a、b和c的构造方法。
a、b、c的三个get方法。
一个名为getDiscriminant()的方法返回判别式,b2-4ac。
一个名为getRoot1()和getRoot2()的方法返回登时的两个根:
r1=(-b+sqr(b2-4ac))/2aandr2=(-b-sqr(b2-4ac))/2a
这些方法只有在判别式为非负数时才有用。
如果判别式为负,这些方法返回0.
编写一个测试程序,提示用户输入a、b和c的值,然后显示判别式的结果。
如果判别式为正数,显示俩个根;
如果判别式为0,显示一个根;
否则,显示“Theequationhasnoroots.”(这个方程无根)。
参见练习题的3.1运行示例。
importjava.util.Scanner;
publicclassQuadraticEquation{
privatedoublea;
privatedoubleb;
privatedoublec;
publicQuadraticEquation(doublea,doubleb,doublec)
this.a=a;
this.b=b;
this.c=c;
publicdoublegetA(){
returna;
publicdoublegetB(){
returnb;
publicdoublegetC(){
returnc;
publicdoublegetDiscriminant()
return(b*b-4*a*c);
publicdoublegetRoot1()
if(this.getDiscriminant()>
=0)
return((-b+Math.pow(this.getDiscriminant(),0.5))/(2*a));
return0;
publicdoublegetRoot2()
return((-b-Math.pow(this.getDiscriminant(),0.5))/(2*a));
}
Scannerinput=newScanner(System.in);
doublex,y,z;
x=input.nextDouble();
y=input.nextDouble();
z=input.nextDouble();
QuadraticEquationqua=newQuadraticEquation(x,y,z);
该方程的两个根分别为:
+qua.getRoot1()+
"
+qua.getRoot2());
1-21
1.01.0
9.3(检验密码)一些网站设定了一些制定密码的规则。
编写一个方法,检验一个字符串是否是合法的密码。
假设密码规则如下:
1、密码必须至少有8个字符
2、密码职能包括字母和数字
3、密码必须至少有2个数字
编写一个程序,提示用户输入密码,如果该密码符合规则就显示“ValidPassword”,否则显示“InvilidPassword”。
publicclassTestPassword{
publicstaticintnumbersOfcount(Strings1)
intcount=0;
for(inti=0;
i<
s1.length();
i++)
{
if(Character.isDigit(s1.charAt(i)))
count++;
}
returncount;
publicstaticbooleantestword(Strings2)
booleanb=false;
s2.length();
if(Character.isDigit(s2.charAt(i))||
Character.isLetter(s2.charAt(i)))
b=true;
else
b=false;
publicstaticvoidtestPassword(Strings)
if(numbersOfcount(s)>
=2&
&
s.length()>
=8&
testword(s))
System.out.println("
ValidPassword!
InvalidPassword!
Enteryourpassword"
+
(includingwordsandnumbers)"
Stringpassword=input.nextLine();
testPassword(password);
Enteryourpassword(includingwordsandnumbers)
12abcddde
12332123
9.5使用下面的方法头编写一个方法,统计每个数字在字符串中出现的次数:
publicstaticvoidmain(String[]args)
这个方法统计每个数字在字符串中出现的次数。
返回值是10个元素构成的数组,每个元素存储的是一个数字出现的的次数。
例如,在执行完int[]count(“12203AB3”)之后,count[0]为1,count[1]为1,count[2]为2,count[3]为2.
编写一个测试程序,提示用户输入一个字符串,然后显示每个数字在字符串中出现的次数。
publicclassTestNumbersOfCount{
publicstaticint[]count(Strings)
int[]count=newint[10];
s.length();
if(Character.isDigit(s.charAt(i)))
count[s.charAt(i)-'
0'
]++;
EnteraString:
Strings1=input.nextLine();
count=count(s1);
count.length;
System.out.print("
count["
+i+"
]="
+count[i]+"
19921202abc
count[0]=1count[1]=2count[2]=3count[3]=0count[4]=0count[5]=0count[6]=0count[7]=0count[8]=0count[9]=2
9.12编写一个方法,检测两个单词是否互为变位词。
如果在不记顺序的情况下两个单词包含完全相同的字母,则称这两个单词互为变位词(anagr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 课后 习题
![提示](https://static.bdocx.com/images/bang_tan.gif)