java语言程序设计基础篇第十版第十四章练习答案

合集下载

java语言程序设计基础篇第十版课后答案

java语言程序设计基础篇第十版课后答案

第一章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语言程序设计课后答案

java语言程序设计课后答案作业参考答案习题一4、如何建立和运行Java程序,首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java作为文件扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。

在集成开发环境Jbuilder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。

5、public class LikeJava{public static void main(String [] args){System.out.println(“I Like Java Very much!”);}}习题二5、(1) 45 (2) false (3) 14 (4) 14 (5),6 (6) true(7) 129、public class Volume{public static void main(String [] args) {double r=0,v=0;r=double.parseDouble(args[0]);v=4*3.14159/3*r*r*r;System.out.println(“球体积为:”+v);}}习题三8、public class Factorials {public static void main(String args[]) {int i, j;long s=0, k;i=1;do //外循环开始{k = 1;j=1;do{//内循环开始k = k * j; //内循环体j++;}while(j<=i);//内循环结束System.out.println(i + "!=" + k);s = s + k;i++;}while(i<=20); //外循环结束System.out.println("Total sum=" + s); }}10、public class Num{public static void main(String[]args) {int i,j,k,n;for (n=100;n<1000;n++){i=n/100;j=(n-i*100)/10;k=n%10;if (i*i*i+j*j*j+k*k*k==n)System.out.print(n+" ");}}}习题四5、import java.util.Scanner;class Factor{long fac(int m){if(m==0||m==1)return 1;else return m*fac(m-1);}public static void main(String [] args){int i,n;long sum=0;String s="";Scanner input=new Scanner(System.in);System.out.print("Please input n: ");n=input.nextInt();Factor f=new Factor();for(i=1;i<=n;i++){ System.out.println(f.fac(i));sum=sum+f.fac(i);s=s+i+"!+";}System.out.println(s.substring(0,s.length()-1)+"="+sum); }}习题五2、import java.io.*;public class YangHuiOk{public static void main (String args[]) throws IOException {int max,a[][],i,j;char x;System.out.print("请输入杨辉三角要显示的行数: ");x=(char)System.in.read();max = Integer.parseInt(String.valueOf(x));a=new int[max][];for (i=0;i<max;i++){a[i]=new int[i+1];}a[0][0]=1;for (i=1;i<max;i++){a[i][0]=1;a[i][a[i].length-1]=1;for (j=1;j<a[i].length-1;j++){a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i<max;i++){//for(j=0;j<=max-i;j++) System.out.print(" ");for(j=0;j<=a[i].length-1;j++) System.out.print(a[i][j]+" "); System.out.println();}}}5、import java.util.Scanner;public class MatrixTurn {public static void main (String[] args) {int m,n;Scanner input=new Scanner(System.in);System.out.print("请输入矩阵的行数: ");m=input.nextInt();System.out.print("请输入矩阵的列数: ");n=input.nextInt();Matrix t=new Matrix(m,n);for(int i=1;i<=m;i++)//为矩阵各元素赋值for (int j=1;j<=n;j++)t.setElement(Math.random(),i,j);System.out.println("转置前的矩阵如下: ");for(int i=1;i<=m;i++){for (int j=1;j<=n;j++)//System.out.print(t.matrix[i][j]+" ");System.out.print(t.getElement(i,j)+" ");//访问矩阵元素方法1 System.out.println();}Matrix z;//声明转置矩阵z=t.turn(t);System.out.println("转置后的矩阵如下: ");for(int i=0;i<n;i++){for (int j=0;j<m;j++)System.out.print(z.matrix[i][j]+" ");//访问矩阵元素方法2,前提是matrix前无privateSystem.out.println();}}}习题六9、public class Vehicle,String color, kind;int speed;Vehicle(){color=”Red”;kind=”卡车”;speed=0;}public void setColor(String color1) { color=color1;}public void setSpeed(String speed1) { speed=speed1;}public void setKind(String kind1) { kind=kind1;}public String getColor( ) {return color;}public String getKind( ) {return kind;}public int getSpeed( ) {return speed;}public static void main(String [] args){Vehicle che=new Vehicle ();Che.setColor(“Blue”);Che.setSpeed(150);Che.setKind(“跑车”);System.out.p rintln(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公路上”);System.out.println(“时速”+che.getSpeed()+”km/h”); }}习题七 7、public class Vehicle ,String color, kind;int speed;Vehicle(){color=” ”;kind=” ”;speed=0;}public void setColor(String color1){color=color1;}public void setSpeed(String speed1) {speed=speed1;}public void setKind(String kind1) {kind=kind1;}public String getColor( ) {return color;}public String getKind( ) {return kind;}public int getSpeed( ) {return speed;}}public class Car extends Vehicle {int passenger;public Car(){super();passenger=0;}public void setPassenger(int passenger){this. passenger = passenger; }public int getPassenger( ) {return passenger;}public static void main(String [] args){Car benz=new Car();benz.setColor(“Yellow”);benz.setKind(“roadster”);benz.setSpeed(120);benz.setPassenger(4);System.out.println(“benz: “);System.out.println(“Color “+benz.getColor());System.out.print(“Speed (km/h)“);System.out.println(benz.getSpeed()); System.out.println(“Kind: “+benz.getKind()); System.out.print(“Passenger: “);System.out.println(benz.getPassenger());}}习题九4、import java.io.*;public class UseException{public static void main(String [] args){System.out.println("请输入一个整数字符串");try{BufferedReader in=new BufferedReader(new InputStreamReader(System.in));int a=Integer.parseInt(in.readLine());System.out.println("您输入的整数是:"+a);}catch(IOException e){System.out.println("IO错误");}catch(NumberFormatException e1){System.out.println("您输入的不是一个整数字符串");}}}习题十 7、import java.io.*;public class SaveName {public static void main(String [] args){try{BufferedReader br=new BufferedReader(newInputStreamReader(System.in));BufferedWriter bw=new BufferedWriter(new FileWriter("name.txt"));String s;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();}br.close();bw.close();}catch(FileNotFoundException e){System.out.println(e.toString());}catch(IOException e1){System.out.println(e1.toString());}}}8、import java.io.*;public class SaveGrade{public static void main(String [] args){try{BufferedReader br=new BufferedReader(newInputStreamReader(System.in));BufferedWriter bw=new BufferedWriter(new FileWriter("grade.txt"));String s,ss;while(true){System.out.println("请输入姓名:");s=br.readLine();if(s.length()==0)break;bw.write(s);bw.newLine();System.out.println("请输入学号:");s=br.readLine();bw.write(s);bw.newLine();System.out.println("请输入成绩:");s=br.readLine();bw.write(s);bw.newLine();}br.close();bw.close();int max=0,min=100,total=0,num=0;BufferedReader bf=new BufferedReader(new FileReader("grade.txt")); while(true){ss=bf.readLine();if(ss==null)break;ss=bf.readLine();ss=bf.readLine();int grade=Integer.parseInt(ss);total+=grade;num+=1;if(grade>max)max=grade;if(grade<min)min=grade;}System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);bf.close();}catch(FileNotFoundException e){System.out.println(e.toString());}catch(IOException e1){System.out.println(e1.toString());}}}习题十一6、import java.awt.*;import java.awt.event.*;public class ChangeColor extends Frame { private Button red=new Button("红");private Button green=new Button("绿"); private Button blue=new Button("蓝"); private TextField text=new TextField(); public ChangeColor(){super("改变颜色");this.setLayout(null);text.setBackground(Color.WHITE);red.setBounds(25,50,50,20);this.add(red);green.setBounds(125,50,50,20);this.add(green);blue.setBounds(225,50,50,20);this.add(blue);text.setBounds(25,100,250,30);this.add(text);red.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.RED);}});green.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.GREEN);}});blue.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {text.setBackground(Color.BLUE);}});addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});setSize(300,200);setVisible(true);}public static void main (String[] args){ChangeColor color=new ChangeColor(); }}习题十二5、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Goods extends JFrame {private JComboBox list;private JTextArea info;private String names[]={"请选择你要查询的商品","A商品","B商品","C商品","D商品","E商品","F商品"};private String goods[][]={ {"","",""},{"A商品","北京",",300"},{"B商品","上海",",400"},{"C商品","广州",",500"},{"D商品","长沙",",600"},{"E商品","武汉",",700"},{"F商品","天津",",800"}};public Goods(){super("商品信息");Container pane=this.getContentPane();pane.setLayout(new BorderLayout());list=new JComboBox(names);info=new JTextArea(5,20);pane.add(list,BorderLayout.NORTH);pane.add(info,BorderLayout.CENTER);list.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) {int index=list.getSelectedIndex();info.setText("商品名:"+goods[index][0]+"\n"); info.append("产地:"+goods[index][1]+"\n"); info.append("价格:"+goods[index][2]+"\n"); }});this.setSize(250,300);this.setVisible(true);}public static void main (String[] args) {Goods ccc=new Goods();ccc.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) {System.exit(0);}});}}。

Java语言程序设计第14章习题参考答案.docx

Java语言程序设计第14章习题参考答案.docx

习题十四参考答案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程序设计 张永强 张墨华 第14章习题答案

Java程序设计  张永强 张墨华 第14章习题答案

第 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语言程序设计(基础篇)第10版习题答案Chapter9-1

Java语言程序设计(基础篇)第10版习题答案Chapter9-1

Java语⾔程序设计(基础篇)第10版习题答案Chapter9-1(矩形类 Rectangle)遵照9.2节中 Circle 类的例⼦,设计⼀个名为 Rectangle 的类表⽰矩形。

这个类包括:两个名为 width 和 height 的 double 型数据域,它们分别表⽰矩形的宽和⾼。

width 和 height 的默认值都为1。

创建默认矩形的⽆参构造⽅法⼀个创建 width 和 height 为指定值的矩形的构造⽅法。

⼀个名为 getArea() 的⽅法返回这个矩形的⾯积。

⼀个名为 getPerimeter() 的⽅法返回周长。

画出该矩形的 UML 图并实现这个类。

编写⼀个测试程序,创建两个 Rectangle 对象——⼀个矩形的宽为 4 ⽽⾼为 40,另⼀个矩形的宽为3.5 ⽽⾼为 35.9 。

按照这个顺序显⽰每个矩形的宽、⾼、⾯积、周长。

程序代码:public class Rectangle {private double width;private double height;public Rectangle(){width=1.0;height=1.0;}public Rectangle(double width,double height) {this.width=width;this.height=height;}public double getWidth() {return width;}public double getHeight() {return height;}public double getArea() {return width*height;}public double getPerimeter() {return 2*(width+height);}public static void main(String[] args) {// TODO Auto-generated method stubRectangle t1=new Rectangle(4,40);Rectangle t2=new Rectangle(3.5,35.9);System.out.println("width:"+t1.getWidth()+" height:"+t1.getHeight()+" Area:"+t1.getArea()+" Per:"+t1.getPerimeter());System.out.println("width:"+t2.getWidth()+" height:"+t2.getHeight()+" Area:"+t2.getArea()+" Per:"+t2.getPerimeter());}}运⾏结果:width:4.0 height:40.0 Area:160.0 Per:88.0width:3.5 height:35.9 Area:125.64999999999999 Per:78.8。

Java程序设计基础习题答案

Java程序设计基础习题答案

Java程序设计基础习题答案Java程序设计基础课后习题参考答案第2章1、关于Java Application得入口方法main()得检验:main()方法得参数名就是否可以改变?main()方法得参数个数就是否可以改变?该方法名就是否可以改变?参考答案:(1)main()方法得参数名可以改变.(2)main()方法得参数个数不可以改变。

(3)该方法名不可以改变。

2、当一个程序没有main()方法时,能编译吗?如果能编译,能运行吗?参考答案:当一个程序没有main()方法就是,就是可以编译通过得,但就是不能给运行,因为找不到一个主函数入口。

3、下列语句能否编译通过?bytei =127;bytej = 128;longl1 = 999999;long l2= 9999999999;参考答案:byte i 与long l1可以编译通过。

而byte j 与longl2 超出自身数据类型范围,所以编译失败。

4、下列语句能否编译通过?float f1 =3、5;float f2 = 3.5f;参考答案:java中浮点型得数据在不声明得情况下都就是doubl e型得,如果要表示一个数据就是float型得,必须在数据后面加上“F”或“f”;因此,floatf1 无法编译通过。

5、验证int 与char,int与double等类型就是否可以相互转换。

参考答案:(1)char类型可以转换为int 类型得,但就是int类型无法转换为char类型得;(2)int 可以转换为double类型得,但就是double类型无法转换为int 类型得。

6、计算下列表达式,注意观察运算符优先级规则。

若有表达式就是非法表达式,则指出不合法之处且进行解释。

(1)4+5 == 6*2 ?(2) (4=5)/6??(3)9%2*7/3>17(4)(4+5)<=6/3 ?(5) 4+5%3!=7-2(6)4+5/6〉=10%2参考答案:表达式(2)为不合法表达式,只能将值赋值给一个变量,因此其中(4=5)将5赋值给4就是不合法得.7、下列()就是合法得Java标识符。

java语言程序设计课后习题答案

java语言程序设计课后习题答案

java语言程序设计课后习题答案Java语言程序设计是计算机科学与技术专业的重要课程之一,通过课后习题的解答,可以帮助学生巩固所学知识,提高编程能力。

本文将回答一些Java语言程序设计的常见习题,帮助读者更好地理解与运用Java语言。

1. 基本数据类型Java中的基本数据类型有byte、short、int、long、float、double、char和boolean。

它们分别用于表示不同的数据类型,如整数、浮点数、字符和布尔值等。

在Java中,基本数据类型的大小是固定的,不会受到不同机器架构的影响。

2. 变量与常量在Java中,使用关键字"var"、"final"和数据类型来声明变量。

变量可以根据需要进行赋值与修改。

而常量在声明时需要使用关键字"final"来修饰,并且一旦赋值后就不能再修改。

3. 运算符Java中的运算符有算术运算符、赋值运算符、关系运算符、逻辑运算符和位运算符等。

它们用于执行不同的操作,如数学运算、赋值、比较和逻辑判断等。

4. 条件语句条件语句用于根据条件来执行不同的代码块。

Java中的条件语句有if语句、if-else语句、switch语句等。

通过条件语句,可以根据不同的条件来执行相应的代码逻辑。

5. 循环语句循环语句用于重复执行一段代码块。

Java中的循环语句有for循环、while循环和do-while循环等。

通过循环语句,可以方便地处理需要重复执行的操作。

6. 数组数组是一种存储相同类型数据的集合。

在Java中,数组可以存储基本数据类型或引用类型的数据。

数组有固定的长度,在声明时需要指定数组的大小,并且可以通过索引访问数组中的元素。

7. 方法方法用于封装一段特定的代码,可以通过方法名来调用执行。

Java中的方法可以有返回值,也可以没有返回值。

通过方法的调用,可以提高代码的复用性和可读性。

8. 类与对象类是面向对象编程的基本单位,用于封装数据和方法。

java语言程序设计基础篇第十版第十四章练习答案

java语言程序设计基础篇第十版第十四章练习答案
}
}
}
// 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()));
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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 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);}}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 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);}}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);}}import 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);}}import 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 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);}}import 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 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);}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 class public 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);}}import 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 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);}}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 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);}}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 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);}}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 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 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 class public 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 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);。

相关文档
最新文档