java程序设计教程(第六版)课后习题答案_第1页
java程序设计教程(第六版)课后习题答案_第2页
java程序设计教程(第六版)课后习题答案_第3页
java程序设计教程(第六版)课后习题答案_第4页
java程序设计教程(第六版)课后习题答案_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PP2.3publicclassfudian{publicstaticvoidmain(String[]args){floata=2.10f,b=3.70f;floatResultl,Result2,Result3;Resultl=a+b;Result2=a-b;Result3=a*b;System.out.println(nResultlis:"+Resultl);System.out.println(',Result2is:H+Result2);System.out.println("Result3is:"+Result3);))2.4publicclassTempConverter{publicstaticvoidmain(String[]args){finaliniBASE=32;finaldoubleCONVERSION_FACTOR=5.0/9.0;doublecelsiusTemp;intfahrenheitTemp=70;//valuetoconvertcelsiusTemp=(fahrenheitTemp-BASE)*CONVERSION_FACTOR;System.out.println("FahrenheitEquivalent:“+fahrenheitTemp);System.out.println("CelsiusTemperature:”+celsiusTemp);))2.5publicclassyinglizhuanqianmi{publicstaticvoidmain(String[]args){floatBase=1.60935f;floatQianmi;floatYingli=19.85f;Qianmi=Yingli*Base;System.out.println("YingLi:"+Yingli);System.out.println("QianMi:"+Qianmi);)}2.6publicclassTimeConverter1{publicstaticvoidmain(String[]args){intHour=5,Minute=35,Second=51;intSECONDS;SECONDS=Hour*60*60+Minute*60+Second;System.out.println(+Hour+"时"+Minute+"分"+Second+"秒”);System.out.println("换算成秒:"+SECONDS);))2.7publicclassTimeConverter2{publicstaticvoidmain(String[]args){intSECONDS=10853;intHour,Minute,Second;Second=SECONDS%60;Minute=(SECONDS-Second)%60;Hour=(SECONDS-Second-Minute*60)/3600;System.out.println(SECONDS+”秒,转化为");System.out.println(Hour+"时"+Minute+“分"+Second+“秒”);))2.9importjava.util.*;publicclassDollarbill{publicstaticvoidmain(String[]args){floatDollarl,Dollar2;intTen,Five,One,Quarters,Dimes,Nickles,Pennies;Scannerreader=newScanner(System.in);System.out.println("输入币值:");Dollarl=reader.nextFloat();Dollar2=Dollar1*100;Pennies=(int)Dollar2%5;Nickles=((int)Dollar2%10-Pennies)/5;Dimes=((int)Dollar2-Pennies-Nickles*5)%50/10;Quarters=(int)Dollar2%100/50;One=((int)Dollar2-Pennies-Nickles*5-Dimes*10-Quarters*5O)%5OO/l00;Five=(int)Dollar2%1000/500;Ten=(int)Dollar2/1000;System.out.println(Ten+"tendollarbills**);System.out.println(Five+"fivedollarbills1*);System.out.println(One+uonedollarbills1');System.out.println(Quarters4-"quartersdollarbills");System.out.println(Dimes+"dimesdollarbills**);System.out.println(Nickles+"nicklesdollarbills'1);System.out.println(Pennies+“penniesdollarbills!”);2.11importjava.util.*;publicclassFenshuzhuanhuan{publicstaticvoidmain(String[]args){intx,y;doubleResult=0;Scannerreader=newScanner(System.in);System.out.println("输入x:");x=reader.nextlnt();System.out.println("输入y:");y=reader.nextlnt();Result+=x/y;System.out.println("分数"+x+"/"+y);System.out.println("转换成小数是:"+Result);})2.16importjavax.swing.JApplet;importjava.awt.*;publicclassOlympiclogoextendsJApplet{publicvoidpaint(Graphicspage){page.setColor(Color.blue);page.drawOval(25,65,40,40);page.setColor(Color.yellow);page.drawOval(55,65,40,40);page.setColor(Color.black);page.drawOval(85,65,40,40);page.setColor(Color.green);page.drawOval(115,65,40,40);page.setColor(Color.red);page.drawOval(145,65,40,40);//circlepage.setColor(Color.cyan);page.drawString("OLYMPICLOGO';40,30);))2.19importjava.applet.*;importjava.awt.*;publicclassEx2_19extendsApplet)publicvoidpaint(Graphicspage){page.setColor(Color.BLACK);page.setFont(newFont("楷体:Fonl.ITALIC+Fonl.BOLD,30));page.drawString("林少锋”,40,30);page.setColor(Color.blue);page.setFont(newFont("宋体二Font.BOLD,30));page.drawString("林少锋*70,80);2.20importjava.applet.*;importjava.awt.*;publicclassEx2_20extendsApplet{publicvoidpaint(Graphicspage){page.drawOval(35,35,130,130);page.setColor(Color.red);page.fillArc(35,35,130,130,0,45);page.setColor(Color.blue);page.fillArc(35,35,130,130,45,45);page.setColor(Color.yellow);page.fillArc(35,35,130,130,90,45);page.setColor(ColoEcyan);page.fillArc(35,35,130,130,135,45);page.setColor(Color.gray);page.fillArc(35,35,130,130,180,45);page.setColor(Color.green);page.fillArc(35,35,130,130,225,45);page,setColor(Color.darkGray);page.fillArc(35,35,130,130,270,45);page.setColor(Color,pink);page.fillArc(35,35,130,130,315,45);})PP4.1方法1importjava.util.*;publicclassCreateSphere{*@paramargs列publicstaticvoidmain(StringtJargs){//TODO自动生成方法存根System.oul.piintln("请输入直径d:°);Scannerscan=newScanner(System.in);doubled=scan.nextDouble();SphereD=newSphere(d);D.Square();D.Volum();System.out.println(D.toStringO);publicclassSphere{finaldoublePI=3.14;doubleV,S;doubled;Sphere(doubled){this.d=d;publicvoidVolum(){V=(4/3)*PI*(d/2)*(d/2)*(d/2);)publicvoidSquare(){S=4*PI*(d/2)*(d/2);publicStringtoString(){Strings=,H,;Stringresult1=Double.toString(S);Stringresult2=Double.toString(V);s=("体积为:"+result2+”面积为:“result1);returns;方法2//Sphere,javapublicclassSphere{privatedoublediameter;publicSphere。{〃构造方法:无参数this,diameter=1.0;)publicSphere(doubled){〃构造方法:带一个参数this,diameter=d;)publicvoidsetDiameter(doubled){〃设置直径值的方法this.diameter=d;)publicdoublegetDiameter(){〃获取直径值的方法returnthis.diameter;)publicdoublevolume。{〃计算球的体积return4*Math.PI*Math.pow(this.diameter/2,3)/3;)publicdoublearea。{〃计算球的表面积return4*Math.PI*Math.pow(this.diameter/2,2);)publicStringtoStringO{Stringout=〃该球体的直径为:"+this,diameter+"\n"+〃该球体的表面积为:〃+this,area()+〃\n〃+“该球体的体积为:"+this,volume();returnout;))//MultiSphere,javaimportjava.util.Scanner;publicclassMultiSphere(publicstaticvoidmain(String[]args){Scannerscan=newScanner(System,in);Spheresphere1=newSphere();Spheresphere2=newSphere(3.5);System,out.printin(z,spherel:〃+sphere1+"\n");System.out.printlnC'sphere2:〃+sphere2+〃\n");System,out.println(z,spherel和sphere2分别调用无参构造方法〃+〃和带一个参数的构造方法进行初始化。〃);System,out.print("现在,请输入一个数作为球spherel的直径值:〃);spherel.setDiameter(scan.nextDoubleO);System,out.printin(〃\n〃+〃更改过的spherel:〃+spherel);//////////////////////////////////////////////////////////PP4.2方法1importjava.util.Scanner;publicclassCreateDog{/***@paramargs★/publicstaticvoidmain(String[]args){//TODO自动生成方法存根System,out.printIn(”请输入狗的年龄age:");Scannerscan=newScanner(System.in);intage=scan.nextlnt();System.out.printin("请输入狗的姓名name:");Scannerscanl=newScanner(System.in);Stringname=scanl.next();KennelAGE=newKennel(age);AGE.Age();System.out.printin(AGE.toString());System.out.printin("狗的名字是:"+name);}}publicclassKennel{finalintmul=7;intage;intagep;charname;Kennel(intage){this.age=age;=name;publicvoidAge(){agep=age*mul;)publicStringtoString(){Strings=nn;Stringrl=Integer.toString(age);Stringr2=Integer.toString(agep);s=("狗的年龄为:"+rl+”对应人的年龄为:"+r2);returns;方法2classDogl{Stringname;intage;publicDogl()(name=nheizin;age=l;)publicDogl(Stringn,inta)(name=n;age=a;)publicintDogAge(intAge)(intage=Age/7;return(age);}publicvoidsetName(Stringnamel){name=namel;publicStringgetName()return(name);publicStringtoString()Dogld=newDogl();return name+nfi^Wn+d.age+n^/。n);)}publicclassKennel{publicstaticvoidmain(String[]args){Dogldogl=newDogl();System.out.printIn(ndogl的name=M+);System,out.printIn("dogl的age=,,+dogl.age);Dogldog2=newDogl(nxiaobainz2);System,out.printIn(Hdog2的name=,,+);System,out.printIn("dog2的age=n+dog2.age);dogl,setName("diandian11);dogl,getName();System,out.printIn(ndogl的name=M+);intage=dogl.DogAge(35);System.out.printIn(nage=,,+age);Stringr=dogl.toString();System.out.printIn("r="+r);)}//////////////////////////////////////////////////////////PP4.4方法1publicclassBook{*@paramargs*/publicstaticvoidmain(String[]args){//TODO自动生成方法存根Stringinfo=null;Bookshelfbook;BookshelfINFO=newBookshelf(info);INFO.BOOKO;System.out.printIn(INFO.toString());publicclassBookshelf{Stringbookname;Stringauthor;Stringpublish;Stringdate;Stringbooks;Stringinfo;publicBookshelf(Stringinfo){bookname=nJAVA”;author=nqwen;publish=nTsingHUA";date=n19890322n;=info;}publicStringBOOK(){info=bookname+author+publish+date;returninfo;publicStringtoString(){Stringbookinfo;bookinfo="书名:n+bookname+n作者:M+author+n\nn;bookinfo+="出版社:"+publish+""+"出版日期:n+date;returnbookinfo;))方法2classBook(Stringtitle,author,publisher,copyright;Book(){title="JAVA编程语言设计”;author=nJohnLewisn;publisher=n电子工业出版社”;copyright=nxueshengn;Book(Stringt,Stringa,Stringp,Stringc){title=t;author=a;publisher=p;copyright=c;)voidsetBook(StringtlzStringal,Stringpl,Stringcl){title=tl;author=al;publisher=pl;copyright=cl;)StringgetBookTitle(){return(title);}StringgetBookAuthor()(return(author);)StringgetBookPublisher()(return(publisher);}StringgetBookCopyright()(return(copyright);}publicStringtoString()(Bookbo=newBook();return("这本名叫”+bo.书,作者是—.author+”出版社是H+bo.publisher+n版权归”+bo・copyright+”所有”);))publicclassBookshelf{publicstaticvoidmain(String[]args){Bookbook=newBook();System.out.printIn(book.title+n M+book.author+nn+book.publisher+nH+book.copyright);//TODOAuto-generatedmethodstubBookbookl=newBook("C语言程序设计","谭浩强","清华出版社","zuozhe");System.out.printIn(bookl.title+n n+bookl.author+nn+bookl.publisher+nn+bookl.copyright);book.setBook(nJAVA语言设计","WilliamLoftus",”西安电子出版社“,"sheng");book,getBookTitle();book・getBookAuthor();book,getBookPublisher();book,getBookCopyright();System.out.printin(book.title+n n+book.author+nn+book.publisher+nn+book.copyright);Stringstr=book.toString();System.out.print(nstr=n+str);//////////////////////////////////////////////////////////PP4.5classFlight{Stringname,origin,destination;intnumber;publicFlight()(name=”西安国际机场”;origin="西安”;destination="北京”;number=121;}publicFlight(StringnzStringozStringdzintnum){name=n;origin=o;destination=d;number=num;)publicvoidsetFlight(Stringnl,Stringol,Stringdl,intnuml){name=nl;origin=ol;destination=dl;number=numl;)publicStringgetName()returnname;)publicStringgetOrigin()(returnorigin;}publicStringgetDestination()(returndestination;}publiclonggetNumber(){returnnumber;}publicStringtoString()(Flightfl=newFlight();return("这是”+f1・name+”的”+f1・number+”航班,从"+f1・origin+”起飞至卜+f1,destination+"降落。”);publicclassFlightTest{publicstaticvoidmain(String[]args){Flightflight=newFlight();System.out.printIn("airlineFlight=n+flight,name);System,out.printin(**theflight1sorigin=**+flight.origin);System.out.printIn(ndestinationcitys=n+flight.destination);System,out.printIn(nflightnumber=,,+flight.number);Flightflightl=newFlight("天津航空“,“天津”,“西安“,321);System.out.printIn(nairlineFlight=n+);System.out.printIn(ntheflight*sorigin=M+flightl.origin);System.out.printIn(^destinationcitys=n+flightl.destination);System,out.printIn(nflightnumber=,,+flightl.number);flight.setFlight("北京航空","北京","西安",125);flight.getName();flight.getOrigin();flight.getDestination();flight.getNumber();System.out.printIn(nairlineFlight=n+);System.out.printIn(ntheflight*sorigin=M+flight.origin);System.out.printIn("destinationcitys=n+flight.destination);System,out.printIn(nflightnumber=,,+flight.number);Stringreal=flight.toString();System.out.printIn(real);))//////////////////////////////////////////////////////////PP4.7classPairOfDice{privatefinalintMAX=6;privateintfaceValue;privateintfaceValuel;publicPairOfDice(){faceValue=l;faceValuel=2;)publicintroll(){faceValue=(int)(Math.random()*MAX)+1;returnfaceValue;)publicintrolll(){faceValuel=(int)(Math.random()*MAX)+1;returnfaceValuel;)publicvoidsetFaceValue(intvalue)(faceValue=value;)publicintgetFaceValue()(returnfaceValue;}publicvoidsetFaceValue1(intvalue){faceValuel=value;}publicintgetFaceValuel()returnfaceValuel;publicintSum()intsum=faceValue+faceValuel;returnsum;)publicStringtoString()(Stringresult=Integer.toString(faceValue);returnresult;))publicclassRollingDice2{publicstaticvoidmain(String[]args){PairOfDicediel=newPairOfDice();intfaceValue=diel.roll();intfaceValuel=diel.rolll();System.out.printin(nfaceValue=n+faceValue+nfaceValuel=n+faceValuel);intsum=diel.Sum();System,out.printIn(,,sum=,,+sum);PairOfDicedie2=newPairOfDice();die2,setFaceValue(4);intfaceValue2=die2.getFaceValue();System,out.printIn(nfaceValue2=*,+faceValue2);die2,setFaceValue1(3);intfaceValue3=die2.getFaceValuel();System.out.printin(nfaceValue3=n+faceValue3);intsum2=die2.Sum();System.out.printin(nsum2=n+sum2);App4.13源程序://PushDisp1ayPane1.javaimportjava.awt.importjava.awt.event.*;importjavax.swing.*;publicclassPushDisplayPanelextendsJPanel(/****/privatestaticfinallongserialVersionUID=8617749572451960356L;privateintcount;privateJButtonpush;privateJLabellabel;intnum;int1=100;publicPushDisplayPanel()(push=newJButton(,zPushpush.addActionListener(newButtonListener());label=newJLabel("Random:"+count);add(push);add(label);setPreferredSize(newDimension(400,100));setBackground(Color,pink);)privateclassButtonListenerimplementsActionListener{publicvoidactionPerformed(ActionEventevent)(for(intj=0;j<I;j++){num=(int)(Math,random()*100)+1;label.setText("Random:"+num);//Random,javaimportjavax.swing.JFrame;publicclassRandom{publicstaticvoidmain(String[]args){JFrameframe=newJFrame("Random");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(newPushDisplayPanel());frame.pack();frame.setVisible(true);/////////////////////////////////////////////////////////////////////App4.14源程序://AddPanel.javaimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassAddPanelextendsJPanel{/****/privatestaticfinallongserialVersionUID=8617749572451960356L;privateintcount;privateJButtonpush,pushl;privateJLabellabel;intnum=50;publicAddPanel()(push=newJButton("Add!");pushl=newJButton("Minus!");push.addActionListener(newButtonListener());pushl.addActionListener(newButtonListener());label=newJLabel("Number:〃+num);add(push);add(pushl);add(label);setPreferredSize(newDimension(400,100));setBackground(Color,pink);)privateclassButtonListenerimplementsActionListenerpublicvoidactionPerformed(ActionEventevent)if(event.getSource()==push)num++;elsenum——;label.setText("Number:〃+num);//Number,javaimportjavax.swing.JFrame;publicclassNumber{/***@paramargs*/publicstaticvoidmain(String[]args){//TODO自动生成方法存根JFrameframe=newJFrame("Random");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(newAddPane1());frame.pack();frame.setVisible(true);/////////////////////////////////////////////////////////////补充:画一个转动的风车importjava.awt.*;importjava.awt.geom.importjavax.swing.*;publicclasstestarc(publicstaticvoidmain(String[]args)(FilIFrameframe=newFilIFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);classFilIFrameextendsJFramepublicFillFrame()(setTitleC'FillTest^);setSize(400,400);//addpaneltoframeDrawPane1panel=newDrawPane1();add(panel);classDrawPane1extendsJPanelimplementsRunnable{privateThreadt;privateintk=0;publicDrawPanel(){setBackground(Color,white);t=newThread(this);t.start();)publicvoidpaintComponent(Graphicsg)(super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;doublexCenter二getWidth()/2;〃中心点xdoubleyCenter二getHeight()/2;〃中心点ydoubleradius=(int)(getWidth()*O.4);〃半径Ellipse2D circle = newEllipse2D.Double(xCenter-radius,yCenter-radius,2*radius,2*radius);g2.draw(circle);〃画一个风车for(inti=0;i<4;i++)|Arc2D arcl=newArc2D.Double(xCenter-radius,yCenter-radius,2*radius,2*radius,-30+90*i+30*k,30,Arc2D.PIE);g2.setPaint(Color.RED);g2.fill(arcl);publicvoidrun()(fo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论