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

下载本文档

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

文档简介

pp2.3public class fudian public static void main(String args) float a=2.10f,b=3.70f; float Result1,Result2,Result3; Result1=a+b; Result2=a-b; Result3=a*b; System.out.println(Result1 is:+Result1); System.out.println(Result2 is:+Result2); System.out.println(Result3 is:+Result3);2.4public class TempConverter public static void main(String args) final int BASE = 32; final double CONVERSION_FACTOR = 5.0 / 9.0; double celsiusTemp; int fahrenheitTemp = 70; / value to convert celsiusTemp = (fahrenheitTemp - BASE)*CONVERSION_FACTOR; System.out.println (Fahrenheit Equivalent: + fahrenheitTemp); System.out.println (Celsius Temperature: + celsiusTemp);2.5public class yinglizhuanqianmi public static void main(String args) float Base=1.60935f;float Qianmi;float Yingli=19.85f;Qianmi=Yingli*Base;System.out.println (Ying Li: + Yingli); System.out.println (Qian Mi: +Qianmi);2.6public class TimeConverter1 public static void main(String args) int Hour=5,Minute=35,Second=51; int SECONDS; SECONDS=Hour*60*60+Minute*60+Second; System.out.println (+Hour+时+Minute+分+Second+秒); System.out.println (换算成秒: + SECONDS);2.7public class TimeConverter2 public static void main(String args) int SECONDS=10853; int Hour,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.9import java.util.*;public class Dollarbill public static void main(String args) float Dollar1,Dollar2; int Ten,Five,One,Quarters,Dimes,Nickles,Pennies; Scanner reader=new Scanner(System.in); System.out.println(输入币值:); Dollar1=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*50)%500/100; Five=(int)Dollar2%1000/500; Ten=(int)Dollar2/1000; System.out.println(Ten+ ten dollar bills); System.out.println(Five+ five dollar bills); System.out.println(One+ one dollar bills); System.out.println(Quarters+ quarters dollar bills); System.out.println(Dimes+ dimes dollar bills); System.out.println(Nickles+ nickles dollar bills); System.out.println(Pennies+ pennies dollar bills!);2.11import java.util.*;public class Fenshuzhuanhuan public static void main(String args) int x,y; double Result=0; Scanner reader=new Scanner(System.in); System.out.println(输入x:); x=reader.nextInt(); System.out.println(输入y:); y=reader.nextInt(); Result+=x/y; System.out.println (分数 +x+/+y); System.out.println (转换成小数是: + Result);2.16import javax.swing.JApplet;import java.awt.*;public class Olympiclogo extends JAppletpublic void paint (Graphics page) 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);/ circle page.setColor(Color.cyan); page.drawString (OLYMPIC LOGO, 40, 30); 2.19import java.applet.*;import java.awt.*;public class Ex2_19 extends Appletpublic void paint (Graphics page)page.setColor(Color.BLACK);page.setFont(new Font(楷体,Font.ITALIC+Font.BOLD,30); page.drawString (林少锋, 40, 30);page.setColor(Color.blue);page.setFont(new Font(宋体,Font.BOLD,30); page.drawString (林少锋, 70, 80);2.20import java.applet.*;import java.awt.*;public class Ex2_20 extends Appletpublic void paint (Graphics page)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(Color.cyan);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方法1import java.util.*;public class CreateSphere /* * param args */public static void main(String args) / TODO 自动生成方法存根System.out.println(请输入直径d:);Scanner scan=new Scanner(System.in);double d=scan.nextDouble(); Sphere D=new Sphere(d);D.Square();D.Volum();System.out.println(D.toString();public class Sphere final double PI=3.14; double V,S; double d; Sphere(double d) this.d=d; public void Volum()V=(4/3)*PI*(d/2)*(d/2)*(d/2);public void Square()S=4*PI*(d/2)*(d/2);public String toString()String s=;String result1=Double.toString(S);String result2=Double.toString(V);s=(体积为:+result2+面积为:+result1);return s;方法2/Sphere.java public class Sphere private double diameter; public Sphere()/构造方法:无参数 this.diameter = 1.0; public Sphere(double d) /构造方法:带一个参数 this.diameter = d; public void setDiameter(double d) /设置直径值的方法 this.diameter = d; public double getDiameter()/获取直径值的方法 return this.diameter; public double volume()/计算球的体积 return 4*Math.PI*Math.pow(this.diameter/2,3)/3; public double area()/计算球的表面积 return 4*Math.PI*Math.pow(this.diameter/2,2); public String toString() String out = 该球体的直径为: + this.diameter + n + 该球体的表面积为: + this.area() + n +该球体的体积为: + this. volume(); return out; /MultiSphere.javaimport java.util.Scanner;public class MultiSphere public static void main(String args) Scanner scan = new Scanner(System.in); Sphere sphere1 = new Sphere(); Sphere sphere2 = new Sphere(3.5); System.out.println(sphere1: + sphere1 + n); System.out.println(sphere2: + sphere2 + n); System.out.println(sphere1和sphere2分别调用无参构造方法 + 和带一个参数的构造方法进行初始化。); System.out.print(现在,请输入一个数作为球sphere1的直径值:); sphere1.setDiameter(scan.nextDouble(); System.out.println(n + 更改过的sphere1: + sphere1); /PP 4.2方法1import java.util.Scanner;public class CreateDog /* * param args */public static void main(String args) / TODO 自动生成方法存根System.out.println(请输入狗的年龄age:);Scanner scan=new Scanner(System.in);int age=scan.nextInt();System.out.println(请输入狗的姓名name:);Scanner scan1=new Scanner(System.in);String name=scan1.next(); Kennel AGE=new Kennel(age);AGE.Age();System.out.println(AGE.toString();System.out.println(狗的名字是: +name);public class Kennelfinal int mul=7;int age;int agep;char name;Kennel(int age)this.age=age;=name;public void Age()agep=age*mul;public String toString()String s=;String r1=Integer.toString(age);String r2=Integer.toString(agep);s=(狗的年龄为:+r1+ 对应人的年龄为:+r2);return s;方法2class Dog1String name; int age; public Dog1() name=heizi; age=1; public Dog1(String n, int a ) name=n; age=a; public int DogAge(int Age) int age=Age/7; return(age); public void setName(String name1) name=name1; public String getName() return(name); public String toString() Dog1 d=new Dog1();return(这只狗叫++已经有+d.age+岁了。); public class Kennel public static void main(String args) Dog1 dog1=new Dog1();System.out.println(dog1的 name=+);System.out.println(dog1的 age=+dog1.age);Dog1 dog2=new Dog1(xiaobai,2);System.out.println(dog2的 name=+);System.out.println(dog2的age=+dog2.age);dog1.setName(diandian);dog1.getName();System.out.println(dog1的 name=+);int age=dog1.DogAge(35);System.out.println(age=+age);String r=dog1.toString();System.out.println(r=+r);/PP 4.4方法1public class Book /* * param args */public static void main(String args) / TODO 自动生成方法存根String info = null;Bookshelf book;Bookshelf INFO=new Bookshelf(info);INFO.BOOK();System.out.println(INFO.toString();public class Bookshelf String bookname;String author;String publish;String date;String books;String info;public Bookshelf(String info)bookname=JAVA;author=qwe;publish=Tsing HUA;date=19890322;=info;public String BOOK()info=bookname+author+publish+date;return info;public String toString()String bookinfo;bookinfo=书名:+bookname+ 作者:+author+n;bookinfo+=出版社:+publish+ +出版日期:+date;return bookinfo;方法2class Book String title,author,publisher,copyright; Book() title=JAVA 编程语言设计; author=John Lewis; publisher=电子工业出版社; copyright=xuesheng; Book(String t,String a,String p,String c) title= t; author=a; publisher=p; copyright=c; void setBook(String t1,String a1,String p1,String c1) title= t1; author=a1; publisher=p1; copyright=c1; String getBookTitle() return(title); String getBookAuthor() return(author);String getBookPublisher() return(publisher); String getBookCopyright() return(copyright); public String toString() Book bo=new Book();return (这本名叫+bo.title+ 书,作者是+bo.author+ 出版社是+bo.publisher+ 版权归+bo.copyright+所有); public class Bookshelf public static void main(String args) Book book=new Book();System.out.println(book.title+ +book.author+ +book.publisher+ +book.copyright);/ TODO Auto-generated method stub Book book1=new Book(C语言程序设计,谭浩强,清华出版社,zuozhe); System.out.println(book1.title+ +book1.author+ +book1.publisher+ +book1.copyright); book.setBook(JAVA 语言设计,William Loftus,西安电子出版社,sheng); book.getBookTitle(); book.getBookAuthor(); book.getBookPublisher(); book.getBookCopyright(); System.out.println(book.title+ +book.author+ +book.publisher+ +book.copyright); String str=book.toString(); System.out.print(str=+str);/PP4.5class FlightString name, origin,destination;int number; public Flight()name=西安国际机场;origin=西安;destination=北京;number=121; public Flight(String n,String o,String d,int num) name=n;origin=o;destination=d;number=num; public void setFlight(String n1,String o1,String d1,int num1) name=n1; origin=o1; destination=d1; number=num1; public String getName() return name; public String getOrigin() return origin; public String getDestination() return destination; public long getNumber() return number; public String toString() Flight fl=new Flight();return (这是++的+fl.number+航班,从+fl.origin+起飞到+fl.destination+降落。); public class FlightTest public static void main(String args) Flight flight=new Flight(); System.out.println(airline Flight=+); System.out.println(the flights origin=+flight.origin); System.out.println(destination citys=+flight.destination); System.out.println(flight number=+flight.number); Flight flight1=new Flight(天津航空,天津,西安,321); System.out.println(airline Flight=+); System.out.println(the flights origin=+flight1.origin); System.out.println(destination citys=+flight1.destination); System.out.println(flight number=+flight1.number); flight.setFlight(北京航空,北京,西安,125); flight.getName(); flight.getOrigin(); flight.getDestination(); flight.getNumber(); System.out.println(airline Flight=+); System.out.println(the flights origin=+flight.origin); System.out.println(destination citys=+flight.destination); System.out.println(flight number=+flight.number); String real=flight.toString(); System.out.println(real);/PP4.7class PairOfDiceprivate final int MAX=6;private int faceValue;private int faceValue1;public PairOfDice()faceValue=1;faceValue1=2;public int roll()faceValue=(int)(Math.random()*MAX)+1;return faceValue;public int roll1()faceValue1=(int)(Math.random()*MAX)+1;return faceValue1;public void setFaceValue(int value)faceValue=value;public int getFaceValue()return faceValue;public void setFaceValue1(int value)faceValue1=value;public int getFaceValue1()return faceValue1;public int Sum() int sum=faceValue+faceValue1; return sum;public String toString()String result=Integer.toString(faceValue);return result;public class RollingDice2 public static void main(String args) PairOfDice die1=new PairOfDice(); int faceValue=die1.roll();int faceValue1=die1.roll1();System.out.println(faceValue=+faceValue+ faceValue1=+faceValue1);int sum=die1.Sum();System.out.println(sum=+sum);PairOfDice die2=new PairOfDice();die2.setFaceValue(4);int faceValue2=die2.getFaceValue();System.out.println(faceValue2=+faceValue2);die2.setFaceValue1(3);int faceValue3=die2.getFaceValue1();System.out.println(faceValue3=+faceValue3); int sum2=die2.Sum();System.out.println(sum2=+sum2);App4.13源程序:/PushDisplayPanel.javaimport java.awt.*;import java.awt.event.*;importjavax.swing.*;public class PushDisplayPanel extends JPanel /* * */private static final long serialVersionUID = 8617749572451960356L;private int count;private JButton push;private JLabel label;intnum;int I=100;public PushDisplayPanel()push=new JButton(Push it!);push.addActionListener(new ButtonListener();label=newJLabel(Random:+count);add (push);add (label);setPreferredSize(new Dimension(400,100);setBackground(Color.pink);private classButtonListenerimplementsActionListenerpublicvoidactionPerformed(ActionEvent event)for(int j=0;jI;j+)num=(int)(Math.random()*100)+1;label.setText(Random:+num);/Random.javaimportjavax.swing.JFrame;public class Random public static void main(String args) JFrame frame=new JFrame(Random);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().add(new PushDisplayPanel();frame.pack();frame.setVisible(true);/App4.14源程序:/AddPanel.javaimport java.awt.*;import java.awt.event.*;importjavax.swing.*;public class AddPanel extends JPanel /* * */private static final long serialVersionUID = 8617749572451960356L;private int count;private JButton push,push1;private JLabel label;intnum=50;public AddPanel()push=new JButton(Add!);push1=new JButton(Minus!);push.addActionListener(new ButtonListener();push1.addActionListener(new ButtonListener();label=newJLabel(Number:+num);add (push);add (push1);add (label);setPreferredSize(new Dimension(400,100);setBackground(Color.pink);private classButtonListenerimplementsActionListenerpublicvoidactionPerformed(ActionEvent event)if(event.getSource()=push)num+;elsenum-;label.setText(Number:+num);/Number.javaimport javax.swing.JFrame;public class Number/* * param args */public static void main(String args) / TODO 自动生成方法存根

温馨提示

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

评论

0/150

提交评论