《Java语言程序设计:基础篇》课后复习题答案-第二十章
- 格式:pdf
- 大小:100.99 KB
- 文档页数:2
第一章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概述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 程序设计》课后习题参考答案――武汉大学出版社习题1参考答案1.Java语言的特点有哪些?答:参考1.1.2防止直接访问数据变量看起来有些奇怪,但它实际上却对使用类的程序质量有极大的好处。
既然数据的单个项是不可访问的,那么惟一的办法就是通过方法来读或写。
因此,如果要求类成员内部的一致性,就应该通过类本身的方法来处理。
这种数据隐藏技术就是面向对象的重要特性——封装。
它将类的外部界面与类功能的实现区分开来,隐藏实现细节,(通过公共方法)保留有限的对外接口,迫使用户使用外部界面,通过访问接口实现对数据的操作。
即使实现细节发生了改变,还可通过界面承担其功能而保留原样,确保调用它的代码还继续工作,这使代码维护更简单。
2.简述封装的优点。
答:封装是一个简单而有效的思想,优点有:(1)模块化,对内成为一个结构完整、可进行自我管理、自我平衡、高度集中的整体。
(2)信息隐蔽,对外则是一个功能明确、接口单一、可在各种适合的环境下都能独立工作的有机单元。
面向对象的程序设计实现了对象的封装,使得用户不必关心诸如对象的行为是如何实现的这样一些细节。
通过对对象的封装,实现了模块化和信息隐藏,有利于程序的可移植性和安全性,同时也有利于对复杂对象的管理。
类的封装性使得代码的可重用性大为提高,这样的有机单元特别适合构建大型标准化的软件系统,具有很高的开发效率。
3.Java的基本工具有哪些?(1)javac 编译器(2)java 解释器(3)jdb Java 语言调试器(4)javadoc API文档管理器(5)javah 头文件生成器(6)appletviewer 小应用程序浏览器(7)javap 类文件反汇编器4.Java开发环境是如何配置的?答:对于Windows 2000以上版本的操作系统,可以打开[控制面板]窗口,双击其中的[系统]图标,在[系统特性]窗口中单击[高级]选项卡,进而单击[环境变量]按钮。
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基础入门》课后习题第1章Java开发入门一、填空题1、Java的三大体系分别是_JavaSE_____、_JavaEE_____、_JavaME_____。
2、Java程序的运行环境简称之为JRE_____。
3、编译Java程序需要使用_javac_____命令。
4、javac.exe和java.exe两个可执行程序存放在JDK安装目录的bin______目录下。
5、path______环境变量用来存储Java的编译和运行工具所在的路径,而_classpath_____环境变量则用来保存保存Java虚拟机要运行的“.class”文件路径。
二、选择题1、以下选项中,哪些属于JDK工具?(多选)ABCDA、Java编译器B、Java运行工具C、Java文档生成工具D、Java打包工具2、Java属于以下哪种语言?CA、机器语言B、汇编语言C、高级语言D、以上都不对3、下面哪种类型的文件可以在Java虚拟机中运行?DA、.javaB、.jreC、.exeD、.class4、安装好JDK后,在其bin目录下有许多exe可执行文件,其中java.exe命令的作用是以下哪一种?BA、Java文档制作工具B、Java解释器C、Java编译器D、Java启动器5、如果jdk的安装路径为:d:\jdk,若想在命令窗口中任何当前路径下,都可以直接使用javac和java命令,需要将环境变量path设置为以下哪个选项?B A.d:\jdk; B.d :\jdk\bin; C.d:\jre\bin; D.d:\jre;三、问答题1、简述Java的特点。
面向对象、跨平台性、健壮性、安全性、可移植性、多线程性、动态性等。
2、简述JRE与JDK的区别。
JRE(Java Runtime Environment,Java运行时环境),它相当于操作系统部分,提供了Java程序运行时所需要的基本条件和许多Java基础类,例如,IO类、GUI控件类、网络类等。
java语言程序设计基础篇第八版复习题答案Java语言程序设计基础篇第八版复习题答案# 开始语复习题是检验学习效果的重要手段,以下是针对《Java语言程序设计基础篇第八版》的一些复习题答案,旨在帮助同学们巩固和检验所学知识。
# 复习题及答案1. 简述Java语言的特点。
答案:Java是一种面向对象的编程语言,具有跨平台性、安全性、健壮性、多线程等特点。
它通过Java虚拟机(JVM)实现“一次编写,到处运行”的口号。
2. 什么是面向对象编程(OOP)?答案:面向对象编程是一种编程范式,它使用“对象”来设计软件,这些对象可以包含数据(属性)和代码(方法)。
OOP的核心概念包括封装、继承和多态。
3. 解释Java中的封装、继承和多态。
答案:- 封装:隐藏对象的内部状态和实现细节,只暴露有限的操作界面。
- 继承:允许一个类(子类)继承另一个类(父类)的属性和方法。
- 多态:允许不同类的对象对同一消息做出响应,但具体的行为会根据对象的实际类型而有所不同。
4. 描述Java中的基本数据类型及其范围。
答案:Java中的基本数据类型包括:- `byte`:8位有符号整数,范围从 -128 到 127。
- `short`:16位有符号整数,范围从 -32,768 到 32,767。
- `int`:32位有符号整数,默认类型,范围从 -2^31 到 2^31-1。
- `long`:64位有符号整数,范围从 -2^63 到 2^63-1,必须以 L 或l 结尾。
- `float`:32位单精度浮点数。
- `double`:64位双精度浮点数,默认浮点数类型。
- `char`:16位Unicode字符。
- `boolean`:只有两个可能的值:true 和 false。
- `void`:表示没有任何值。
5. 什么是Java的自动装箱和拆箱?答案:自动装箱是Java自动将基本数据类型转换为对应的包装类的过程,例如将`int`转换为`Integer`。
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程序,实现学生信息管理系统,要求包括以下功能:- 添加学生信息(姓名、年龄、性别、学号等);- 修改学生信息;- 删除学生信息;- 查询学生信息。
3.4import javax.swing.*;public class AdditionTutor{public static void main(String[] args){int number1=(int)(System.currentTimeMillis()%100);int number2=(int)(System.currentTimeMillis()*5%100);String answerString=JOptionPane.showInputDialog("what is "+ number1 +"+ "+ number2+" ?");int answer=Integer.parseInt(answerString);JOptionPane.showMessageDialog(null,number1 +" + "+ number2 +" = "+answer+" is "+(number1+number2==answer));}}3.10import javax.swing.JOptionPane;public class ComputeTaxWithSelectionStatement{public static void main(String[] args){//Prompt the user to enter filing statusString statusString = JOptionPane.showInputDialog("Enter the filing status:\n"+"(0-single filer,1-married jointly,\n"+"2-married separately,3-head of household)");int status = Integer.parseInt(statusString);//Prompt the user to enter taxable incomeString incomeString = JOptionPane.showInputDialog("Enter the taxable income:");double income = Double.parseDouble(incomeString);//Comput taxdouble tax=0;if (status == 0){//Compute tax for single filersif (income <= 6000)tax = income * 0.10;else if (income <= 27950)tax = 6000 * 0.10 + (income - 6000) * 0.15;else if (income <= 67700)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(income - 27950) * 0.27;else if (income <= 141250)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (income - 67700) * 0.30;else if (income <=307050)tax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (141250 - 67700) * 0.30 +(income - 141250) * 0.35;elsetax = 6000 * 0.10 + (27950 - 6000) * 0.15 +(67700 - 27950) * 0.27 + (141250 -67700) * 0.30 +(307050 - 141250) * 0.35 + (income - 307050) * 0.386;}else if (status == 1){//Compute tax for married file jointly if (income <= 12000)tax = income * 0.10;else if (income <= 46700)tax = 12000 * 0.10 + (income - 12000) * 0.15;else if (income <= 112850)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(income - 46700) * 0.27;else if (income <= 171950)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700) * 0.27 + (income - 112850) * 0.30;else if (income <= 307050)tax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700) * 0.27 + (141250 - 112850) * 0.30 +(income - 307050) * 0.35;elsetax = 12000 * 0.10 + (46700 - 12000) * 0.15 +(112850 - 46700 ) * 0.27 + (171950 - 112850) * 0.30 +(307050 - 171950) * 0.35 + (income - 307050) * 0.386;}else if (status == 2){//Compute tax for married separately if (income <= 6000)tax = income * 0.10;else if (income <= 23350)tax = 6000 * 0.10 + (income - 6000) * 0.15;else if (income <= 56425)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(income - 23350) * 0.27;else if (income <= 85975)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (income - 56425) * 0.30;else if (income <= 153525)tax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (85975 - 56425) * 0.30 +(income - 85975) * 0.35;elsetax = 6000 * 0.10 + (23350 - 6000) * 0.15 +(56425 - 23350) * 0.27 + (85975 - 56425) * 0.30 +(153525 - 85975) * 0.35 + (income - 153525) * 0.386;}else if (status == 3){//Compute tax for head of householdif (income <= 10000)tax = income * 0.10;else if (income <= 37450)tax = 10000 * 0.10 + (income - 10000) * 0.15;else if (income <= 96700)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(income - 37450) * 0.27;else if (income <= 156600)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (income - 96700) * 0.30;else if (income <= 307050)tax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (156600 - 96700) * 0.30 +(income - 156600) * 0.35;elsetax = 10000 * 0.10 + (37450 - 10000) * 0.15 +(96700 - 37450) * 0.27 + (156600 - 96700) * 0.30 +(307050 - 156600) * 0.35 + (income - 307050) * 0.386;}else{System.out.println("Error: invalid status");System.exit(0);}//Display the resultJOptionPane.showMessageDialog(null,"Tax is " +(int)(tax * 100) / 100.0);}}4.11public class ZhengChu{public static void main(String[] args){int count=0;for(int i=100;i<=200;i++){if((i%5==0||i%6==0)&&i%30!=0){System.out.print(" "+ i);count++;if(count%10==0)System.out.println();}}}}4.14public class ASCII{public static void main(String[] args){int count = 0;for(int i = 33; i <= 126; i++){count++;char ch = (char)i;System.out.print(" " + ch);if(count % 10 == 0)System.out.println();}}}4.17import javax.swing.JOptionPane;public class FindSalesAmount{/**Main method*/public static void main(String[] args){//The commission soughtString COMMISSION_SOUGHTString = JOptionPane.showInputDialog("Enter the COMMISSION_SOUGHT :");double COMMISSION_SOUGHT = Double.parseDouble(COMMISSION_SOUGHTString);double commission = 0;double salesAmount;for (salesAmount = 0.01;commission <= COMMISSION_SOUGHT;){salesAmount += 0.01; //防止犯off-by-one错误,先判断在做自加!if (salesAmount >= 10000.01)commission =5000 * 0.08 + 5000 * 0.1 + (salesAmount - 10000) * 0.12;else if (salesAmount >= 5000.01)commission = 5000 * 0.08 + (salesAmount - 5000) * 0.10;elsecommission = salesAmount * 0.08;}String output ="The sales amount $" + (int)(salesAmount * 100) / 100.0 +"\n is needed to make a commission of $" + COMMISSION_SOUGHT;JOptionPane.showMessageDialog(null,output);}}5.6import javax.swing.JOptionPane;public class PrintPyramid{public static void main(String[] args){String input = JOptionPane.showInputDialog("Enter the number of lines:");int numberOfLines = Integer.parseInt(input);displayPattern(numberOfLines);}public static void displayPattern(int n){for (int row = 1;row <= n;row++){for (int column = 1;column <= n - row;column++)System.out.print(" ");for (int num = row;num >= 1;num--)System.out.print((num >= 10) ? " " + num : " " + num);System.out.println();}}}5.18public class MathSuanFa{public static void main(String[] args){double A = Math.sqrt(4);double B = (int)Math.sin(2 * Math.PI);double C = (int)Math.cos(2 * Math.PI);double D = (int)Math.pow(2, 2);double E = (int)Math.log(Math.E);double F = (int)(Math.exp(1)*100000)/100000.0;double G = (int)Math.max(2, Math.min(3, 4));double H = (int)Math.rint(-2.5);double I = (int)Math.ceil(-2.5);double J = (int)Math.floor(-2.5);int K = (int)Math.round(-2.5f);int L = (int)Math.round(-2.5);double M = (int)Math.rint(2.5);double N = (int)Math.ceil(2.5);double O = (int)Math.floor(2.5);int P = (int)Math.round(2.5f);int Q = (int)Math.round(2.5);int R = (int)Math.round(Math.abs(-2.5));System.out.println(A +" "+ B +" "+ C +" "+ D +" "+ E +" "+ F +" "+ G +" "+ H +" "+ I +" "+ J +" "+ K +" "+ L +" "+ M +" "+ N +" "+ O +" "+ P +" "+ Q +" "+ R);}}6.9import javax.swing.JOptionPane;public class Array{public static void main (String[] args){String incomeString = JOptionPane.showInputDialog("Enter the number of array size:");int income = Integer.parseInt(incomeString);int arraySize = income;int [] myList = new int [arraySize];int m;for ( m = 0; m < arraySize; m++){String elementString = JOptionPane.showInputDialog("Enter the " + (m + 1) + " element of the array");int element = Integer.parseInt(elementString);myList[m] = element;}int minElement = myList[0];for (int i=1; i < arraySize; i++){if (myList[i] < minElement)minElement = myList[i];}JOptionPane.showMessageDialog(null,"The min element is: " + minElement); }}。
1. 2. 3. 4.Chapter 1 Introduction to Computers, Programs, and Java A computer is an electronic device that stores and processes data. A computerincludes both hardware and software. In general, hardware is the physical aspect of the computer that can be seen, and software is the invisible instructions that control the hardware and make it work. The hardware of a computer consists of a CPU,cache, memory, hard disk, floppy disk, monitor, printer, and communication devices. The machine language is a set of primitive instructions built into every computer. Assembly language is a low-level programming language in which a mnemonic is used to represent each of the machine language instructions. The high-level languages are English-like and easy to learn and program.The operating system (OS) is a program that manages and controls a computer’s activities. The examples of OS are Windows 98, NT, 2000, XP, or ME. Windows. Application programs such as an Internet browser and a word processor run on top of an operating system.100 in decimal => 64 in hex100 in decimal => 01100100 in binary4340 in decimal => 10F4 in hex4340 in decimal => 1000011110100 in binary2000 in decimal => 7D0 in hex2000 in decimal => 111110100005.1000011001 in binary => 219 in hex1000011001 in binary => 537 in decimal100000000 in binary => 100 in hex 100000000 in binary => 256 in decimal100111 in binary => 27 in hex 100111 in binary => 39 in decimal 6.FEFA9 in hex => 1044393 in decimalFEFA9 in hex => 11111110111110101001 in binary93 in hex => 147 in decimal 93 in hex => 10010011 in binary 2000 in hex => 8192 in decimal7.8.9.10.11.12.13.14.16.17. 2000 in hex => 10000000000000 in binaryDeveloped by a team led by James Gosling at Sun Microsystems in 1991. Originally called Oak, it became Java in 1995 when it was redesigned for developing Internet applications.Java can run on any platform with a Java Virtual Machine. The minimum requirement is the Java Runtime Environment, free from the .The input is the Java source code and the output is the Java bytecode (if compiled successfully).JBuilder by Borland, Sun ONE Studio by Sun, Café by WebGain, Visual Age for Java by IBM are the tools for developing Java programs, not dialects of Java. These tools make developing Java programs easier and more productive.HTML is a markup language for displaying static Web pages. Java is a full-fledged programming language that can be used to develop dynamic Web pages. The Java programs that run from a Web browser are called applets. Java applets must be embedded in HTML files using the <applet> tag.Keywords have specific meaning to the compiler and cannot be used for other purposes in the program such as variables or method names. Examples of keywords are class, static, and void.Java source code is case sensitive. Java keywords are always in lowercase.The source file extension is .java and the bytecode file extension is .class. Comments are used to document what a program is for and how a program is constructed. Comments help the programmers or users to communicate and understand the program. Comments are not programming statements and are ignored by the compiler. In Java, comments are preceded by two forward slashes (//) in a line or enclosed between /* and */ in multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.15. System.out.println(...);JOptionPane.showMessageDialog(null,“Hello world”);Line 2. Main should be main.Line 2. static is missing.Line 3: Welcome to Java! should be enclosed inside double quotation marks.javac is the JDK command to compile a program program. java is the JDK command to run a program.18.19. Java interpreter cannot find the .class file. Make sure you placed the .class in the right place, and invoked java command with appropriate package name.The class does not have a main method, or the signature of the main method is incorrect.Thank you for evaluating AdreamSoft PDF to Word You can only convert 3 pages with the trial versionTo get all the pages converted, you need to purchase the software from/purchase/buy-pdf-to-word.html。
《Java语言程序设计基础教程》练习思考题参考答案第1章Java程序设计概述1.9 练习思考题1、Java运行平台包括三个版本,请选择正确的三项:()A. J2EEB. J2MEC. J2SED. J2E解答:A,B,C2、Java JDK中反编译工具是:()A. javacB. javaC. jdbD.javap解答:D3、public static void main方法的参数描述是:()A. String args[]B. String[] argsC. Strings args[]D. String args解答:A,B4、在Java中,关于CLASSPATH环境变量的说法不正确的是:()A. CLASSPATH一旦设置之后不可修改,但可以将目录添加到该环境变量中。
B. 编译器用它来搜索各自的类文件。
C. CLASSPATH是一个目录列表。
D. 解释器用它来搜索各自的类文件。
解答:A5、编译Java Application源文件将产生相应的字节码文件,扩展名为()A. .javaB. .classC. .htmlD. .exe解答:B6、开发及运行Java程序需要经过的三个主要步骤为____________、____________和____________。
7、如果一个Java Applet源程序文件只定义有一个类,该类的类名为MyApplet,则类MyApplet必须是______类的子类并且存储该源程序文件的文件名为______。
8、如果一个Java Applet程序文件中定义有3个类,则使用Sun公司的JDK编译器编译该源程序文件将产生______个文件名及类名相同而扩展名为______的字节码文件。
9、开发及运行Java程序需要经过哪些主要步骤和过程?10、Java程序是由什么组成的?一个程序中必须要有public类吗?Java源文件的命名规则是怎么样的?11、编写一个简单的Java应用程序,该程序在命令行窗口输出两行文字:“你好,很高兴学习Java”和“We are students”。
Chapter20Recursion
1.A method that calls itself.One or more base cases(the simplest case)are used to stop recursion.Every recursive call reduces the original problem,bringing it increasingly close to a base case until it becomes that case.
2.f(n)=2if n=1
f(n)=2*2^(n-1)for(n>1)
3.f(n)=x if n=1
f(n)=x*x^(n-1)for(n>1)
4.f(n)=1if n=1
f(n)=f(n-1)+n for(n>1)
5.six times.(base case factorial(0))
6.25times(Why?
number of time fib is invoked in fib(0)=
1
number of time fib is invoked in fib(1)=
1
number of time fib is invoked in fib(2)=
1+number of time fib is invoked in fib(1)+number of time fib is invoked in fib(2)
=1+1+1=3
number of time fib is invoked in fib(3)=
1+number of time fib is invoked in fib(1)+number of time fib is invoked in fib(2)
=1+1+3=5
number of time fib is invoked in fib(4)=
1+number of time fib is invoked in fib(2)+number of time fib is invoked in fib(3)
=1+3+5=9
number of time fib is invoked in fib(5)=
1+number of time fib is invoked in fib(3)+number of time fib is invoked in fib(4)
=1+5+9=15
number of time fib is invoked in fib(6)=
1+number of time fib is invoked in fib(4)+number of time fib is invoked in fib(5)
=1+9+15=25
7.omitted.
8.omitted.
9.an overloaded method with additional parameters.
10.2^5–1
11.
∙Any recursive methods can be converted into a non-recursive method.(TRUE)
∙Recursive method usually takes more time and memory to execute than non-recursive methods.(TRUE)
∙Recursive methods are always simpler than non-recursive methods.(FALSE)
∙There is always a condition statement in a recursive method to check whether a base case is reached.(TRUE)
12.When a method is invoked,its contents are placed into a stack.If a method is
recursively invoked,it is possible that the stack space is exhausted.This causes
stack overflow.
13.(a)The output is15(5+4+3+2+1=15)
(b)7654321
14.(a)The output is54321
(b)The output is12345
15.(a)n is double.There is no guarantee that n!=0will be eventually false.
(b)Infinite recursion due to new Test()inside the constructor Test().
16.The isPalindrome method in Listing20.4,sort method in Listing20.5,and
binarySearch method in Listing20.6are tail-recursive.
17.
/**Return the Fibonacci number for the specified index*/
public static long fib(long index){
return fib(index,1,0);
}
/**Auxiliary tail-recursive method for fib*/
private static int fib(long index,int next,int result){
if(index==0)
return result;
else
return fib(index-1,next+result,next);
}。