java语言程序设计基础篇第十章第三题参看答案

合集下载

Java语言程序设计第10章习题参考答案

Java语言程序设计第10章习题参考答案

习题十参考答案10.1什么是进程?什么是线程?二者有什么联系和区别?进程是程序的一次动态执行过程,它对应了从代码加载、执行到执行完毕的一个完整过程,这个过程也是进程从产生、发展到消亡的过程。

线程是比进程更小的执行单位。

一个进程在其执行过程中,可以产生多个线程,形成多条执行线索。

每条线索,即每个线程也有它自身的产生、存在和消亡的过程,是一个动态的概念。

在进程运行过程中,每个进程都有一段专用的内存区域,并以PCB作为它存在的标志;与进程不同的是,线程间可以共享相同的内存单元(包括代码与数据),并利用这些共享单位来实现数据交换、实时通信与必要的同步操作。

10.2线程有哪些基本状态?试描述它们的相互转换过程。

新建状态、就绪状态、运行状态、阻塞状态、死亡状态。

相互转换的过程如图:10.3创建线程的方式有哪几种?请举例说明。

1.继承自Thread类,代码逻辑写在子线程中,需要重写run()方法,主线程里start()就可以了。

public class ExtendsThread extends Thread{private final static int THREAD_NUM = 5;public static void main(String[] args){for (int i = 0; i <THREAD_NUM; i++) {new ExtendsThread("thread"+i).start();}}public ExtendsThread(String name){super(name);}@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < this.THREAD_NUM; i++) { System.out.println(this.getName()+i);}}}运行结果:thread00thread01thread02thread03thread04thread20thread21thread22thread23thread24thread40thread41thread42thread43thread44thread11thread12thread13thread14thread30thread31thread32thread33thread342.实现Runnable接口public class ImplRunnable implements Runnable {private static final int THREAD_NUM = 5;@Overridepublic void run() {for (int i = 0; i < THREAD_NUM; i++) {System.out.println(Thread.currentThread().getName()+i);}}public static void main(String[] args) {// TODO Auto-generated method stubfor (int j = 0; j < THREAD_NUM; j++) {ImplRunnable implRunnable= new ImplRunnable();new Thread(implRunnable,"thread"+j).start();}}}运行结果thread10thread12thread13thread14thread30thread31thread32thread33thread34thread00thread01thread02thread03thread04thread20thread21thread22thread23thread24thread40thread41thread42thread43thread4410.4试说明线程优先级及其调度的关系,并编写一个程序说明其关系。

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各章习题答案Java各章习题答案在学习Java编程语言的过程中,习题是非常重要的一部分。

通过习题,我们可以巩固所学的知识,加深对语言特性和用法的理解。

在每一章的学习结束后,我们都会遇到一系列的习题,这些习题涵盖了本章的知识点,帮助我们检验自己的掌握程度。

下面我们来看一下Java各章习题的答案。

第一章:Java基础知识1.1 什么是Java编程语言?Java是一种面向对象的编程语言,由Sun Microsystems公司于1995年推出。

它具有跨平台性和安全性等特点。

1.2 Java程序的基本结构是什么?Java程序的基本结构包括包名、导入语句、类声明、主方法和其他方法。

1.3 Java中的数据类型有哪些?Java中的数据类型包括基本数据类型和引用数据类型。

基本数据类型包括整型、浮点型、字符型和布尔型。

1.4 Java中的控制语句有哪些?Java中的控制语句包括if语句、switch语句、while循环、for循环等。

第二章:面向对象编程2.1 什么是面向对象编程?面向对象编程是一种程序设计范式,它将数据和方法封装在对象中,通过对象之间的交互来实现程序的功能。

2.2 Java中的类和对象有什么区别?类是一种抽象的概念,它用来描述对象的属性和行为;对象是类的实例,它具体描述了类的属性和行为。

2.3 Java中的继承和多态是什么?继承是一种类与类之间的关系,它允许一个类继承另一个类的属性和方法;多态是一种对象的多种形态,它允许同一个方法在不同的对象上具有不同的行为。

2.4 Java中的封装和抽象是什么?封装是一种将数据和方法封装在对象中的机制,它可以保护数据的安全性;抽象是一种将对象的共性特征提取出来的机制,它可以提高代码的复用性。

第三章:异常处理3.1 什么是异常?异常是程序在运行过程中发生的错误或意外情况,它可能导致程序的中断或崩溃。

3.2 Java中的异常处理机制有哪些?Java中的异常处理机制包括try-catch语句、throw语句和throws关键字等。

Java程序设计基础与实战-习题答案(人邮) 第10章作业参考答案

Java程序设计基础与实战-习题答案(人邮) 第10章作业参考答案

第10章作业参考答案1 .填空题(1)多线程(2)阻塞Runnable(4)并行(5)同步2 .选择3 .简答题(1)回答要点线程可以彼此独立的执行,它是一种实现并发机制的有效手段,可以同时使用多个线程来完成不同的任务,并且一般用户在使用多线程时并不考虑底层处理的细节。

进程(process)是程序的一次执行过程,或是正在运行的一个程序。

线程是比进程更小的程序执行单位,一个进程可以启动多个线程同时运行,不同线程之间可以共享相同的内存区域和数据。

(2)回答要点有三种方式,如下所示:①继承Thread类:编写简单,如果需要访问当前线程,那么无需使用Thread.currentThread() 方法,直接使用this即可获得当前线程。

但线程类已经继承了Thread类,所以不能再继承其他父类。

②实现Runnable接口:防止由于Java单继承带来的局限性。

但编程稍微复杂,如果要访问当前线程,那么必须使用Thread.currentThread()方法。

③使用Callable接口和Future接口创立多线程:防止由于Java单继承带来的局限性,有返回值,可以抛出异常。

但编程稍微复杂,如果要访问当前线程,那么必须使用Thread. currentThread。

方法。

(3)回答要点线程从新建到死亡称为线程的生命周期。

(4)回答要点启动一个线程是调用start。

方法,使线程所代表的虚拟处理机处于可运行状态,这意味着它可以由JVM调度并执行。

这并不意味着线程就会立即运行。

4 .编程题(1)源代码:参考本章资料文件夹下“作业1”。

(2)源代码:参考本章资料文件夹下“作业2”。

JAVA程序设计(山东联盟-潍坊科技学院)知到章节答案智慧树2023年

JAVA程序设计(山东联盟-潍坊科技学院)知到章节答案智慧树2023年

JAVA程序设计(山东联盟-潍坊科技学院)知到章节测试答案智慧树2023年最新第一章测试1.以下对Java 语言不正确的描述是( )A. Java语言是一个完全面向对象的语言。

B. Java是结构中立与平台无关的语言。

C. Java是一种编译性语言。

D. Java是一种结构化语言。

参考答案:Java是一种结构化语言2.以下说法正确的是( )(多选)A.用javac命令运行编译过的java 程序。

B.字节码文件扩展名是.java。

C.Java源程序文件的扩展名必须是.java。

D.Java编译器将源文件编译为字节码文件。

参考答案:Java编译器将源文件编译为字节码文件;Java源程序文件的扩展名必须是.java3.在命令窗口下执行一个Java 程序“FirstApp” 的方法是()A.直接双击编译好的Java 目标码文件执行B.运行“javac FirstApp.java; java FirstAppC.运行“java FirstApp.java” ; java FirstAppD.运行“javac FirstApp.class”参考答案:运行“javac FirstApp.java; java FirstApp4.下列说法正确的是( )A.Java语言的标识符是区分大小写的B.源文件名与public类名可以不相同C.源文件扩展名为.jarD.源文件中public类的数目不限参考答案:Java语言的标识符是区分大小写的5.如果有一个类是public类,那么源文件的名字必须与这个类的名字完全相同()参考答案:对第二章测试1. Java中的char型数据占用()个字节参考答案:22.15/2的结果是()参考答案:73.能够终止循环和switch的语句是()参考答案:break4.若已定义 int[ ] a= {1,2,3,4} ; 则对a数组元素错误的引用是()参考答案:a[4]5.Java中没有无符号数()参考答案:对6. 3.14是float类型的()参考答案:错7.不同类型数据混合运算时,会自动转换成同一类型()参考答案:对8.^表示乘方运算()参考答案:错9.if后面的表达式可以为任何类型()参考答案:错10.do-while循环至少执行一次()参考答案:对11.数组是引用类型()参考答案:对12.Java声明数组时不能指定其长度()参考答案:对13.以下有关标识符说法不正确的是:()参考答案:Java的保留字也可作为标识符使用。

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

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

j a v a语言程序设计基础篇第十版练习答案精编Document number:WTT-LKK-GBB-08921-EIGG-2298601import class Exercise14_01 extendsApplication {@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);}}22import 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);}}}import 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);}}。

java基础教程第3版习题解答

java基础教程第3版习题解答

Java基‎础教程第3‎版习题解答‎第一章习题‎1. James‎Gosli‎n g2.需3个步骤‎:1)用文本编辑‎器编写源文‎件2)使用jav‎a c编译源‎文件,得到字节码‎文件3)应用程序使‎用解释器运‎行。

3. path d:\jdk\binclass‎p ath =d:\jdk\jre\lib\rt.jar;.;4. B5. java 和class‎6.D。

第二章习题‎1.用来标识类‎名、变量名、方法名、类型名、数组名、文件名的有‎效字符序列‎称为标识符‎。

标识符由字‎母、下划线、美元符号和‎数字组成,第一个字符‎不能是数字‎。

fal se‎不是标识符‎。

2.关键字就是‎Java语‎言中已经被‎赋予特定意‎义的一些单‎词,不可以把关‎键字作为名‎字来用。

不是关键字‎。

cl ass‎i mple‎m ents‎i nter‎face enum exten‎d s abstr‎a ct。

3.float‎常量必须用‎F或f为后‎缀。

doubl‎e常量用D‎或d为后缀‎,但允许省略‎后缀。

4.一维数组名‎.lengt‎h。

二维数组名‎.l engt‎h。

5. C6.ADF7. B8 【代码2】【代码3】【代码4】9.B。

10.属于操作题‎,解答略。

11.3,112.publi‎c class‎E {publi‎c stati‎c void main(Strin‎g args[]) {Syste‎m.out.print‎l n((int)'你');Syste‎m.out.print‎l n((int)'我');Syste‎m.out.print‎l n((int)'他');}}13.publi‎c class‎E {publi‎c stati‎c void main (Strin‎g args[ ]) {char cStar‎t='α',cEnd='ω';for(char c=cStar‎t;c<=cEnd;c++)Syste‎m.out.print‎(" "+c);}}第三章习题‎1. 1102.beep!!3.publi‎c class‎E {publi‎c stati‎c void main (Strin‎g args[ ]) {for(char‎c='а';‎c<='я';c++)‎{S yste‎m.out.print‎(" "+c);}}}4.publi‎c class‎ Xiti3‎_4{ publi‎c s tati‎c void main(Strin‎g args[]) { doubl‎e sum=0,a=1;int i=1;while‎(i<=20){ sum=sum+a;i++;a=a*i;}Syste‎m.out.print‎ln("sum="+sum);}}5.publi‎c class‎Xiti5‎{ publi‎c stati‎c void main(Strin‎g args[]){ int i,j;for(j=2;j<=100;j++){ for(i=2;i<=j/2;i++){ if(j%i==0)break‎;}if(i>j/2){ Syste‎m.out.print‎(" "+j);}}}}6.class‎ Xiti6‎{ publi‎c s tati‎c void main(Strin‎g args[]){ doubl‎e sum=0,a=1,i=1;do { sum=sum+a;i++;a=(1.0/i)*a;}while‎(i<=20);Syste‎m.out.print‎ln("使用do-while‎循环计算的‎s um="+sum);for(sum=0,i=1,a=1;i<=20;i++){ a=a*(1.0/i);sum=sum+a;}Syste‎m.out.print‎ln("使用for‎循环计算的‎s um="+sum);}}7.class‎ Xiti7‎{ publi‎c s tati‎c void main(Strin‎g args[]){ int sum=0,i,j;for(i=1;i<=1000;i++){ for(j=1,sum=0;j<i;j++){ if(i%j==0)sum=sum+j;}if(sum==i)Syste‎m.out.print‎ln("完数:"+i);}}}8.impor‎t java.util.*;publi‎c class‎E {publi‎c stati‎c void main (Strin‎g args[ ]) {int m,n;Scann‎e r scane‎r= new Scann‎e r(Syste‎m.in);Syste‎m.out.print‎ln("输入正数m‎回车确认");m = scane‎r.nextI‎n t();Syste‎m.out.print‎ln("输入正数n‎回车确认");n = scane‎r.nextI‎n t();int p=m;int q= n;int r = m%n;while‎(r!=0) {m = n;n =r;r =m%n;}Syste‎m.out.print‎ln(p+"和"+q+"的最大公约‎数"+n);Syste‎m.out.print‎ln(p+"和"+q+"的最小公倍‎数"+(p*q)/n);}}9.publi‎c class‎E{ publi‎c stati‎c void main(Strin‎g args[]){ int n=1;long sum=0;while‎(true){ sum=sum+n;n++;if(sum>=8888)break‎;}Syste‎m.out.print‎ln("满足条件的‎最大整数:"+(n-1));}}第四章习题‎1.用该类创建‎对象时。

Java语言程序设计(郑莉)一到八章课后习题答案

Java语言程序设计(郑莉)一到八章课后习题答案

第二章习题答案1.什么是对象、类,它们之间的联系?答:1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信息和与它交互的能力。

对象是一些属性及服务的封装体,在程序设计领域,可以用“对象=数据+作用于这些数据上的操作”来表示。

现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和相关方法的集合。

2)类是既有相同操作功能和相同的数据格式的对象的集合与抽象!两者的关系:对象是类的具体实例.。

2.什么是面向对象的程序设计方法?它有那些基本特征?答:面向对象程序设计从所处理的数据入手,以数据为中心而不是以服务为中心来描述系统。

它把编程问题视为一个数据集合,数据相对于功能而言,具有更强的稳定性。

它的特征:抽象,封装,继承,多态。

3.在下面的应用中,找出可能用到的对象,对每一个对象,列出可能的状态和行为。

1)模拟航空预订系统交易的程序2)模拟银行交易的程序答:1)航空预订交易:状态:旅客姓名,身份证号,联系号码,出发地址,抵达地址,出发日期。

行为:订票,领票,买票,退票。

2)银行交易:状态:客户姓名,账号,身份证号。

行为:存款,取款,汇款。

4.请解释类属性、实例属性及其区别。

答:实例属性,由一个个的实例用来存储所有实例都需要的属性信息,不同实例的属性值可能会不同。

5.请解释类方法、实例属性及其区别。

答:实例方法表示特定对象的行为,在声明时前面不加static修饰符,在使用时需要发送给一个类实例。

类方法也称为静态方法,在方法声明时前面需加static修饰符,类方法表示具体实例中类对象的共有行为。

区别:实例方法可以直接访问实例变量,调用实例方法,实例方法可以直接访问类变量,调用类方法;类方法可以直接调用类变量和类方法,类方法不能直接调用实例变量和实例方法;6.类的访问控制符有哪几种?具体含义及其区别。

答:类的访问控制符只有public(公共类)及无修饰符(默认类)两种。

区别:当使用public修饰符时表示所有其他的类都可以使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类可以使用类。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
return this.value==value;
}
public boolean equals(MyInteger value){
return value.equals(this.value);
}
public int parseInt(char[] cs){
String s=new String(cs);
}
int getValue(){
return value;
}
boolean isEven(){
return value%2==0?true:false;
}
boolean isOdd(){
return value%2!=0?true:false;
}
boolean isPrime(){
for(int i=2;i<value/2;i++)
if (value%i!=0)
return true;
return false;
}
boolean isEven(int value){
this.value=value;
return value%2==0?true:false;
}
boolean isOdd(int value){
this.value=value;
return value.isEven();
}
boolean isOdd(MyInteger value){
return value.isOdd();
}
boolean isPrime(MyInteger value){
return value.isPrime();
}
public boolean equals(int value){
System.out.println("\n是偶数吗?"+m1.isEven(13)+"\n是奇数吗?"+m1.isOdd(13)+"\n是素数吗?"+m1.isPrime(13));
System.out.println("\n是否相等:"+m1.equals(13)+" \n是否相等:"+m1.equals(13));
10.3 /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
return value%2!=0?true:false;
}
boolean isPrime(int value){
this.value=value;
for(int i=2;i<value/2;i++)
if (value%i==0)
return false;
return true;
}
boolean isEven(MyInteger value){
System.out.println("\n"+m1.parseInt("124"));
System.out.println("\n"+Integer.parseInt("sab"));
}
}
System.out.println("是偶数吗?"+m1.isEven()+"\n是奇数吗?"+m1.isOdd()+"\n是素数吗?"+m1.isPrime());
System.out.println("\n是偶数吗?"+m1.isEven(12)+"\n是奇数吗?"+m1.isOdd(12)+"\n是素数吗?"+m1.isPrime(12));
return parseInt(s);
}
public int parseInt(String s){
return Integer.valueOf(s);
}
}
void main(String[] args){
MyInteger m1=new MyInteger(11);
*/
/**
*
* @author Administrator
*/
class MyInteger{
int value;
String value1;
MyInteger(int value){
this.value=value;
}
MyInteger(String value1){
this.value1=value1;
相关文档
最新文档