java语言程序设计基础篇第十版第十四章练习答案
java语言程序设计基础篇第十版课后答案
![java语言程序设计基础篇第十版课后答案](https://img.taocdn.com/s3/m/8fb5f94e16fc700abb68fcd9.png)
第一章1.1public class Test{public static void main(String[]args){System.out.println("Welcome to Java!"); System.out.println("Welcome to Computer Science!");System.out.println("Progr amming is fun.");}}1.2public class Test{public static void main(String[]args){for(int i=0;i<=4;i++){System.out.println("Welcome to Java!");}}}1.3public class Test{public static void main(String[]args){System.out.println("]");System.out.printl n("]");System.out.println("]]");System.out.println("]]");}}public class Test{public static void main(String[]args){System.out.println("A"); System.out.println("A A");System.out.println("AAAAA");System.out.println("A A");}}public class Test{public static void main(String[]args){System.out.println("V V");System.out.println("V V");System.out.println("V V");System.out.println(" V");}}1.4public class Test{public static void main(String[]args){System.out.println("a a^2a^3");System.out.println("111");System.out.println("248");System.out.println("3 927");System.out.println("41664");}}1.5public class Test{public static void main(String[]args){System.out.println((9.5*4.5-2.5*3)/(45.5-3.5) );}}1.6public class Test{public static void main(String[]args){int i=1,sum=0;for(;i<=9;i++)sum+ =i;System.out.println(sum);}1.7public class Test{public static void main(String[]args){System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11));System.out.println(4*(1.0-1.0/3+1.0/5-1.0/7+1.0/9-1.0/11+1.0/13));}}1.8public class Test{public static void main(String[]args){final double PI=3.14; double radius=5.5;System.out.println(2*radius*PI);System.out.println(PI*radius*radius);}}1.9public class Test{public static void main(String[]args){System.out.println(7.9*4.5);System.out.p rintln(2*(7.9+4.5));}}1.10public class Test{public static void main(String[]args){double S=14/1.6;double T=45*60+30;double speed=S/T;System.out.println(speed);}1.11public class Test{public static void main(String[]args){int BN=312032486; //original person numbers double EveryYS,EveryYBP,EveryYDP,EveryYMP;EveryY S=365*24*60*60;EveryYBP=EveryYS/7;EveryYDP=EveryYS/13;Every YMP=EveryYS/45;int FirstYP,SecondYP,ThirdYP,FourthYP,FivthYP;FirstYP=(int)(BN+EveryYBP+EveryYMP-EveryYDP);SecondYP=(int)(FirstYP +EveryYBP+EveryYMP-EveryYDP);ThirdYP=(int)(SecondYP+EveryYBP+Ev eryYMP-EveryYDP);FourthYP=(int)(ThirdYP+EveryYBP+EveryYMP-EveryYD P);FivthYP=(int)(FourthYP+EveryYBP+EveryYMP-EveryYDP);System.out.pri ntln(FirstYP);System.out.println(SecondYP);System.out.println(ThirdYP);Syste m.out.println(FourthYP);System.out.println(FivthYP);}}1.12public class Test{public static void main(String[]args){double S=24*1.6; double T=(1*60+40)*60+35;double speed=S/T;System.out.println(sp eed);}}1.13import java.util.Scanner;public class Test{public static void main(String[]args){Scanner input=new Scan ner(System.in);System.out.println("input a,b,c,d,e,f value please:");double a=input.nextDouble();double b=input.nextDouble();double c=input.nextDouble();double d=input. nextDouble();double e=input.nextDouble();第二章package cn.Testcx;import java.util.Scanner;public class lesson2{public static void main(String[]args){@SuppressWarnings("resource")Scanner in put=new Scanner(System.in);System.out.print("请输入一个摄氏温度:");double Celsius=input.nextDouble();double Fahrenheit=(9.0/5)*Celsius+3 2;System.out.println("摄氏温度:"+Celsius+"度"+"转换成华氏温度为:"+Fahrenheit+"度");System.out.print("请输入圆柱的半径和高:");double radius=input.nextDouble();int higth=input.nextInt();double are as=radius*radius*Math.PI;double volume=areas*higth;System.out.println("圆柱体的面积为:"+areas);System.out.println("圆柱体的体积为:"+volume);System.out.print("输入英尺数:");double feet=input.nextDouble();double meters=feet*0.305;System.out.print ln(feet+"英尺转换成米:"+meters);System.out.print("输入一个磅数:");double pounds=input.nextDouble();double kilograms=pounds*0.454;Syste m.out.println(pounds+"磅转换成千克为:"+kilograms);System.out.println("输入分钟数:");long minutes=input.nextInt();long years=minutes/(24*60*365);long days=(minutes%(24*60*365))/(24*60);System.out.println(minutes+"分钟"+"有"+years+"年和"+days+"天");long totalCurrentTimeMillis=System.currentTimeMillis();long totalSeconds=t otalCurrentTimeMillis/1000;long currentSeconds=totalSeconds%60;long totalM inutes=totalSeconds/60;long currentMinutes=(totalSeconds%(60*60))/60;long currenthours=(totalMinutes/60)%24;System.out.print("输入时区偏移量:");byte zoneOffset=input.nextByte();long currentHour=(currenthours+(zoneOf fset*1))%24;System.out.println("当期时区的时间为:"+currentHour+"时"+currentMinutes+"分"+currentSeconds+"秒");System.out.print("请输入v0,v1,t:");double v0=input.nextDouble();double v1=input.nextDouble();doublet=input.nextDouble();float a=(float)((v1-v0)/t);System.out.println("平均加速度a="+a);System.out.println("输入水的重量、初始温度、最终温度:");double water=input.nextDouble();double initialTemperature=input.nextDou ble();double finalTemperature=input.nextDouble();double Q=water*(finalTemp erature-initialTemperature)*4184;System.out.println("所需热量为:"+Q);System.out.print("输入年数:");int numbers=input.nextInt();long oneYearsSecond=365*24*60*60;Longpop ulation=(long)((312032486+((oneYearsSecond/7.0)+(oneYearsSecond/45.0)-(oneYearsSecond/13.0))*numbers));System.out.println("第"+numbers+"年后人口总数为:"+population);System.out.print("输入速度单位m/s和加速度a单位m/s2:");double v=input.nextDouble();double a1=input.nextDouble();double l engthOfAirplane=(Math.pow(v,2))/(2*a1);System.out.println("最短长度为:"+lengthOfAirplane);System.out.print("输入存入的钱:");double money=input.nextInt();double monthRate=5.0/1200;for(int i=1;i<7; i++){double total=money*(Math.pow(1+monthRate,i));System.out.println("第"+i+"个月的钱为:"+total);//告诉我书上的银行在哪里,我要去存钱,半年本金直接翻6倍、、、}System.out.print("用户请输入身高(英寸)、体重(磅):");double height=input.nextDouble();double weight=input.nextDouble(); double BMI=(weight*0.45359237)/(Math.pow((height*0.0254),2));System.out.println("BMI的值为"+BMI);System.out.print("输入x1和y1:");System.out.print("输入x2和y2:");double x1=input.nextDouble();double y1=input.nextDouble();double x2 =input.nextDouble();double y2=input.nextDouble();double point1=Math.pow((x2-x1),2);double point2=Math.pow((y2-y1),2);double distance=Math.pow((point1+point2),(1.0/2));//也可以Math.pow((point1+point2),0.5)System.out.println("两点间的距离为:"+distance);System.out.print("输入六边形的边长:");double side=input.nextDouble();double area=(3*(Math.pow(3,0.5))*(Math.p ow(side,2)))/2;System.out.println("六边形的面积为:"+area);}}。
JAVA各章习题及答案
![JAVA各章习题及答案](https://img.taocdn.com/s3/m/6566fbfc6f1aff00bed51e9e.png)
三、编程题 1.编写一个 Java Application 类型的程序,输出“This is my first Java Application! ” 。 【 编 程 分 析 】 要 编 写 Java Application 类 型 的 程 序 , 需 要 在 JCreator 中 创 建 一 个 Java Application 类型的工程,这时 JCreator 会自动创建程序框架,该框架包含一个公共类,其中 包含一个 main()方法。删除 main()方法中自动生成的代码,编写自己的代码即可。由于该程 序要求输出一句话,因此在 main() 方法中直接调用 System.out.println() 或 System.out.print() 方法即可实现。 【参考程序】 public class X3_1 { public static void main(String args[]){ System.out.println("This is my first Java Application!"); } } 【运行结果】 This is my first Java Application! 2.编写一个 Java Applet 类型的程序,输出“这是我的第一个 Java Applet 类型的程序” 。 【编程分析】 要编写一个 Java Applet 类型的程序,首先利用 JCreator 创建一个 Java Applet 类型的工程, 该工程自动创建两个文件,一个源文件(.java 文件)和一个 HTML 文件(.html 文件) ,源文件中包含一个公共类,其中包含两个方法,即 init()方法和 paint()方法,由于该 程序只是要求输出一句话, 因此只要将 paint()方法中 g.drawString()方法的第一个参数修改为 要输出的内容即可。如果不是利用 JCreator 开发工具,而是利用 J2SDK,则用户需要自己编 写 HTML 文件。 【参考程序】 import java.awt.*; import java.applet.*; public class X3_2 extends Applet { public void paint(Graphics g) { g.drawString("这是我的第一个 Java Applet 类型的程序",50,50); } } 【运行结果】
java语言程序设计课后习题答案
![java语言程序设计课后习题答案](https://img.taocdn.com/s3/m/3f4364f132d4b14e852458fb770bf78a65293abe.png)
java语言程序设计课后习题答案第一章:计算机、程序和JAVA概述1、2、1什么是硬件和软件?答:硬件指计算机中可见的物理部分;而软件提供不可见的指令,这些指令控制硬件并使硬件完成特定的任务。
1、2、2列举计算机的5个主要硬件组件。
答:中央处理器(CPU);内存(主存);存储设备(磁盘、光盘);输入设备(鼠标、键盘);输出设备(显示器、打印机);通信设备(调制解调器、网卡)。
1、2、3编写“CPU”代表什么含义?测量CPU速度的单位是什么?答:CPU(Central Proceing Unit)中央处理单元,包括控制单元和算术、逻辑单元;单位是HZ,现在通常以MHZ,GHZ数量级衡量。
1、2、4什么是比特?什么是字节?答:bit是计算机物理设备中存储的最小单位;8个bit为1个byte。
1、2、5内存是用来做什么的?RAM代表什么?为什么内存成为RAM?答:内存用来存储程序和数据;RAM(Random-Acce Memory)-可随机访问存储介质;因为内存可以按任意顺序存取字节所以被称为RAM(按功能划分)。
1、2、6用于测量内存大小的单位是什么?用于测量磁盘大小的单位是什么?答:GB,TB1、2、7内存和永久存储设备的主要不同是什么?答:内存是易失性存储介质(断电即失),存储容量小,传输速度快;永久存储设备为非易失性存储介质(断电可留),存储容量大,传输速度慢。
1、3、1CPU能理解什么语言。
机器语言。
1、3、2什么是汇编语言?什么是汇编器?汇编语言能用短的描述性单词来表示每一条机器语言指令,是一种低级语言。
汇编器可以将汇编语言转换成机器语言。
1、3、3什么是高级编程语言?什么是源程序?很像英语,易于学习和使用的编程语言称为高级编程语言。
使用高级编程语言编写的程序称为源程序。
1、3、4什么是解释器?什么是编译器?解释器会逐条读取源代码中的语言,并立刻翻译成机器代码或者虚拟机器代码,然后立刻运行。
Java语言程序设计第14章习题参考答案.docx
![Java语言程序设计第14章习题参考答案.docx](https://img.taocdn.com/s3/m/933084fa988fcc22bcd126fff705cc1755275f73.png)
习题十四参考答案1.什么是Web,其工作原理如何?答:Web是一种浏览器/服务器技术。
Web服务器分布在世界各地,存储着各种各样的信息。
Web的浏览器用来请求任何服务器上的信息,并负责显示这些信息。
Web工作原理:(1)用户在浏览器中输入要访问网页的URL地址(如:),向服务器发送浏览请求。
(2)Web服务器接收到请求后,把响应结果返回到浏览器,浏览器解析HTML,把内内容呈现给用户。
(3)通信完成,关闭连接。
2.JSP的工作原理是什么?答:JSP的工作过程如下:(1)当一个JSP页面第一次被请求或在上次调用后被修改过,服务器首先会把JSP页面转换成Servleto(2)在转换的过程中,若JSP页面存在语法错误,转换会被终止,并向服务器和客户端输出错误信息。
如果转换成功,转换后的Servlet会被编译成相应的类文件。
服务器加载该类文件并允许其实例完成需要的工作,把响应返回客户端浏览器。
(3)如果被请求的JSP文件在上次调用后没有被修改过,服务器直接调用已经转换编译过的类文件实例完成工作,把响应返回客户端浏览器。
(4)Sen-let被处理完毕以后,其生命周期结束,被Java虚拟机的垃圾回收器回收。
3.JSP的构成元素有哪些?答:一个JSP页面由模板元素和JSP元素构成。
模板元素指的是JSP引擎不处理的部分, 比如HTML. JavaScript和CSS等。
JSP元素指的是由JSP引擎直接处理的部分,这一部分必须符合JSP 语法,否则会导致编译错误。
JSP元素包括注释、指令元素、脚本元素和动作元素。
4.什么是内置对象? JSP的内置对象有哪些,各有何功能?答:所谓JSP内置对象就是町以不加声明就在JSP页面脚本中使用的成员变量。
卜面介绍常见的内置对象。
常用的内置对象有:(I)request对象。
该对象封装了用户提交的信息,通过调用该对象相应的方法可以获取封装的信息,即使用该对象可以获取用户提交信息。
Java程序设计各章习题及其答案
![Java程序设计各章习题及其答案](https://img.taocdn.com/s3/m/082fa611366baf1ffc4ffe4733687e21ae45ff5c.png)
Java程序设计各章习题及其答案第一章习题及思考题1、Java程序是由什么组成的?一个程序中必须有public类吗?Java源文件的命名规则是怎样的?答:一个Java源程序是由若干个类组成。
一个Java程序不一定需要有public类:如果源文件中有多个类时,则只能有一个类是public 类;如果源文件中只有一个类,则不将该类写成public也将默认它为主类。
源文件命名时要求源文件主名应与主类(即用public修饰的类)的类名相同,扩展名为.java。
如果没有定义public类,则可以任何一个类名为主文件名,当然这是不主张的,因为它将无法进行被继承使用。
另外,对Applet小应用程序来说,其主类必须为public,否则虽然在一些编译编译平台下可以通过(在BlueJ下无法通过)但运行时无法显示结果。
2、怎样区分应用程序和小应用程序?应用程序的主类和小应用程序的主类必须用public修饰吗?答:Java Application是完整的程序,需要独立的解释器来解释运行;而Java Applet则是嵌在HTML编写的Web页面中的非独立运行程序,由Web浏览器内部包含的Java解释器来解释运行。
在源程序代码中两者的主要区别是:任何一个Java Application 应用程序必须有且只有一个main方法,它是整个程序的入口方法;任何一个Applet小应用程序要求程序中有且必须有一个类是系统类Applet的子类,即该类头部分以extends Applet结尾。
应用程序的主类当源文件中只有一个类时不必用public修饰,但当有多于一个类时则主类必须用public修饰。
小应用程序的主类在任何时候都需要用public来修饰。
3、开发与运行Java程序需要经过哪些主要步骤和过程?答:主要有三个步骤(1)、用文字编辑器notepad(或在Jcreator,Gel, BuleJ,Eclipse, Jbuilder等)编写源文件;(2)、使用Java编译器(如Javac.exe)将.java源文件编译成字节码文件.class;(3)、运行Java程序:对应用程序应通过Java解释器(如java.exe)来运行,而对小应用程序应通过支持Java标准的浏览器(如Microsoft Explorer)来解释运行。
Java程序设计 张永强 张墨华 第14章习题答案
![Java程序设计 张永强 张墨华 第14章习题答案](https://img.taocdn.com/s3/m/715a13dd360cba1aa811da31.png)
第 14 章 网络编程请分析对比 TCP 协议与 UDP 协议的特点,以及它们通讯流程的区别。
参考 14.1.2 2. 解释在基于 TCP 的网络编程时 ServerSocket 和 Socket 的作用。
ServerSocket 实现了服务器套结字,它的实例需要注册到主机系统,并提供面向客户 端的请求侦听服务。
而 Socket 实现客户端套接字,基于 TCP 网络编程时的网络客户端可以 利用一个 Socket 实例和服务器端的 Socket 实例进行通信。
3. 解释在基于 UDP 的网络编程时 DatagramSocket 和 DatagramPacket 的作用。
DatagramSocket 表示用来发送和接收数据报包的套接字,数据报套接字是包投递服务 的发送或接收点。
DatagramPacket 数据报包用来实现无连接包投递服务。
4. 如果要捕捉端口重复使用的异常,应该使用哪个异常类? BindException 用来将套接字绑定到本地地址和端口时发生错误的情况下,抛出此异 常。
5. 对于对象的传输,在编程的时候要注意哪些问题?传输基础对象和非基础对象时,程 序编写有什么不同? 一个对象能否传输取决于该对象是否能够被串行化。
对于 JDK 提供的如 Integer、 String、Date 等类型的对象,由于已经被串行化,因此可以被直接传输,而使用用户自定 义的类,如果需要进行串行化传输,则该类要实现 Serializable 接口,并且保证该类的每 个要传输的属性其类型必须也是可串行化。
同时,由于 Socket 的通信是建立在双方的同步基础之上的,因此在进行非基础对象的传输 时,需要保证通信两端构造流的顺序必须按照相反的顺序构造。
6. 程序 14-6 给出了客户端的对象传输程序,请给出服务器端的改造程序,并调试运行。
1.package chap14.session4; public class Account { private String id; // 用户唯一的 id private String name;// 用户名称 private int balance;// 当前余额 public Account(String id, String name, int balance) { super(); this.id = id; = name;this.balance = balance; } public Account(String id, String name) { super(); this.id = id; = name; this.balance = 0; } // 这里省略了每个属性的 getter 和 setter 方法,请创建时自行添加。
java语言程序设计基础篇第十版第十四章练习答案
![java语言程序设计基础篇第十版第十四章练习答案](https://img.taocdn.com/s3/m/5ddf1197284ac850ad02424b.png)
}
}
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Exercise14_02"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
02Байду номын сангаас
public class Exercise14_02 extends Application {
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}}
05
public class Exercise14_05 extends Application {
for (int i = 0; i < 5; i++) {
Text txt = new Text("Java");
txt.setRotate(90);
txt.setFont(font);
txt.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));
java语言程序设计基础篇第十版第十四章练习答案
![java语言程序设计基础篇第十版第十四章练习答案](https://img.taocdn.com/s3/m/cfb38cfe6e1aff00bed5b9f3f90f76c661374c96.png)
java语言程序设计基础篇第十版第十四章练习答案01public class Exercise14_01 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);ImageView imageView1 = new ImageView("image/uk.gif");ImageView imageView2 = new ImageView("image/ca.gif");ImageView imageView3 = new ImageView("image/china.gif");ImageView imageView4 = new ImageView("image/us.gif");pane.add(imageView1, 0, 0);pane.add(imageView2, 1, 0);pane.add(imageView3, 0, 1);pane.add(imageView4, 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_01"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}02public class Exercise14_02 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Image imageX = new Image("image/x.gif");Image imageO = new Image("image/o.gif");GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {int status = (int)(Math.random() * 3);if (status == 0) {pane.add(new ImageView(imageX), j, i);}else if (status == 1) {pane.add(new ImageView(imageO), j, i);}}}// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_02"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}* The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}03public class Exercise14_03 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {// There are two ways for shuffling. One is to use the hint in the book. ArrayList list = new ArrayList<>();for (int i = 1; i <= 52; i++) {list.add(i);}HBox pane = new HBox(5);pane.setAlignment(Pos.CENTER);pane.getChildren().add(new ImageView("image/card/" + list.get(0) + ".png")); pane.getChildren().add(new ImageView("image/card/" + list.get(1) + ".png")); pane.getChildren().add(new ImageView("image/card/" + list.get(2) + ".png")); // Create a scene and place it in the stage Scene scene = new Scene(pane);primaryStage.setTitle("Exercise14_03"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}04public class Exercise14_04 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {HBox pane = new HBox();pane.setAlignment(Pos.CENTER);Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.ITALIC, 22);for (int i = 0; i < 5; i++) {Text txt = new Text("Java");txt.setRotate(90);txt.setFont(font);txt.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 100);primaryStage.setTitle("Exercise14_04"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}05public class Exercise14_05 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length(); Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}05public class Exercise14_05 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length(); Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}06public class Exercise14_06 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;Pane pane = new Pane();for (int i = 0; i < 8; i++) {boolean isWhite = i % 2 == 0;for (int j = 0; j < 8; j++) {Rectangle rectangle = new Rectangle(i * WIDTH / 8,j * HEIGHT / 8, WIDTH / 8, HEIGHT / 8);rectangle.setStroke(Color.BLACK);if (isWhite) {rectangle.setFill(Color.WHITE);}else {rectangle.setFill(Color.BLACK);}isWhite = !isWhite;pane.getChildren().add(rectangle);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_06"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}07public class Exercise14_07 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;GridPane pane = new GridPane();for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {TextField tf = new TextField((int)(Math.random() + 0.5) + "");tf.setPrefColumnCount(1);tf.setAlignment(Pos.CENTER);pane.add(tf, j, i);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_07"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}08public class Exercise14_08 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {GridPane pane = new GridPane();for (int i = 0; i < 6; i++) {for (int j = 0; j < 9; j++) {pane.add(new ImageView("image/card/" + (i * 6 + j + 1) + ".png"), j, i); }}// Create a scene and place it in the stageScene scene = new Scene(pane, 600, 600);primaryStage.setTitle("Exercise14_08"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}09public class Exercise14_09 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.add(new FanPane(), 0, 0);pane.add(new FanPane(), 1, 0);pane.add(new FanPane(), 0, 1);pane.add(new FanPane(), 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_09"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}class FanPane extends Pane {double radius = 50;public FanPane() {Circle circle = new Circle(60, 60, radius);circle.setStroke(Color.BLACK);circle.setFill(Color.WHITE);getChildren().add(circle);Arc arc1 = new Arc(60, 60, 40, 40, 30, 35);arc1.setFill(Color.RED); // Set fill colorarc1.setType(ArcType.ROUND);Arc arc2 = new Arc(60, 60, 40, 40, 30 + 90, 35);arc2.setFill(Color.RED); // Set fill colorarc2.setType(ArcType.ROUND);Arc arc3 = new Arc(60, 60, 40, 40, 30 + 180, 35);arc3.setFill(Color.RED); // Set fill colorarc3.setType(ArcType.ROUND);Arc arc4 = new Arc(60, 60, 40, 40, 30 + 270, 35);arc4.setFill(Color.RED); // Set fill colorarc4.setType(ArcType.ROUND);getChildren().addAll(arc1, arc2, arc3, arc4);}}10public class Exercise14_10 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Ellipse ellipse = new Ellipse(100, 40, 50, 20);ellipse.setFill(Color.WHITE);ellipse.setStroke(Color.BLACK);Arc arc1 = new Arc(100, 140, 50, 20, 0, 180);arc1.setFill(Color.WHITE);arc1.setStroke(Color.BLACK);arc1.getStrokeDashArray().addAll(6.0, 21.0);Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);arc2.setFill(Color.WHITE);arc2.setStroke(Color.BLACK);pane.getChildren().addAll(ellipse, arc1, arc2,new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_10"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}11public class Exercise14_11 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Circle circle = new Circle(100, 100, 80);circle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);Circle circle1 = new Circle(70, 70, 10);Circle circle2 = new Circle(130, 70, 10);Ellipse ellipse1 = new Ellipse(70, 70, 20, 15);ellipse1.setFill(Color.WHITE);ellipse1.setStroke(Color.BLACK);Ellipse ellipse2 = new Ellipse(130, 70, 20, 15);ellipse2.setFill(Color.WHITE);ellipse2.setStroke(Color.BLACK);Line line1 = new Line(100, 80, 80, 120);Line line2 = new Line(80, 120, 120, 120);Line line3 = new Line(120, 120, 100, 80);Arc arc = new Arc(100, 130, 40, 15, 180, 180);arc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);pane.getChildren().addAll(circle, ellipse1, ellipse2,circle1, circle2, line1, line2, line3, arc);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_11"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}12public class Exercise14_12 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();double height = 300;double paneHeight = 150;Rectangle r1 = new Rectangle(10, paneHeight - height * 0.2, 100, height * 0.2); r1.setFill(Color.RED);Text text1 = new Text(10, paneHeight - height * 0.2 - 10, "Project -- 20%"); Rectangle r2 = new Rectangle(10 + 110, paneHeight - height * 0.1, 100, height * 0.1);r2.setFill(Color.BLUE);Text text2 = new Text(10 + 110, paneHeight - height * 0.1 -10, "Quiz -- 10%");Rectangle r3 = new Rectangle(10 + 220, paneHeight - height * 0.3, 100, height * 0.3);r3.setFill(Color.GREEN);Text text3 = new Text(10 + 220, paneHeight - height * 0.3 - 10, "Midterm -- 30%");Rectangle r4 = new Rectangle(10 + 330, paneHeight - height * 0.4, 100, height * 0.4);r4.setFill(Color.ORANGE);Text text4 = new Text(10 + 330, paneHeight - height * 0.4 - 10, "Final -- 40%"); pane.getChildren().addAll(r1, text1, r2, text2, r3, text3, r4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 500, paneHeight);primaryStage.setTitle("Exercise14_12"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}13public class Exercise14_13 extends Application {@Override // Override the start method in the Applicationclasspublic void start(Stage primaryStage) {Pane pane = new Pane();Arc arc1 = new Arc(100, 100, 80, 80, 0, 360 * 0.2);arc1.setFill(Color.RED);arc1.setType(ArcType.ROUND);Text text1 = new T ext(110, 80, "Project -- 20%");Arc arc2 = new Arc(100, 100, 80, 80, 360 * 0.2, 360 * 0.1);arc2.setFill(Color.BLUE);arc2.setType(ArcType.ROUND);Text text2 = new T ext(80, 15, "Quiz -- 10%");Arc arc3 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1, 360 * 0.3);arc3.setFill(Color.GREEN);arc3.setType(ArcType.ROUND);Text text3 = new T ext(5, 100, "Midterm -- 30%");Arc arc4 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1 + 360 * 0.3, 360 * 0.4);arc4.setFill(Color.ORANGE);arc4.setType(ArcType.ROUND);Text text4 = new T ext(100, 180, "Final -- 40%");pane.getChildren().addAll(arc1, text1, arc2, text2, arc3, text3, arc4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_13"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}14public class Exercise14_14 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;double width = paneWidth * 0.90 - 30;double height = paneWidth * 0.90 - 60;double diff = Math.min(width, height) * 0.4;// Draw the front rectangleRectangle r1 = new Rectangle(10, 60, width, height);r1.setFill(new Color(1, 1, 1, 0));r1.setStroke(Color.BLACK);// Draw the back rectangleRectangle r2 = new Rectangle(30, 60 - diff, width, height);r2.setFill(new Color(1, 1, 1, 0));r2.setStroke(Color.BLACK);// Connect the cornersLine line1 = new Line(10, 60, 30, 60 - diff);Line line2 = new Line(10, 60 + height, 30, 60 - diff + height);Line line3 = new Line(10 + width, 60, 30 + width, 60 - diff);Line line4 = new Line(10 + width, 60 + height, 30 + width, 60 - diff + height); pane.getChildren().addAll(r1, r2, line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, paneWidth, paneHeight);primaryStage.setTitle("Exercise14_14"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}15public class Exercise14_15 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {// Create a pane, a polygon, and place polygon to paneStackPane pane = new StackPane();Polygon polygon = new Polygon();polygon.setFill(Color.RED);ObservableList list = polygon.getPoints();final double WIDTH = 200, HEIGHT = 200;double centerX = WIDTH / 2, centerY = HEIGHT / 2;double radius = Math.min(WIDTH, HEIGHT) * 0.4;// Add points to the polygon listfor (int i = 0; i < 8; i++) {list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8 - Math.PI / 8)); list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8 - Math.PI / 8)); }Text text = new Text("STOP");text.setFill(Color.WHITE);text.setFont(Font.font("Times New Roman", 40));pane.getChildren().addAll(polygon, text);// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_15"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}16public class Exercise14_16 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Line line1 = new Line(0, 0, 0, 0);line1.startYProperty().bind(pane.heightProperty().divide(3));line1.endXProperty().bind(pane.widthProperty());line1.endYProperty().bind(pane.heightProperty().divide(3));line1.setStroke(Color.BLUE);Line line2 = new Line(0, 0, 0, 0);line2.startYProperty().bind(pane.heightProperty().multiply(2) .divide(3)); line2.endXProperty().bind(pane.widthProperty());line2.endYProperty().bind(pane.heightProperty().multiply(2). divide(3)); line2.setStroke(Color.BLUE);Line line3 = new Line(0, 0, 0, 0);line3.startXProperty().bind(pane.widthProperty().divide(3));line3.endXProperty().bind(pane.widthProperty().divide(3));line3.endYProperty().bind(pane.heightProperty());line3.setStroke(Color.RED);Line line4 = new Line(0, 0, 0, 0);line4.startXProperty().bind(pane.widthProperty().multiply(2). divide(3));line4.endXProperty().bind(pane.widthProperty().multiply(2).divid e(3));line4.endYProperty().bind(pane.heightProperty());line4.setStroke(Color.RED);pane.getChildren().addAll(line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_16"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}17public class Exercise14_17 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Arc arc = new Arc(60, 240, 40, 20, 0, 180); // Draw the base arc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);Line line1 = new Line(20 + 40, 220, 20 + 40, 20); // Draw the poleLine line2 = new Line(20 + 40, 20, 20 + 40 + 100, 20); // Draw the hangerLine line3 = new Line(20 + 40 + 100, 20, 20 + 40 + 100, 40); // Draw the hanger // Draw the circleint radius = 20;Circle circle = new Circle(20 + 40 + 100, 40 + radius, radius); // Draw the hangercircle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);// Draw the left armLine line4 = new Line(20 + 40 + 100 - radius * Math.cos(Math.toRadians(45)), 40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 - 60, 40 + radius + 60);// Draw the right armLine line5 = new Line(20 + 40 + 100 + radius * Math.cos(Math.toRadians(45)), 40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 + 60, 40 + radius + 60);// Draw the bodyLine line6 = new Line(20 + 40 + 100, 40 + 2 * radius,20 + 40 + 100, 40 + radius + 80);// Draw the left legLine line7 = new Line(20 + 40 + 100, 40 + radius + 80, 20 + 40 + 100 - 40, 40 + radius + 80 + 40);// Draw the right legLine line8 = new Line(20 + 40 + 100, 40 + radius + 80, 20 + 40 + 100 + 40, 40 + radius + 80 + 40);pane.getChildren().addAll(arc, line1, line2, line3, circle, line4, line5, line6, line7, line8);// Create a scene and place it in the stageScene scene = new Scene(pane, 400, 250);primaryStage.setTitle("Exercise14_17"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from thecommand line.*/public static void main(String[] args) {launch(args);}}18public class Exercise14_18 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Polyline polyline = new Polyline();ObservableList list = polyline.getPoints();double scaleFactor = 0.0125;for (int x = -100; x <= 100; x++) {list.add(x + 200.0);list.add(200 - scaleFactor * x * x);}Line line1 = new Line(10, 200, 390, 200);Line line2 = new Line(200, 30, 200, 390);// Draw arrowsLine line3 = new Line(390, 200, 370, 190);Line line4 = new Line(390, 200, 370, 210);Line line5 = new Line(200, 30, 190, 50);Line line6 = new Line(200, 30, 210, 50);// Draw x, y axis labelsText text1 = new T ext(380, 170, "X");Text text2 = new T ext(220, 40, "Y");pane.getChildren().addAll(polyline, line1, line2,line3, line4, line5, line6, text1, text2);// Create a scene and place it in the stageScene scene = new Scene(pane, 400, 250);primaryStage.setTitle("Exercise14_18"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}19public class Exercise14_19 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Polyline polyline1 = new Polyline();ObservableList list1 = polyline1.getPoints();polyline1.setStroke(Color.RED);Polyline polyline2 = new Polyline();ObservableList list2 = polyline2.getPoints();polyline2.setStroke(Color.BLUE);double scaleFactor = 50;for (int x = -170; x <= 170; x++) {list1.add(x + 200.0);list1.add(100 - scaleFactor * Math.sin((x / 100.0) * 2 * Math.PI)); list2.add(x + 200.0);list2.add(100 - scaleFactor * Math.cos((x / 100.0) * 2 * Math.PI)); }Line line1 = new Line(10, 100, 390, 100);Line line2 = new Line(200, 10, 200, 200);// Draw arrowsLine line3 = new Line(390, 100, 370, 90);Line line4 = new Line(390, 100, 370, 110);Line line5 = new Line(200, 10, 190, 30);Line line6 = new Line(200, 10, 210, 30);// Draw x, y axis labelsText text1 = new T ext(380, 70, "X");Text text2 = new T ext(220, 20, "Y");// Draw -2piText text3 = new T ext(140, 115, "-\u03c0");Text text4 = new T ext(250, 115, "\u03c0");Text text5 = new T ext(90, 115, "-2\u03c0");Text text6 = new T ext(345, 115, "2\u03c0");Text text7 = new T ext(200, 115, "0");pane.getChildren().addAll(polyline1, polyline2, line1, line2, line3, line4, line5, line6, text1, text2, text3,text4, text5, text6, text7);// Create a scene and place it in the stageScene scene = new Scene(pane, 400, 250);primaryStage.setTitle("Exercise14_19"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from thecommand line.*/public static void main(String[] args) {launch(args);}}20public class Exercise14_20 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();double paneWidth = 300;double paneHeight = 250;double x1 = Math.random() * (paneWidth - 12);double y1 = Math.random() * (paneHeight - 12);double x2 = Math.random() * (paneWidth - 12);double y2 = Math.random() * (paneHeight - 12);drawArrowLine(x1, y1, x2, y2, pane);// Create a scene and place it in the stageScene scene = new Scene(pane, 400, 250);primaryStage.setTitle("Exercise14_20"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}public static void drawArrowLine(double x1, double y1,double x2, double y2, Pane pane) {pane.getChildren().add(new Line(x1, y1, x2, y2));// find slope of this linedouble slope = ((((double) y1) - (double) y2))。
《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二章练习题答案
![《Java语言程序设计(基础篇)》(第10版 梁勇 著)第二章练习题答案](https://img.taocdn.com/s3/m/7502f89cfad6195f302ba670.png)
《Java语言程序设计(基础篇)》(第10版梁勇著)第二章练习题答案2.1public class Exercise02_01 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Enter a temperature in CelsiusSystem.out.print("Enter a temperature in Celsius: ");double celsius = input.nextDouble();// Convert it to Fahrenheitdouble fahrenheit = (9.0 / 5) * celsius + 32;// Display the resultSystem.out.println(celsius + " Celsius is " +fahrenheit + " Fahrenheit");}}2.1附加public class Exercise02_01Extra {// Main methodpublic static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the coordinates for two points: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();System.out.println("The slope for the line that connects two points (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 + ") is " +(y2 - y1) / (x2 - x1));}}2.2public class Exercise02_02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Enter radius of the cylinderSystem.out.print("Enter the radius and length of a cylinder: ");double radius = input.nextDouble();double length = input.nextDouble();double area = radius * radius * 3.14159;double volume = area * length;System.out.println("The area is " + area);System.out.println("The volume of the cylinder is " + volume);}}2.2附加public class Exercise02_02Extra {public static void main(String[] args) {System.out.print("Enter the ball travel time in seconds: ");Scanner input = new Scanner(System.in);double t = input.nextDouble();final double g = 9.8;double d = g * t * t / 2;System.out.print("The height of the building is " + d + " meters "); }}2.3public class Exercise02_03 {public static void main(String[] args) {// Enter footjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Enter a value for feet: ");double feet = input.nextDouble();double meter = feet * 0.305;System.out.println(feet + " feet is " + meter + " meters");}}2.3附加public class Exercise02_03Extra {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter the friction force in Newtons: ");double f = input.nextDouble();System.out.print("Enter the object抯 mass in kg: ");double m = input.nextDouble();System.out.print("Enter the object抯 acceleration in m/s^2: ");double a = input.nextDouble();final double g = 9.8;double u = (f - m * a) / (m * g);System.out.print("The coefficient for friction is " + u);}}2.4public class Exercise02_04 {public static void main(String[] args) {// Prompt the inputjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Enter a number in pounds: ");double pounds = input.nextDouble();double kilograms = pounds * 0.454;System.out.println(pounds + " pounds is " + kilograms + " kilograms"); }2.5public class Exercise02_05 {public static void main(String args[]) {// Read subtotaljava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Enter subtotal and gratuity rate: ");double subtotal = input.nextDouble();double rate = input.nextDouble();double gratuity = subtotal * rate / 100;double total = subtotal + gratuity;System.out.println("The gratuity is " + gratuity +" total is " + total);}}2.6// Exercise02_06.java: Summarize all digits in an integer < 1000 public class Exercise02_06 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Read a numberSystem.out.print("Enter an integer between 0 and 1000: ");int number = input.nextInt();// Find all digits in numberint lastDigit = number % 10;int remainingNumber = number / 10;int secondLastDigit = remainingNumber % 10;remainingNumber = remainingNumber / 10;int thirdLastDigit = remainingNumber % 10;// Obtain the sum of all digitsint sum = lastDigit + secondLastDigit + thirdLastDigit;// Display resultsSystem.out.println("The sum of all digits in " + number+ " is " + sum);}}2.7public class Exercise02_07 {public static void main(String[] args) {// Prompt the user to enter the number of minutesScanner input = new Scanner(System.in);System.out.print("Enter the number of minutes: ");long minutes = input.nextLong();long numberOfDays = minutes / (24 * 60);long numberOfYears = numberOfDays / 365;// Display resultsSystem.out.println(minutes + " minutes is approximately " +numberOfYears + " years and " + (numberOfDays % 365) + " days"); }2.8public class Exercise02_08 {public static void main(String[] args) {// Prompt the user to enter the time zone offset to GMTScanner input = new Scanner(System.in);System.out.print("Enter the time zone offset to GMT: ");long timeZoneOffset = input.nextInt();// Obtain the total milliseconds since the midnight, Jan 1, 1970long totalMilliseconds = System.currentTimeMillis();// Obtain the total seconds since the midnight, Jan 1, 1970long totalSeconds = totalMilliseconds / 1000;// Compute the current second in the minute in the hourlong currentSecond = totalSeconds % 60;// Obtain the total minuteslong totalMinutes = totalSeconds / 60;// Compute the current minute in the hourlong currentMinute = totalMinutes % 60;// Obtain the total hourslong totalHours = totalMinutes / 60;// Compute the current hourlong currentHour = (totalHours + timeZoneOffset) % 24;// Display resultsSystem.out.println("Current time is " + currentHour + ":"+ currentMinute + ":" + currentSecond);}}2.9public class Exercise02_09 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter v0, v1, and t: ");double v0 = input.nextDouble();double v1 = input.nextDouble();double t = input.nextDouble();double a = (v1 - v0) / t;System.out.println("The average acceleration is " + a);}}2.10public class Exercise02_10 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Enter the amount of water in kilograms: ");double mass = input.nextDouble();System.out.print("Enter the initial temperature: ");double initialTemperature = input.nextDouble();System.out.print("Enter the final temperature: ");double finalTemperature = input.nextDouble();double energy =mass * (finalTemperature - initialTemperature) * 4184;System.out.print("The energy needed is " + energy);}}2.11public class Exercise02_11 {public static void main(String[] args) {// Prompt the user to enter the time zone offset to GMTScanner input = new Scanner(System.in);System.out.print("Enter the number of years: ");int numberOfYears = input.nextInt();double population = 312032486 + numberOfYears * 365 * 24 * 60 * 60 / 7.0 -numberOfYears * 365 * 24 * 60 * 60 / 13.0 + numberOfYears * 365 * 24 * 60 * 60 / 45.0;// Display resultsSystem.out.println("The population in " + numberOfYears + " years is " + (int)population);}}2.12public class Exercise02_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter speed v: ");double v = input.nextDouble();System.out.print("Enter acceleration a: ");double a = input.nextDouble();double length = v * v / (2 * a);System.out.println("The minimum runway length for this airplane is " + length + " meters");}}2.13public class Exercise02_13 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter monthly saving amount: ");double monthlyDeposit = input.nextDouble();double currentValue = monthlyDeposit;// First month valuecurrentValue = currentValue * (1 + 0.00417);System.out.println("After the first month, the account value is " +currentValue);// Second month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println("After the second month, the account value is " + currentValue);// Third month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println("After the third month, the account value is " + currentValue);// Fourth month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);// Fifth month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);// Sixth month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println("After the sixth month, the account value is " + currentValue);}}2.14public class Exercise02_14 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter weight in poundsSystem.out.print("Enter weight in pounds: ");double weight = input.nextDouble();// Prompt the user to enter height in inchesSystem.out.print("Enter height in inches: ");double height = input.nextDouble();double bmi = weight * 0.45359237 / (height * 0.0254 * height * 0.0254);System.out.print("BMI is " + bmi);}2.15public class Exercise02_15 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Enter the first point with two double valuesSystem.out.print("Enter x1 and y1: ");double x1 = input.nextDouble();double y1 = input.nextDouble();// Enter the second point with two double valuesSystem.out.print("Enter x2 and y2: ");double x2 = input.nextDouble();double y2 = input.nextDouble();// Compute the distancedouble distance = Math.pow((x1 - x2) * (x1 - x2) +(y1 - y2) * (y1 - y2), 0.5);System.out.println("The distance of the two points is " + distance);}}2.16public class Exercise02_16 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Enter the side of the hexagonSystem.out.print("Enter the side: ");double side = input.nextDouble();// Compute the areadouble area = 3 * 1.732 * side * side / 2;System.out.println("The area of the hexagon is " + area); }}public class Exercise02_17 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Enter the temperature in FahrenheitSystem.out.print("Enter the temperature in Fahrenheit between -58 and 41: ");double fahrenheit = input.nextDouble();// Enter the wind speed miles per hourSystem.out.print("Enter the wind speed miles per hour " +"(must be greater than or equal to 2) : ");double speed = input.nextDouble();// Compute wind chill indexdouble windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 *Math.pow(speed, 0.16) + 0.4275 * fahrenheit *Math.pow(speed, 0.16);// Display the resultSystem.out.println("The wind chill index is " + windChillIndex);}}2.18public class Exercise02_18 {// Main methodpublic static void main(String[] args) {System.out.println("a b pow(a, b)");System.out.println("1 2 " + (int)Math.pow(1, 2));System.out.println("2 3 " + (int)Math.pow(2, 3));System.out.println("3 4 " + (int)Math.pow(3, 4));System.out.println("4 5 " + (int)Math.pow(4, 5));System.out.println("5 6 " + (int)Math.pow(5, 6));}}2.19public class Exercise02_19 {public static void main(String[] args) {// Enter three points for a triangleSystem.out.print("Enter three points for a triangle: ");Scanner input = new Scanner(System.in);double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDouble();double y3 = input.nextDouble();// Compute the length of the three sidesdouble side1 = Math.pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5);double side2 = Math.pow((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3), 0.5);double side3 = Math.pow((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2), 0.5);double s = (side1 + side2 + side3) / 2;double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);System.out.println("The area of the triangle is " + area);}}2.20public class Exercise02_20 {public static void main(String args[]) {Scanner input = new Scanner(System.in);// Obtain inputSystem.out.print("Enter balance and annual interest rate: ");double balance = input.nextDouble();double annualInterestRate = input.nextDouble();double monthlyInterestRate = annualInterestRate / 1200;double interest = balance * monthlyInterestRate;// Display outputSystem.out.println("The interest is " + (int)(100* interest) / 100.0); }}2.21// Exercise02_11.java: Create a method for computing future valuepublic class Exercise02_21 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Enter the investment amountSystem.out.print("Enter the investment amount, for example 120000.95: ");double investmentAmount = input.nextDouble();// Enter yearly interest rateSystem.out.print("Enter annual interest rate, for example 8.25: ");double annualInterestRate = input.nextDouble();// Obtain monthly interest ratedouble monthlyInterestRate = annualInterestRate / 1200;// Enter number of yearsSystem.out.print("Enter number of years as an integer, for example 5: ");int numOfYears = input.nextInt();double futureValue =investmentAmount * Math.pow(1 + monthlyInterestRate,numOfYears * 12);System.out.print("Future value is " +(int)(futureValue * 100) / 100.0);}}2.22public class Exercise02_22 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Receive the amount entered from the keyboardSystem.out.print("Enter an amount in integer, for example 1156 \nfor 11 dollars and 56 cents: ");int amount = input.nextInt();int remainingAmount = amount;// Find the number of one dollarsint numOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quaters in the remaining amountint numOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numOfPennies = remainingAmount;// Display resultsSystem.out.println("Your amount " + amount + " consists of "); System.out.println(numOfOneDollars + " dollars");System.out.println(numOfQuarters + " quarters");System.out.println(numOfDimes + " dimes");System.out.println(numOfNickels + " nickels");System.out.println(numOfPennies + " pennies");}}2.23public class Exercise02_23 {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter the driving distance: ");double distance = input.nextDouble();System.out.print("Enter miles per gallon: ");double milesPerGallon = input.nextDouble();System.out.print("Enter price per gallon: ");double pricePerGallon = input.nextDouble();System.out.println("The cost of driving is $" + (distance / milesPerGallon) * pricePerGallon); }}。
java基础入门课后习题答案
![java基础入门课后习题答案](https://img.taocdn.com/s3/m/cb42d7943086bceb19e8b8f67c1cfad6195fe99b.png)
java基础入门课后习题答案Java基础入门课后习题答案Java是一门广泛应用于软件开发领域的编程语言,掌握Java的基础知识对于想要从事软件开发的人来说是非常重要的。
在学习Java的过程中,课后习题是巩固知识和提高编程能力的重要途径。
本文将为大家提供一些Java基础入门课后习题的答案,希望能够对大家的学习有所帮助。
一、基础语法题1. 编写一个Java程序,输出"Hello, World!"。
```javapublic class HelloWorld {public static void main(String[] args) {System.out.println("Hello, World!");}}```2. 编写一个Java程序,计算并输出1到100之间所有偶数的和。
```javapublic class SumOfEvenNumbers {public static void main(String[] args) {int sum = 0;for (int i = 2; i <= 100; i += 2) {sum += i;System.out.println("1到100之间所有偶数的和为:" + sum); }}```3. 编写一个Java程序,判断一个数是否为素数。
```javapublic class PrimeNumber {public static void main(String[] args) {int num = 17;boolean isPrime = true;for (int i = 2; i <= Math.sqrt(num); i++) {if (num % i == 0) {isPrime = false;break;}}if (isPrime) {System.out.println(num + "是素数");} else {System.out.println(num + "不是素数");}}```二、面向对象题1. 编写一个Java类,表示一个学生,包含学生的姓名和年龄,并提供获取和设置姓名、年龄的方法。
Java语言程序设计课后习题答案全集
![Java语言程序设计课后习题答案全集](https://img.taocdn.com/s3/m/b8b2d0d2f9c75fbfc77da26925c52cc58bd690ba.png)
Java语言程序设计课后习题答案全集Java语言程序设计是一门广泛应用于软件开发领域的编程语言,随着其应用范围的不断扩大,对于掌握Java编程技巧的需求也逐渐增加。
为了帮助读者更好地掌握Java编程,本文将提供Java语言程序设计课后习题的全集答案,供读者参考。
一、基础知识题1. 代码中的注释是什么作用?如何使用注释.答:注释在代码中是用来解释或者说明代码的功能或用途的语句,编译器在编译代码时会自动忽略注释。
在Java中,有三种注释的方式:- 单行注释:使用"// " 可以在代码的一行中加入注释。
- 多行注释:使用"/* */" 可以在多行中添加注释。
- 文档注释:使用"/** */" 可以添加方法或类的文档注释。
2. 什么是Java的数据类型?请列举常见的数据类型。
答:Java的数据类型用来指定变量的类型,常见的数据类型有:- 基本数据类型:包括整型(byte、short、int、long)、浮点型(float、double)、字符型(char)、布尔型(boolean)。
- 引用数据类型:包括类(class)、接口(interface)、数组(array)等。
二、代码编写题1. 编写Java程序,输入两个整数,求和并输出结果。
答:```javaimport java.util.Scanner;public class SumCalculator {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入第一个整数:");int num1 = scanner.nextInt();System.out.print("请输入第二个整数:");int num2 = scanner.nextInt();int sum = num1 + num2;System.out.println("两个整数的和为:" + sum);}}```三、综合应用题1. 编写Java程序,实现学生信息管理系统,要求包括以下功能:- 添加学生信息(姓名、年龄、性别、学号等);- 修改学生信息;- 删除学生信息;- 查询学生信息。
Java语言程序设计课后习题答案全集
![Java语言程序设计课后习题答案全集](https://img.taocdn.com/s3/m/216067128e9951e79b892772.png)
J a v a语言程序设计课后习题答案全集Document serial number【UU89WT-UU98YT-UU8CB-UUUT-UUT108】指出JAVA语言的主要特点和JAVA程序的执行过程。
答:(1)强类型;(2)编译和解释;(3)自动无用内存回收功能;(4)面向对象;(5)与平台无关;(6)安全性;(7)分布式计算;(8)多线程;程序执行过程如图所示:编写源文件,编译器编译源文件转换成字节码,解释器执行字节码。
说出开发与运行JAVA程序的重要步骤。
答:(1)编写源文件:使用一个文本编译器,如Edit或记事本,不可以使用Word.将编好的源文件保存起来,源文件的扩展名必须是.java;(2)编译Java源文件:使用Java编译器编译源文件得到字节码文件;(3)运行Java程序:Java程序分为两类——Java应用程序必须通过Java解释器来解释执行其字节码文件;Java小应用程序必须通过支持Java标准的浏览器来解释执行。
如何区分应用程序和小应用程序答:应用程序在与源文件名字相同的类中,有main()方法,该方法代表应用程序的入口; 小应用程序必须有一个Applet类的子类,该类称作主类,必须用public修饰。
说出JAVA源文件的命名规则。
答:源文件命名规则和类命名规则一样,所有的单词首字母都用大写字母,且必须和源文件的public类同名。
JAVA语言使用什么字符集共有多少个不同的字符答:Java语言使用Unicode字符集,共有65535个字符。
JAVA语言标识符的命名规则是什么(1)由字母(包括英文字母、下划线字符、美元字符、文字字符)和数字字符组成(2)限定标识符的第一个字符不能是数字字符(3)不能和关键字重名(4)长度不能超过255个字符JAVA有那些基本数据类型,它们的常量又是如何书写的指出下列内容哪些是JAVA语言的整型常量,哪些是浮点数类型常量,哪些两者都不是。
整型常量: 4)0xABCL,8)003,10)077,12)056L浮点数类型常量:3)-1E-31,5).32E31 13)0.,14).0两者都不是: 1),2),6),7),9),11)第二章 运算和语句Java 的字符能参加算术运算吗可以。
Java语言程序设计(基础篇)(第10版 梁勇著)第二章练习题答案
![Java语言程序设计(基础篇)(第10版 梁勇著)第二章练习题答案](https://img.taocdn.com/s3/m/d4e09f58be1e650e52ea99fe.png)
《Java语言程序设计(基础篇)》(第10版 梁勇 著) 第二章 基本程序设计 练习题答案
本人在自学编程过程中,发现本书答案很难找,找到的要么不完整、要么错误百出,所以我将自己所做的 练习题答案,提供给有需要者,供大家交流。本章答案均为本人一字一字所敲,答案均经过验证,虽为初学者, 但代码均按照书中规范要求书写,如有错误或更好的建议,请指正交流。
// 第二章 P59 练习题2.1 (将摄氏温度转为华氏温度) import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) { // 华氏温度和摄氏温度的转换公式为:华氏温度 = (9/5)*摄氏温度+32 Scanner input = new Scanner(System.in);
System.out.print("Enter the time zone offset to GMT: "); long timeZoneOffset = input.nextLong(); // 此处用long还是int?
long totalMilliseconds = System.currentTimeMillis(); long totalSeconds = totalMilliseconds / 1000; long currentSecond = totalSeconds % 60; long totalMinutes = totalSeconds / 60; long currentMinute = totalMinutes % 60; long totalHours = totalMinutes / 60; long currentHour = totalHours % 24; long hour = currentHour + timeZoneOffset;
《Java基础入门》课后习题答案
![《Java基础入门》课后习题答案](https://img.taocdn.com/s3/m/21b3091358f5f61fb6366628.png)
《Java基础入门》课后习题答案第1章Java开发入门一、填空题1、Java EE、Java SE、Java ME2、JRE3、javac4、bin5、path、classpath二、判断题1、对2、错3、对4、对5、错三、选择题1、ABCD2、C3、D4、B5、B四、简答题1、面向对象、跨平台性、健壮性、安全性、可移植性、多线程性、动态性等。
2、JRE(Java Runtime Environment,Java运行时环境),它相当于操作系统部分,提供了Java程序运行时所需要的基本条件和许多Java基础类,例如,IO类、GUI控件类、网络类等。
JRE是提供给普通用户使用的,如果你只想运行别人开发好的Java程序,那么,你的计算机上必须且只需安装JRE。
JDK(Java Development Kit,Java开发工具包),它包含编译工具、解释工具、文档制作工具、打包工具多种与开发相关的工具,是提供给Java开发人员使用的。
初学者学习和使用Java语言时,首先必须下载和安装JDK。
JDK中已经包含了JRE部分,初学者安装JDK后不必再去下载和安装JRE了。
3、Java程序运行时,必须经过编译和运行两个步骤。
首先将后缀名为.java的源文件进行编译,生成后缀名为.class的字节码文件,然后Java虚拟机将字节码文件进行解释执行,并将结果显示出来。
五、编程题1、参考答案HelloWorld.java1public class HelloWorld {2public static void main(String[] args) {3System.out.println("这是第一个Java程序!");4}5}第2章Java编程基础一、填空题1、true和false2、基本数据类型、引用数据类型3、& && | ||4、55、56二、判断题1、错2、对3、错4、对5、错三、选择题1、AD2、C3、C4、B5、A四、简答题1、Java语言的八种基本数据类型有:byte字节型,占一个字节。
java语言程序设计课后习题+答案
![java语言程序设计课后习题+答案](https://img.taocdn.com/s3/m/2ffbc2363186bceb18e8bb1b.png)
J a v a语言程序设计课后习题+答案(总142页)-CAL-FENGHAI.-(YICAI)-Company One1-CAL-本页仅作为文档封面,使用请直接删除第一章课后习题1.编译Java程序的命令是什么2.执行Java程序的命令是什么应用程序和小程序的区别是什么4.编写一个application ,实现在屏幕上打印自己名字的功能。
第一章课后习题答案1.编译Java程序的命令是什么答案:javac 源文件名2.执行Java程序的命令是什么java 主类名应用程序和小程序的区别是什么Java application由Java解释器独立运行字节码由专门的命令行启动程序执行程序中有定义了main()方法的主类Java applet不能独立运行,字节码必须嵌入HTML文档当浏览器调用含applet的Web页面时执行程序中含有java. applet. Applet 类的子类4.编写一个application ,实现在屏幕上打印自己名字的功能。
class Test{public static void main(String[] args){张三”);}}第二章课后习题(1)一、选择题1.下列变量定义错误的是。
A) int a; B) double b=; C) boolean b=true; D)float f=;2.下列数据类型的精度由高到低的顺序是:a)float,double,int,longb)double,float,int,bytec)byte,long,double,floatd)double,int,float,long3.执行完下列代码后,int a=3;char b='5';char c=(char)(a+b);c的值是A)’8’ b)53 c)8 d)56是一种_____________A) 数据类型 B)java包 C)字符编码 D)java类+5%3+2的值是___________A)2 B)1 C) 9 D)106.下面的逻辑表达式中合法的是__________A)(7+8)&&(9-5) B)(9*5)||(9*7) C)9>6&&8<10 D)(9%4)&&(8*3)语言中,占用32位存储空间的是__________。
Java语言程序设计基础篇课后答案-机械工业出版社[资料]
![Java语言程序设计基础篇课后答案-机械工业出版社[资料]](https://img.taocdn.com/s3/m/2e0c8907a31614791711cc7931b765ce05087a16.png)
Java语言程序设计基础篇课后答案-机械工业出版社Java语言程序设计1 / 81&[编程练习题答案]Java语言程序设计(基础篇)(进阶篇)第13、14、15、24、25、26、28、29、30、31章部分习题答案,奇数题和部分双数题是我自己做的,在此和大家分享(^_^~Chapter 13 Exercise 13.2 / 13.8 / 13.11import javax.swing.*;import java.awt.*;public class Exercise13_2 extends JFrame {// Create two buttonsprivate OvalButton jbtOk = new OvalButton("OK");private OvalButton jbtCancel = new OvalButton("Cancel");/** Default constructor */public Exercise13_2() {// Set the window titlesetTitle("Exercise13_2");// Set FlowLayout manager to arrange the componentsJava语言程序设计2 / 81// inside the framegetContentPane().setLayout(new FlowLayout());// Add buttons to the framegetContentPane().add(jbtOk);getContentPane().add(jbtCancel);}/** Main method */public static void main(String[] args) {Exercise13_2 frame = new Exercise13_2();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setVisible(true);}}class OvalButton extends JButton {public OvalButton() {}public OvalButton(String text) {super(text);}protected void paintComponent(Graphics g) {// Draw an ovalsuper.paintComponent(g);g.drawOval(5, 5, getWidth() - 10, getHeight() - 10); }/** Override get method for preferredSize */public Dimension getPreferredSize() {return new Dimension(100, 50);}/** Override get method for minimumSize */public Dimension getMinimumSize() {return new Dimension(100, 50);}}Java语言程序设计3 / 81import java.awt.*;import javax.swing.*;public class Exercise13_8 extends JFrame {public static void main(String[] args) {JFrame frame = new Exercise13_8();frame.setSize(300, 300);frame.setTitle("Exercise13_8");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}public Exercise13_8() {getContentPane().add(new OctagonPanel());}}class OctagonPanel extends JPanel {protected void paintComponent(Graphics g) {super.paintComponent(g);int xCenter = getWidth() / 2;int yCenter = getHeight() / 2;int radius =(int) (Math.min(getWidth(), getHeight()) * 0.4);// Create a Polygon objectPolygon polygon = new Polygon();// Add points to the polygonpolygon.addPoint(xCenter + radius, yCenter);polygon.addPoint((int)(xCenter + radius * Math.cos(2 *Math.PI / 8)),(int)(yCenter - radius * Math.sin(2 * Math.PI / 8)));polygon.addPoint((int)(xCenter + radius * Math.cos(2 * 2 * Math.PIJava语言程序设计4 / 81/ 8)),(int)(yCenter - radius * Math.sin(2 * 2 * Math.PI / 8)));polygon.addPoint((int)(xCenter + radius * Math.cos(3 * 2 * Math.PI/ 8)),(int)(yCenter - radius * Math.sin(3 * 2 * Math.PI / 8)));polygon.addPoint((int)(xCenter + radius * Math.cos(4 * 2 * Math.PI/ 8)),(int)(yCenter - radius * Math.sin(4 * 2 * Math.PI / 8)));polygon.addPoint((int)(xCenter + radius * Math.cos(5 * 2 * Math.PI/ 8)),(int)(yCenter - radius * Math.sin(5 * 2 * Math.PI / 8)));polygon.addPoint((int)(xCenter + radius * Math.cos(6 * 2 * Math.PI/ 8)),(int)(yCenter - radius * Math.sin(6 * 2 * Math.PI / 8))); polygon.addPoint((int)(xCenter + radius * Math.cos(7 * 2* Math.PI/ 8)),(int)(yCenter - radius * Math.sin(7 * 2 * Math.PI / 8)));// Draw the polygong.drawPolygon(polygon);}}import javax.swing.*;import java.awt.*;public class Exercise13_11 extends JFrame {public Exercise13_11(){add(new SquareFunction());}/*** @param args*/public static void main(String[] args) {Java语言程序设计5 / 81// TODO 自动生成方法存根Exercise13_11 frame = new Exercise13_11();frame.setTitle("Exercise13.11");frame.setSize(300,200);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}}class SquareFunction extends JPanel {public SquareFunction(){}protected void paintComponent(Graphics g){super.paintComponent(g);g.drawLine(10, this.getHeight()-20, this.getWidth()-10,this.getHeight()-20);g.drawLine(this.getWidth()-30, this.getHeight()-15,this.getWidth()-10, this.getHeight()-20);g.drawLine(this.getWidth()-30, this.getHeight()-25,this.getWidth()-10, this.getHeight()-20);g.drawString("X", this.getWidth()-10,this.getHeight()-30);g.drawLine(this.getWidth()/2, 20, this.getWidth()/2,this.getHeight()-5);g.drawLine(this.getWidth()/2, 20, this.getWidth()/2-5,35);g.drawLine(this.getWidth()/2, 20, this.getWidth()/2+5,35);g.drawString("Y", this.getWidth()/2 + 10, 30);Polygon p = new Polygon();double scaleFactor = 0.01;for (int x=-100; x<=100; x++){p.addPoint(x+this.getWidth()/2,this.getHeight()-20-(int)(scaleFactor*x*x));}g.drawPolyline(p.xpoints, p.ypoints, p.npoints);}}Java语言程序设计6 / 81Chapter 14 Exercise 14.4 / 14.8 / 14.9package chapter14;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Exercise14_4 extends JFrame {public Exercise14_4(){MousePosition p = new MousePosition();add(p);}/*** @param args*/public static void main(String[] args) {// TODO 自动生成方法存根Exercise14_4 frame = new Exercise14_4();frame.setTitle("Exercise14.4");frame.setSize(300, 200);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocationRelativeTo(null);frame.setVisible(true);}static class MousePosi。
自考04747《Java语言程序设计(一)》课后习题答案全集
![自考04747《Java语言程序设计(一)》课后习题答案全集](https://img.taocdn.com/s3/m/c8d55ad933d4b14e8524686d.png)
第一章 Java语言基础 (5)1.1 指出JA V A语言的主要特点和JA V A程序的执行过程。
(5)1.2 说出开发与运行JA V A程序的重要步骤。
(5)1.3 如何区分应用程序和小应用程序? (5)1.4 说出JA V A源文件的命名规则。
(5)1.6 JA V A语言使用什么字符集?共有多少个不同的字符? (5)1.7 JA V A语言标识符的命名规则是什么? (6)1.8 JA V A有那些基本数据类型,它们的常量又是如何书写的? (6)1.9 指出下列内容哪些是JA V A语言的整型常量,哪些是浮点数类型常量,哪些两者都不是。
(6)第二章运算和语句 (7)2.1 Java的字符能参加算术运算吗? (7)2.2 占字节多的变量能直接赋值给占字节少的变量么? (7)2.3 试用Java语言表达式描述以下数学计算式或逻辑条件: (7)2.4 设在求以下表达式之前,整型变量a的值是4,试指出在求了以下表达式之后,变量a、b和c的值。
(7)2.6 若一个数恰好等于它的因子之和,则这个数称为“完全数”。
编写程序求1000之内的所有完全数。
(7)2.7 编写输入正实数x,求平方不超过x的最大整数n,并输出。
(8)2.8 输入正整数n,输出由n行n列星号字符组成的三角形图案。
(11)2.9 设有整型变量x和y的值分别为5和110。
试指出执行了以下循环语句后,变量x和y的值分别多少? (12)2.10求小于999的所有水仙花数。
(12)2.11 编程序解百鸡问题:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。
百钱买百鸡。
求鸡翁、鸡母和鸡雏各几何? (13)2.12 编写程序,按下面的公式计算自然对数底e的值:e=1+1/1!+1/2!+1/3!+1/4!+...... (13)2.13 编写程序,用如下公式计算圆周率PI的近似值:PI=4-4/3+4/5-4/7+4/9-4/11... (14)2.14 回文整数是指正读法和反读相同的整数,编写一个程序,输入一个整数,判断他是否为回文整数。
java语言程序的设计基础篇第十版第十四章练习答案
![java语言程序的设计基础篇第十版第十四章练习答案](https://img.taocdn.com/s3/m/4c75d47a0722192e4536f6c5.png)
01import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_01 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);ImageView imageView1 = new ImageView("image/uk.gif");ImageView imageView2 = new ImageView("image/ca.gif");ImageView imageView3 = new ImageView("image/china.gif");ImageView imageView4 = new ImageView("image/us.gif");pane.add(imageView1, 0, 0);pane.add(imageView2, 1, 0);pane.add(imageView3, 0, 1);pane.add(imageView4, 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_01"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}02import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;import javafx.scene.image.Image;public class Exercise14_02 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Image imageX = new Image("image/x.gif");Image imageO = new Image("image/o.gif");GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {int status = (int)(Math.random() * 3);if (status == 0) {pane.add(new ImageView(imageX), j, i);}else if (status == 1) {pane.add(new ImageView(imageO), j, i);}}}// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_02"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}import java.util.ArrayList;import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.HBox;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_03 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {// There are two ways for shuffling. One is to use the hint in the book. // The other way is to use the static shuffle method in the java.util.Collections class.ArrayList<Integer> list = new ArrayList<>();for (int i = 1; i <= 52; i++) {list.add(i);}java.util.Collections.shuffle(list);HBox pane = new HBox(5);pane.setAlignment(Pos.CENTER);pane.getChildren().add(new ImageView("image/card/" + list.get(0) + ".png")); pane.getChildren().add(new ImageView("image/card/" + list.get(1) + ".png")); pane.getChildren().add(new ImageView("image/card/" + list.get(2) + ".png"));// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_03"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}04import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.HBox;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_04 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {HBox pane = new HBox();pane.setAlignment(Pos.CENTER);Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.ITALIC, 22);for (int i = 0; i < 5; i++) {Text txt = new Text("Java");txt.setRotate(90);txt.setFont(font);txt.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 100);primaryStage.setTitle("Exercise14_04"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}05import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_05 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length(); Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {}}05import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_05 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD,FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length(); Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {}}06import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.scene.shape.Rectangle;public class Exercise14_06 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;Pane pane = new Pane();for (int i = 0; i < 8; i++) {boolean isWhite = i % 2 == 0;for (int j = 0; j < 8; j++) {Rectangle rectangle = new Rectangle(i * WIDTH / 8,j * HEIGHT / 8, WIDTH / 8, HEIGHT / 8);rectangle.setStroke(Color.BLACK);if (isWhite) {rectangle.setFill(Color.WHITE);}else {rectangle.setFill(Color.BLACK);}isWhite = !isWhite;pane.getChildren().add(rectangle);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_06"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}07import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.TextField;import yout.GridPane;import javafx.stage.Stage;public class Exercise14_07 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;GridPane pane = new GridPane();for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {TextField tf = new TextField((int)(Math.random() + 0.5) + ""); tf.setPrefColumnCount(1);tf.setAlignment(Pos.CENTER);pane.add(tf, j, i);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_07"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}08import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.image.ImageView;import yout.GridPane;import javafx.stage.Stage;public class Exercise14_08 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {GridPane pane = new GridPane();for (int i = 0; i < 6; i++) {for (int j = 0; j < 9; j++) {pane.add(new ImageView("image/card/" + (i * 6 + j + 1) + ".png"), j, i); }}// Create a scene and place it in the stageScene scene = new Scene(pane, 600, 600);primaryStage.setTitle("Exercise14_08"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}09import javafx.application.Application;import javafx.scene.Scene;import yout.GridPane;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.ArcType;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class Exercise14_09 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.add(new FanPane(), 0, 0);pane.add(new FanPane(), 1, 0);pane.add(new FanPane(), 0, 1);pane.add(new FanPane(), 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_09"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}class FanPane extends Pane {double radius = 50;public FanPane() {Circle circle = new Circle(60, 60, radius);circle.setStroke(Color.BLACK);circle.setFill(Color.WHITE);getChildren().add(circle);Arc arc1 = new Arc(60, 60, 40, 40, 30, 35);arc1.setFill(Color.RED); // Set fill colorarc1.setType(ArcType.ROUND);Arc arc2 = new Arc(60, 60, 40, 40, 30 + 90, 35);arc2.setFill(Color.RED); // Set fill colorarc2.setType(ArcType.ROUND);Arc arc3 = new Arc(60, 60, 40, 40, 30 + 180, 35);arc3.setFill(Color.RED); // Set fill colorarc3.setType(ArcType.ROUND);Arc arc4 = new Arc(60, 60, 40, 40, 30 + 270, 35);arc4.setFill(Color.RED); // Set fill colorarc4.setType(ArcType.ROUND);getChildren().addAll(arc1, arc2, arc3, arc4);}}10import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Ellipse;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_10 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Ellipse ellipse = new Ellipse(100, 40, 50, 20);ellipse.setFill(Color.WHITE);ellipse.setStroke(Color.BLACK);Arc arc1 = new Arc(100, 140, 50, 20, 0, 180);arc1.setFill(Color.WHITE);arc1.setStroke(Color.BLACK);arc1.getStrokeDashArray().addAll(6.0, 21.0);Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);arc2.setFill(Color.WHITE);arc2.setStroke(Color.BLACK);pane.getChildren().addAll(ellipse, arc1, arc2,new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_10"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}11import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Circle;import javafx.scene.shape.Ellipse;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_11 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Circle circle = new Circle(100, 100, 80);circle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);Circle circle1 = new Circle(70, 70, 10);Circle circle2 = new Circle(130, 70, 10);Ellipse ellipse1 = new Ellipse(70, 70, 20, 15);ellipse1.setFill(Color.WHITE);ellipse1.setStroke(Color.BLACK);Ellipse ellipse2 = new Ellipse(130, 70, 20, 15);ellipse2.setFill(Color.WHITE);ellipse2.setStroke(Color.BLACK);Line line1 = new Line(100, 80, 80, 120);Line line2 = new Line(80, 120, 120, 120);Line line3 = new Line(120, 120, 100, 80);Arc arc = new Arc(100, 130, 40, 15, 180, 180);arc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);pane.getChildren().addAll(circle, ellipse1, ellipse2,circle1, circle2, line1, line2, line3, arc);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_11"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}12import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_12 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();double height = 300;double paneHeight = 150;Rectangle r1 = new Rectangle(10, paneHeight - height * 0.2, 100, height * 0.2); r1.setFill(Color.RED);Text text1 = new Text(10, paneHeight - height * 0.2 - 10, "Project -- 20%");Rectangle r2 = new Rectangle(10 + 110, paneHeight - height * 0.1, 100, height * 0.1);r2.setFill(Color.BLUE);Text text2 = new Text(10 + 110, paneHeight - height * 0.1 - 10, "Quiz -- 10%");Rectangle r3 = new Rectangle(10 + 220, paneHeight - height * 0.3, 100, height * 0.3);r3.setFill(Color.GREEN);Text text3 = new Text(10 + 220, paneHeight - height * 0.3 - 10, "Midterm -- 30%");Rectangle r4 = new Rectangle(10 + 330, paneHeight - height * 0.4, 100, height * 0.4);r4.setFill(Color.ORANGE);Text text4 = new Text(10 + 330, paneHeight - height * 0.4 - 10, "Final -- 40%"); pane.getChildren().addAll(r1, text1, r2, text2, r3, text3, r4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 500, paneHeight);primaryStage.setTitle("Exercise14_12"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}13import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.ArcType;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_13 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Arc arc1 = new Arc(100, 100, 80, 80, 0, 360 * 0.2);arc1.setFill(Color.RED);arc1.setType(ArcType.ROUND);Text text1 = new Text(110, 80, "Project -- 20%");Arc arc2 = new Arc(100, 100, 80, 80, 360 * 0.2, 360 * 0.1);arc2.setFill(Color.BLUE);arc2.setType(ArcType.ROUND);Text text2 = new Text(80, 15, "Quiz -- 10%");Arc arc3 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1, 360 * 0.3);arc3.setFill(Color.GREEN);arc3.setType(ArcType.ROUND);Text text3 = new Text(5, 100, "Midterm -- 30%");Arc arc4 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1 + 360 * 0.3, 360 * 0.4);arc4.setFill(Color.ORANGE);arc4.setType(ArcType.ROUND);Text text4 = new Text(100, 180, "Final -- 40%");pane.getChildren().addAll(arc1, text1, arc2, text2, arc3, text3, arc4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_13"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}14import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Line;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class Exercise14_14 extends Application {Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;double width = paneWidth * 0.90 - 30;double height = paneWidth * 0.90 - 60;double diff = Math.min(width, height) * 0.4;// Draw the front rectangleRectangle r1 = new Rectangle(10, 60, width, height);r1.setFill(new Color(1, 1, 1, 0));r1.setStroke(Color.BLACK);// Draw the back rectangleRectangle r2 = new Rectangle(30, 60 - diff, width, height);r2.setFill(new Color(1, 1, 1, 0));r2.setStroke(Color.BLACK);// Connect the cornersLine line1 = new Line(10, 60, 30, 60 - diff);Line line2 = new Line(10, 60 + height, 30, 60 - diff + height);Line line3 = new Line(10 + width, 60, 30 + width, 60 - diff);Line line4 = new Line(10 + width, 60 + height, 30 + width, 60 - diff + height); pane.getChildren().addAll(r1, r2, line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, paneWidth, paneHeight);primaryStage.setTitle("Exercise14_14"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}15import javafx.application.Application;import javafx.collections.ObservableList;import javafx.scene.Scene;import yout.StackPane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.scene.shape.Polygon;import javafx.scene.text.Font;import javafx.scene.text.Text;public class Exercise14_15 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {// Create a pane, a polygon, and place polygon to paneStackPane pane = new StackPane();Polygon polygon = new Polygon();polygon.setFill(Color.RED);ObservableList<Double> list = polygon.getPoints();final double WIDTH = 200, HEIGHT = 200;double centerX = WIDTH / 2, centerY = HEIGHT / 2;double radius = Math.min(WIDTH, HEIGHT) * 0.4;// Add points to the polygon listfor (int i = 0; i < 8; i++) {list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8 - Math.PI / 8)); list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8 - Math.PI / 8)); }Text text = new Text("STOP");text.setFill(Color.WHITE);text.setFont(Font.font("Times New Roman", 40));pane.getChildren().addAll(polygon, text);// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_15"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}16import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_16 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Line line1 = new Line(0, 0, 0, 0);line1.startYProperty().bind(pane.heightProperty().divide(3));line1.endXProperty().bind(pane.widthProperty());line1.endYProperty().bind(pane.heightProperty().divide(3));line1.setStroke(Color.BLUE);Line line2 = new Line(0, 0, 0, 0);line2.startYProperty().bind(pane.heightProperty().multiply(2).divide(3)); line2.endXProperty().bind(pane.widthProperty());line2.endYProperty().bind(pane.heightProperty().multiply(2).divide(3)); line2.setStroke(Color.BLUE);Line line3 = new Line(0, 0, 0, 0);line3.startXProperty().bind(pane.widthProperty().divide(3));line3.endXProperty().bind(pane.widthProperty().divide(3));line3.endYProperty().bind(pane.heightProperty());line3.setStroke(Color.RED);Line line4 = new Line(0, 0, 0, 0);line4.startXProperty().bind(pane.widthProperty().multiply(2).divide(3)); line4.endXProperty().bind(pane.widthProperty().multiply(2).divide(3));line4.endYProperty().bind(pane.heightProperty());line4.setStroke(Color.RED);pane.getChildren().addAll(line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_16"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}17import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Line;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class Exercise14_17 extends Application {Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Arc arc = new Arc(60, 240, 40, 20, 0, 180); // Draw the basearc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);Line line1 = new Line(20 + 40, 220, 20 + 40, 20); // Draw the poleLine line2 = new Line(20 + 40, 20, 20 + 40 + 100, 20); // Draw the hangerLine line3 = new Line(20 + 40 + 100, 20, 20 + 40 + 100, 40); // Draw the hanger// Draw the circleint radius = 20;Circle circle = new Circle(20 + 40 + 100, 40 + radius, radius); // Draw the hanger circle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);// Draw the left armLine line4 = new Line(20 + 40 + 100 - radius * Math.cos(Math.toRadians(45)), 40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 - 60, 40 + radius + 60);// Draw the right armLine line5 = new Line(20 + 40 + 100 + radius * Math.cos(Math.toRadians(45)), 40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 + 60, 40 + radius + 60);// Draw the bodyLine line6 = new Line(20 + 40 + 100, 40 + 2 * radius,20 + 40 + 100, 40 + radius + 80);// Draw the left legLine line7 = new Line(20 + 40 + 100, 40 + radius + 80, 20 + 40 + 100 - 40, 40 + radius + 80 + 40);。
《JAVA程序设计》习题集及参考答案_IT168文库-推荐下载
![《JAVA程序设计》习题集及参考答案_IT168文库-推荐下载](https://img.taocdn.com/s3/m/cc54a72bbe1e650e52ea99c7.png)
9. 异常处理的重要性在于,程序不但要发现异常,还要捕获异常并编写代 码来处理
10. 类是同种对象集合的抽象,而属于类的某一个对象则被称为是类的一个 实例,对象是具有 状态 、行为 、标志三种属性的计算机软件结构。
适当事件发生时自动调用该实例的几个主要方法是:init()方法、start()
方法、 paint()
方法、stop( )方法和 destroy() 方法。
12. Java Application 是独立、完整的程序,在命令行调用独立的解释器软件
(java.exe)即可运行,其程序主类必须有一个 main()方法,完整的方法
22. Java 虚拟机(JVM)是编译后的 Java 程序和 硬件系统 之间的接口, 是用软件方法实现的一台假想机。JVM 使用的代码存储在 .class 文 件中 。
23. Java Applet 中的 init() 方法仅在开始的时候执行一次, start() 方法在 用户每次访问包含 Applet 的 HTML 文件时都会被调用, repaint() 方法 可以用来更新 Applet 的画面。
头为
public static void main(String aegs[]) 。
13. Java Applet 程序需要与浏览器中已预先实现好的功能结合,才能构成一
个完整的程序,其直接父类必须为 Applet 类,Applet 所需做的是接
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术通关,1系电过,力管根保线据护敷生高设产中技工资术0艺料不高试仅中卷可资配以料置解试技决卷术吊要是顶求指层,机配对组置电在不气进规设行范备继高进电中行保资空护料载高试与中卷带资问负料题荷试22下卷,高总而中体且资配可料置保试时障卷,各调需类控要管试在路验最习;大题对限到设度位备内。进来在行确管调保路整机敷使组设其高过在中程正资1常料中工试,况卷要下安加与全强过,看2度并22工且22作尽22下可22都能2可地护1以缩关正小于常故管工障路作高高;中中对资资于料料继试试电卷卷保破连护坏接进范管行围口整,处核或理对者高定对中值某资,些料审异试核常卷与高弯校中扁对资度图料固纸试定,卷盒编工位写况置复进.杂行保设自护备动层与处防装理腐置,跨高尤接中其地资要线料避弯试免曲卷错半调误径试高标方中高案资等,料,编5试要写、卷求重电保技要气护术设设装交备备4置底高调、动。中试电作管资高气,线料中课并3敷试资件且、设卷料中拒管技试试调绝路术验卷试动敷中方技作设包案术,技含以来术线及避槽系免、统不管启必架动要等方高多案中项;资方对料式整试,套卷为启突解动然决过停高程机中中。语高因文中此电资,气料电课试力件卷高中电中管气资壁设料薄备试、进卷接行保口调护不试装严工置等作调问并试题且技,进术合行,理过要利关求用运电管行力线高保敷中护设资装技料置术试做。卷到线技准缆术确敷指灵设导活原。。则对对:于于在调差分试动线过保盒程护处中装,高置当中高不资中同料资电试料压卷试回技卷路术调交问试叉题技时,术,作是应为指采调发用试电金人机属员一隔,变板需压进要器行在组隔事在开前发处掌生理握内;图部同纸故一资障线料时槽、,内设需,备要强制进电造行回厂外路家部须出电同具源时高高切中中断资资习料料题试试电卷卷源试切,验除线报从缆告而敷与采设相用完关高毕技中,术资要资料进料试行,卷检并主查且要和了保检解护测现装处场置理设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
01import class Exercise14_01 extends Application {@Override Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}02import class Exercise14_02 extends Application {@Override Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}03import class Exercise14_03 extends Application {@Override One is to use the hint in the book.ArrayList<Integer> list = new ArrayList<>();for (int i = 1; i <= 52; i++) {(i);}HBox pane = new HBox(5);;().add(new ImageView("image/card/" + (0) + ".png")); ().add(new ImageView("image/card/" + (1) + ".png")); ().add(new ImageView("image/card/" + (2) + ".png"));Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}04import class Exercise14_04 extends Application { @Override dd(txt);}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}05import class Exercise14_05 extends Application { @Override dd(txt);}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}05import class Exercise14_05 extends Application { @Override dd(txt);}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}06import class Exercise14_06 extends Application { @Override dd(rectangle);}}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}07import class Exercise14_07 extends Application {@Override Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}08import class Exercise14_08 extends Application {@Override ng"), j, i);}}Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}09import class Exercise14_09 extends Application { @Override Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}class FanPane extends Pane {double radius = 50;public FanPane() {Circle circle = new Circle(60, 60, radius);;;getChildren().add(circle);Arc arc1 = new Arc(60, 60, 40, 40, 30, 35);; ddAll(arc1, arc2, arc3, arc4);}}10import class Exercise14_10 extends Application {@Override ddAll, ;Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);;;().addAll(ellipse, arc1, arc2,new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}11import class Exercise14_11 extends Application {@Override ddAll(circle, ellipse1, ellipse2,circle1, circle2, line1, line2, line3, arc);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}12import class Exercise14_12 extends Application {@Override ddAll(r1, text1, r2, text2, r3, text3, r4, text4);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}13import class Exercise14_13 extends Application {@Override ddAll(arc1, text1, arc2, text2, arc3, text3, arc4, text4); Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}14import class Exercise14_14 extends Application { @Override ddAll(r1, r2, line1, line2, line3, line4);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}15import class Exercise14_15 extends Application { @Override ddAll(polygon, text);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}16import class Exercise14_16 extends Application {@Override ind().divide(3));().bind());().bind().divide(3));;Line line2 = new Line(0, 0, 0, 0);().bind().multiply(2).divide(3));().bind());().bind().multiply(2).divide(3));;Line line3 = new Line(0, 0, 0, 0);().bind().divide(3));().bind().divide(3));().bind());;Line line4 = new Line(0, 0, 0, 0);().bind().multiply(2).divide(3));().bind().multiply(2).divide(3));().bind());;().addAll(line1, line2, line3, line4);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}17import class Exercise14_17 extends Application {@Override ddAll(arc, line1, line2, line3, circle, line4, line5, line6, line7, line8);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}18import class Exercise14_18 extends Application {@Override ddAll(polyline, line1, line2,line3, line4, line5, line6, text1, text2);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}19import class Exercise14_19 extends Application {@Override ddAll(polyline1, polyline2, line1, line2,line3, line4, line5, line6, text1, text2, text3,text4, text5, text6, text7);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}20import class Exercise14_20 extends Application {@Override dd(new Line(x1, y1, x2, y2));dd(new Line(x2, y2, (x2 + (arctan + set45) * arrlen)),((y2)) + (arctan + set45) * arrlen)));().add(new Line(x2, y2, (x2 + (arctan - set45) * arrlen)),((y2)) + (arctan - set45) * arrlen)));}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}21import class Exercise14_21 extends Application {@Override istance(x2, y2) + "");().addAll(circle1, circle2, line, text);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}import class Exercise14_22 extends Application { @Override ddAll(circle1, circle2, line, text1, text2);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}23import class Exercise14_23 extends Application { @Override ddAll(r1, r2, text);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}24import class Exercise14_24 extends Application { @Override ddAll(polygon, new Circle(x5, y5, 10), text);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}25import class Exercise14_25 extends Application { @Override ddAll(circle, polygon);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}26import class Exercise14_26 extends Application {@Override ddAll(clock1, clock2);Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}27import class Exercise14_27 extends Application { @OverrideNot needed for running from the command line.*/public static void main(String[] args) {launch(args);}}class DetailedClockPane extends Pane {private int hour;private int minute;private int second;lear();getChildren().addAll(circle, sLine, mLine, hLine);dd(new Line(xOuter, yOuter, xInner, yInner));}dd(text);}}}28import class Exercise14_28 extends Application { @OverrideNot needed for running from the command line.*/public static void main(String[] args) {launch(args);}}class ClockPaneWithBooleanProperties extends Pane {private int hour;private int minute;private int second;private boolean hourHandVisible = true;private boolean minuteHandVisible = true;private boolean secondHandVisible = true;public boolean isHourHandVisible() {return hourHandVisible;}public void setHourHandVisible(boolean hourHandVisible) {= hourHandVisible;paintClock();}public boolean isMinuteHandVisible() {return minuteHandVisible;}public void setMinuteHandVisible(boolean minuteHandVisible) { = minuteHandVisible;paintClock();}public boolean isSecondHandVisible() {return secondHandVisible;}public void setSecondHandVisible(boolean secondHandVisible) { = secondHandVisible;paintClock();}lear();getChildren().addAll(circle, t1, t2, t3, t4);if (secondHandVisible) {getChildren().add(sLine);}if (minuteHandVisible) {getChildren().add(mLine);}if (hourHandVisible) {getChildren().add(hLine);}}}29import class Exercise14_29 extends Application {final static double HGAP = 20;final static double VGAP = 20;final static double RADIUS = 5;final static double LENGTH_OF_SLOTS = 40;final static double LENGTH_OF_OPENNING = 15;final static double Y_FOR_FIRST_NAIL = 50;final static double NUMBER_OF_SLOTS = 9;final static double NUMBER_OF_ROWS = NUMBER_OF_SLOTS - 2;@Override dd(c);}}dd(new Line(x, y, x, y + LENGTH_OF_SLOTS));}dd(new Line(centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP,y + LENGTH_OF_SLOTS, centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP,y + LENGTH_OF_SLOTS));dd(new Line(centerX + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS,centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y));().add(new Line(centerX - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS,centerX - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y));dd(new Line(centerX - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS,centerX - HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING));().add(new Line(centerX + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, centerX + HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING));Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}。