Java书上及实验内容编程答案Word下载.docx
《Java书上及实验内容编程答案Word下载.docx》由会员分享,可在线阅读,更多相关《Java书上及实验内容编程答案Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
![Java书上及实验内容编程答案Word下载.docx](https://file1.bdocx.com/fileroot1/2022-11/25/c5c32d05-fd29-4419-8d10-daf66dba46f9/c5c32d05-fd29-4419-8d10-daf66dba46f91.gif)
j--;
}while(j>
=1);
sum=sum+k;
i++;
}while(i<
=100);
System.out.println("
1!
="
+sum);
//Rectangle类:
(第80面2)
publicclassRectangle{
doublewidth;
doublelength;
Rectangle(doublel,doublew)
{
check(w,l);
width=w;
length=l;
publicvoidcheck(doublew,doublel){
if(w<
=0||w>
50||l<
=0||l>
50)
System.out.print("
长或宽超出规定范围!
"
);
System.exit(0);
publicdoublegetArea(){
returnthis.length*this.width;
publicdoublegetPerimeter(){
return(this.length+this.width)*2;
publicvoidDraw(){
for(inti=1;
=this.width;
i++){
for(intj=1;
j<
=this.length;
j++){
if(i==1||i==this.width)
{
System.out.print("
*"
}
else{
if(j==1||j==this.length)
{
System.out.print("
}
else
"
\n"
//Test测试类:
publicstaticvoidmain(String[]args){
Rectanglerec=newRectangle(10,5);
面积:
+rec.getArea());
周长:
+rec.getPerimeter());
rec.Draw();
}//Area接口(第116面2)
publicinterfaceArea{
doubleCalsulateArea();
//MyCircle类
publicclassMyCircleimplementsArea{
doubleradius;
finaldoublePI=3.1415926;
publicMyCircle(doubler){
radius=r;
publicdoubleCalsulateArea(){
returnPI*radius*radius;
}//MyRectangle类
publicclassMyRectangleimplementsArea{
doublewidth,height;
publicMyRectangle(doublew,doubleh){
width=w;
height=h;
returnwidth*height;
}//Test测试类
MyRectanglem=newMyRectangle(5,6);
MyCirclem1=newMyCircle(10);
正方形m的面积:
+m.CalsulateArea()+"
\n圆形m1的面积:
+m1.CalsulateArea());
151页题3
inttable[]={4,1,3,5,9,2,1};
//初始化;
for(inti=0;
table.length;
System.out.print(table[i]+"
\t"
//未改变时输出;
System.out.print("
for(inti=0;
table.length/2;
i++)//进行变换;
inttemp;
temp=table[i];
table[i]=table[table.length-1-i];
table[table.length-1-i]=temp;
for(inti=0;
//改变后输出;
//(第213面)第2题
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.*;
publicclassFrame2extendsJFrameimplementsActionListener{
JButtonbtn1,btn2;
JTextAreatxt;
Frame2(){
this.setBounds(10,10,600,400);
this.setLayout(newFlowLayout(FlowLayout.CENTER));
btn1=newJButton("
按钮btn1"
btn1.addActionListener(this);
btn2=newJButton("
按钮btn2"
btn2.addActionListener(this);
txt=newJTextArea(1,15);
this.add(txt);
this.add(btn1);
this.add(btn2);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
publicstaticvoidmain(Stringargs[]){
newFrame2();
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(e.getSource()==btn1){txt.setText("
if(e.getSource()==btn2){txt.setText("
213页第三题太难了,,,六个Java程序,我就懒得复制了,需要的自己去看
实验1题3
importjava.util.Scanner;
publicclassQiugen{
publicstaticvoidmain(Stringargs[]){
System.out.println("
请依次输入a、b、c的值,每输入一个数按回车键确认:
Scannerreader=newScanner(System.in);
doublea=reader.nextDouble();
doubleb=reader.nextDouble();
doublec=reader.nextDouble();
Gongshi(a,b,c);
}
publicstaticvoidGongshi(doublea,doubleb,doublec){
if(a==0){
doublex;
if(b==0){
if(c==0){
这个一元二次多项式的根为任意实数。
}
else{
System.out.println("
这个一元二次多项式无实数解。
}
else{
x=-c/b;
这个一元二次多项式有一个实根为:
x="
+x);
doublex1,x2,y;
if(b*b-4*a*c==0){
x1=x2=-b/(2*a);
这个一元二次多项式有两个相等的实根为:
x1=x2="
+x1);
elseif(b*b-4*a*c>
0){
y=Math.sqrt(b*b-4*a*c);
x1=(-b+y)/(2*a);
x2=(-b-y)/(2*a);
这个一元二次多项式有两个不相等的实根为:
x1="
+x1+"
,x2="
+x2);
这个一元二次多项式方程无实数解。
实验3题2
实验4题4
classaccount
{
intleftmoney;
Stringid;
publicintgetleftmoney(){
returnleftmoney;
publicvoidsavemoney(doublemoney){
leftmoney+=money;
publicvoidgetmoney(doublemoney){
if(money<
=leftmoney)
leftmoney-=money;
else
只能取:
+leftmoney);
publicstaticvoidmain(String[]args)
accountaa=newaccount();
aa.leftmoney=0;
aa.id="
1234567899"
;
aa.savemoney(21000);
存入21000元后余额为:
+aa.getleftmoney());
aa.getmoney(11500);
11500元后余额为:
实验6题1
importjava.util.HashMap;
classBook{
publicStringname;
publicdoubleprice;
publicintnum;
publicstaticclassNum1{
publicstaticdoublegetSum(HashMap<
Integer,Book>
books){
doublesum=0;
for(inti=0;
i<
books.size();
i++){
Bookb=books.get(i);
sum+=b.price*b.num;
returnsum;
HashMap<
m=newHashMap<
();
输入5本书的名称、单价、购买数量:
5;
Bookb=newBook();
Scannera=newScanner(System.in);
b.name=a.nextLine();
b.price=a.nextDouble();
b.num=a.nextInt();
m.put(i,b);
doublesum=getSum(m);
sum="
实验9题3
的时刻是光标离开第一个文本框。
packagebag;
importjava.awt.*;
importjava.awt.event.*;
publicclassTextFieldtestextendsJFrame
publicstaticvoidmain(String[]args)
JFrameframe=newTextFieldtest();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextFieldfirst=newJTextField();
JTextFieldsecond=newJTextField();
JTextFieldresult=newJTextField();
intn,m;
TextFieldtest()
super("
TextFieldTest"
setBounds(250,200,400,200);
setLayout(newFlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
first=newJTextField("
inputinteger1"
second=newJTextField("
inputinteger2"
result=newJTextField("
GettheResult"
//添加鼠标事件,使鼠标离开first时候检查*/
first.addMouseListener(newMouseHander(first));
second.addMouseListener(newMouseHander(second));
result.addMouseListener(newMouseHander(result));
//添加鼠标事件,方便输入数据和计算结果
result.addMouseMotionListener(newMouseMotionHander());
second.addMouseMotionListener(newMouseMotionHander());
first.setColumns(30);
second.setColumns(30);
result.setColumns(30);
add(first);
add(second);
add(result);
privateclassMouseHanderextendsMouseAdapter
publicMouseHander(JTextFieldc)
current=c;
publicvoidmousePressed(MouseEventevent)
if(current==result)
doublefirstNumber=Double.parseDouble(first.getText());
doublesecondNumber=Double.parseDouble(second.getText());
doubleResult=firstNumber+secondNumber;
result.setText("
+Result);
elsecurrent.setText("
privateJTextFieldcurrent;
privateclassMouseMotionHanderextendsMouseMotionAdapter
publicvoidmouseMoved(MouseEventevent)
doublenumber=Double.parseDouble(first.getText());
if(number<
100||number>
200)
inttype=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(null,newString("
输入数字必须在100~200之间"
),"
提示"
2);