java语言程序设计第二版习题答案
java语言程序设计教程第二版习题解答

4) 消息:消息是对象之间进行通信的一种数据结构。
5) 封装:封装性是保证软件部件具有优良的模块性的基础。面向对象的类是封装良好
的模块,类定义将其说明(用户可见的外部接口)与实现(用户不可见的内部实现)
显式地分开,其内部实现按其具体定义的作用域提供保护。
End of j loop;i=2
(7)Java系统要求对对象进行相容性检查,以防止不安全的类型转换。
(8) Java语言最强大的特性之一是它的平台独立性,Java可以处理好平台之间的
移植问题。
(9)Java语言中没有全局变量的定义,只能通过公用的静态的变量实现,从而减少了
引起错误的地方。
6.什么叫Java虚拟机?什么叫Java的字节码?
(3)的运行结果如下:
运行结果取决于随机数。求随机数的阶乘,其中的一个运行结果如下:
3!=6
(4)的运行结果如下:
0 0 0
0 0 1
0 0 2
End of k loop;j=0
0 1 0
0 1 1
6.阅读下列程序,写出运行结果。
(1).
public class Example0401{
public static void main(String[] args){
for(int x=0;x<10;x++){
int y=x*x+x+41';
System.out.println("\t"+x+"\t"+y);
2 0 1
java语言程序设计教程第二版(邱桃荣著)课后答案下载

java语言程序设计教程第二版(邱桃荣著)课后答案下载《JAVA语言程序设计教程》为Java语言基础教程,在讲解Java 语法的同时,介绍了用Eclipse软件实现Java可视化编程的方法。
以下是要与大家分享的java语言程序设计教程第二版(邱桃荣著),供大家参考!点击此处下载???java语言程序设计教程第二版(邱桃荣著)课后答案???《JAVA语言程序设计教程》可作为本科和大专院校计算机及相关专业的教材,也可作为Java培训教程,并可供广大Java编程学习者参考。
出版说明前言第1章Java编程概述1.1Java语言简介1.1.1Java语言的特点1.1.2Java虚拟机的运行过程1.2Java开发环境简介1.2.1开发工具包JDK1.2.2开发平台Eclipse1.3Java编程简介1.3.1创建Java项目1.3.2控制台程序开发1.3.3SWT应用程序开发1.3.4SWT中组件的帮助1.3.5Applet小程序开发1.4类和对象的初步介绍1.4.1Java的面向对象技术1.4.2类的定义与对象的创建 1.5习题第2章Java语言编程基础2.1数据类型、常量和变量2.1.1标识符和关键字2.1.2数据类型2.1.3基本数据类型2.1.4基本数据类型的转换2.1.5字符串类型2.2运算符与表达式2.2.1算术运算符与算术表达式 2.2.2关系运算符与关系表达式 2.2.3逻辑运算符与逻辑表达式 2.2.4赋值运算符与赋值表达式 2.2.5条件运算符与条件表达式 2.2.6位运算符与位表达式2.2.7运算符综述2.3控制语句2.3.1算法与语句概述2.3.2选择结构语句2.3.3循环结构语句2.4习题第3章Java的数组与方法 3.1数组3.1.1数组的基本概念3.1.2一维数组3.1.3二维数组3.1.4数组的复制与重置 3.2方法3.2.1方法的基本概念3.2.2方法的应用3.2.3方法的递归3.3习题第4章类与对象4.1类的定义4.1.1类定义的一般格式 4.1.2数据成员4.1.3成员方法4.1.4成员的访问控制4.1.5类的继承4.2对象4.2.1对象的创建4.2.2对象的使用4.3包4.3.1package语句4.3.2import语句4.3.3将类打包4.4接口4.4.1接口的声明与使用4.4.2接口回调4.4.3接口做参数4.5Java库常见类举例4.5.1基本类型的类包装4.5.2String类4.5.3Calendar类4.5.4Math类4.5.5其他4.6习题第5章SWT组件及事件处理 5.1SWT中的包5.2SWT的常用组件5.2.1按钮、复选框、单选按钮5.2.2文本框5.2.3下拉框(Combo类)5.2.4列表框(List类)5.3容器类5.3.1面板(Composite类)5.3.2分组框(Group类)5.3.3选项卡(TabFolder类和Tabltem类)5.3.4分隔窗(SashForm类)5.3.5带滚动条的面板(ScrolledComposite类) 5.3.6窗口容器(Shell类)5.4布局管理器5.4.1简介5.4.2填充布局(FillLayout类)5.4.3行布局(RowLayout类)5.4.4网格布局(GridLayout类)5.4.5堆栈布局(StackLayout类)5.4.6表格布局(FormLayout类)5.5习题第6章高级SVvT组件6.1工具栏与菜单6.1.1工具栏6.1.2动态工具栏6.1.3菜单6.2滚动条与画布6.2.1滚动条、刻度条、进度条6.2.2画布6.3表格、树与浏览器6.3.1表格6.3.2树、表格型树6.3.3浏览器(Browser类)6.4对话框6.4.1对话框(Dialog类)6.4.2消息提示对话框(MessageBox类)6.4.3文件选择对话框(FileDialog类)6.4.4目录选择对话框(DirectoryDialog类) 6.4.5颜色对话框(ColorDialog类)6.4.6字体对话框(FontDialog类)6.4.7打印对话框(PrintDialog类)6.4.8自定义对话框6.5习题第7章Java异常处理7.1Java的异常7.1.1常见异常7.1.2异常的基本概念7.1.3异常处理机制7.1.4抛出异常7.2内置异常类7.2.1异常类的层次和主要子类 7.2.2异常类的方法和属性7.2.3异常类的使用实例7.3自定义异常7.4习题第8章Java的输入,输出处理 8.1输入/输出流简介8.1.1流的概念8.1.2输入/输出类的层次8.1.3标准输入/输出8.2以字节为数据单位的流类 8.2.1文件输入/输出字节流8.2.2内存读写8.2.3过滤流与缓冲流8.3以字符为数据单位的流类 8.3.1输入/输出字符流8.3.2文件输入/输出字符流8.3.3缓冲字符流8.3.4打印输出字符流8.4文件处理与对象串行化8.4.1文件描述8.4.2文件访问8.4.3对象串行化8.5其他常用流8.5.1管道流8.5.2顺序流……第9章Java线程第10章JavaApplet小程序设计第11章综合实例……1.Java程序设计教程第2版(雍俊海著)课后答案下载2.java语言程序设计基础篇第六版英文版课后答案机械工业出版社3.jsp程序设计实例教程刘志成课后答案人民邮电出版社。
Java语言程序设 第2版 (郑莉)课后习题答案

Java语言程序设计第2版(郑莉)第二章习题答案1.什么是对象、类,它们之间的联系?答:1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信息和与它交互的能力。
对象是一些属性及服务的封装体,在程序设计领域,可以用“对象=数据+作用于这些数据上的操作”来表示。
现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和相关方法的集合。
2)类是既有相同操作功能和相同的数据格式的对象的集合与抽象!3)两者的关系:对象是类的具体实例.。
2.什么是面向对象的程序设计方法?它有那些基本特征?答:面向对象程序设计从所处理的数据入手,以数据为中心而不是以服务为中心来描述系统。
它把编程问题视为一个数据集合,数据相对于功能而言,具有更强的稳定性。
它的特征:抽象,封装,继承,多态。
3(无用)4.请解释类属性、实例属性及其区别。
答:实例属性,由一个个的实例用来存储所有实例都需要的属性信息,不同实例的属性值可能会不同。
5.请解释类方法、实例属性及其区别。
答:实例方法表示特定对象的行为,在声明时前面不加static修饰符,在使用时需要发送给一个类实例。
类方法也称为静态方法,在方法声明时前面需加static修饰符,类方法表示具体实例中类对象的共有行为。
区别:实例方法可以直接访问实例变量,调用实例方法,实例方法可以直接访问类变量,调用类方法;类方法可以直接调用类变量和类方法,类方法不能直接调用实例变量和实例方法;6.类的访问控制符有哪几种?具体含义及其区别。
答:类的访问控制符只有public(公共类)及无修饰符(默认类)两种。
区别:当使用public修饰符时表示所有其他的类都可以使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类可以使用类。
7类成员的访问控制符有哪几种?他们对类成员分别有哪些访问限制的作用?答:类成员的访问控制符有public,private,protecte及无修饰符.public(公有的):用public修饰的成分表示公有的,也就是它可以被其他任何对象访问(前提是对累成员所在的类访问有访问权限).Private(保护的):类中限定为private的成员只能被这个类本身访问,在类外不可见。
JAVA语言程序设计教程第二版习题解答

【答】: Java的运算符主要由算术运算符、关系运算符、条件运算符、位运算符、逻 辑运算符以及赋值运算符。表达式由运算符、操作数和方法调用,按照语言 的语法规则构造而成的符号序列。 表达式的结构是: 1)– –a%b++的结果是:1 2)(a>=1)&&a<=12?a:b)的结果是:1 3)f^(a>b)的结果是:false 4)(– –a)<<a的结果是:0 5.Java中标准输入输出使用哪种系统类、使用哪个对象和方法?
对象“汽车”与对象“小汽车”是什么关系,对象“汽车”与“轮胎”又是什么关 系?
【答】: 对象“汽车”与对象“小汽车”具有继承关系,即对象“小汽车”继承了对象“汽 车”。“轮胎”是对象“汽车”的一个属性,所以对象“汽车”包含“轮胎”,二者 是包含关系。 简述Java语言的主要特点。
【答】: Java语言的主要特点:(1)简单性 (2)面向对象 (3)分布式 (4)健 壮性 (5)结构中立 (6)安全性 (7)可移植 (8)解释的(9)高性能 (10) 多线程 (11)动态性 5.简述Java语言与C/C++语言的主要差异。
public class Example02{ public static void main(String[] args){ System.out.print(0) int fib0=0; int fib1=1; int fib2=fib0+fib1 while(fib2<100){ fib0=fib1; fib1=fib2; fib2=fib1+fib0; System.out.print(","+fib1); } } } (3).产生0~20的随机整型,然后计算并打印它的结果:
Java程序设计 精编教程(第2版)习题解答

习题解答习题一(第1章)1。
J ames Gosl ing2.需3个步骤:1) 用文本编辑器编写源文件。
2) 使用javac 编译源文件,得到字节码文件。
3) 使用解释器运行程序。
3.set classpath=D :\jd k\jre\lib\rt 、ja r;、;4、 B5、 J ava 源文件得扩展名就是、java ,J ava 字节码得扩展名就是、c lass. 6。
D.习题二(第2章)1.2。
Tea cher 、javap ublic class T eacher {d ouble add(double a ,do uble b ) {return a+b ;}doub le s ub(double a,dou ble b) {r et urn a-b ;}}Stu den t、javapublic cl ass Stud ent {pub lic void speak () {Syst em 、ou t、pri ntl n(”老师好");}}MainClass 、jav apu blic class MainClass {pu blic s tatic v oid m ain(String args []) {height bottomTeacherzhang=new Teacher();System、out、println(zhang、add(12,236));System、out、println(zhang、add(234,120));Student jiang=newStudent();jiang、speak();}}3.如果源文件中有多个类,但没有public类,那么源文件得名字只要与某个类得名字相同,并且扩展名就是、java就可以了,如果有一个类就是public类,那么源文件得名字必须与这个类得名字完全相同,扩展名就是、java.4。
行尾风格。
习题三(第3章)1.用来标识类名、变量名、方法名、类型名、数组名、文件名得有效字符序列称为标识符。
java程序设计教程第二版课后答案

java程序设计教程第二版课后答案【篇一:《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.2import java.util.*;class MyDate{private int year;private int month;private int day;public MyDate(int y,int m,int d){//构造函数,构造方法year=y;month=m;day=d;}//end public MyDate(int y,int m,int d)public int getYear(){//返回年return year;}//end getYear()public int getMonth(){//返回月return month;}//end getMonth()public int getDay(){//返回日return day;}//end getDay()}//end class MyDateclass Employee{private String name;private double salary;private MyDate hireDay;public Employee(String n,double s,MyDate d){name=n;salary=s;hireDay=d;}//end public Employee(String n,double s,MyDate d)public void print(){System.out.println("名字:"+name+"\n工资:"+salary+"\n雇佣年份:"+hireYear()+"\n");}//end print()public void raiseSalary(double byPercent){salary*=1+byPercent/100;}//endpublic int hireYear(){return hireDay.getYear();}}//end class Employeepublic class MyTestClass {public static void main(String[] args) {Employee[]staff=new Employee[3];staff[0]=new Employee("Harry Hacker",35000,new MyDate(1989,10,1));staff[1]=new Employee("Carl Carcker",75000,new MyDate(1987,12,15));staff[2]=new Employee("Tony Tester",38000,new MyDate(1990,3,12));int integerValue;System.out.println("The information of employee are:");for(integerValue=0;integerValue<=2;integerValue++){staff[integerValue].raiseSalary(5);}//end for()for(integerValue=0;integerValue<=2;integerValue++){staff[integerValue].print();}//end for()}//end main()}//end class MyTestClass//习题2.4import java.util.*;public class DataType {public static void main(String[] args) {boolean flag;char yesChar;byte finByte;int intValue;long longValue;short shortValue;float floatValue;double doubleValue;flag=true;yesChar='y';finByte=30;intValue=-7000;longValue=200l;shortValue=20000;floatValue=9.997E-5f;doubleValue=floatV alue*floatValue;System.out.println("the values are:");System.out.println("布尔类型变量flag="+flag);System.out.println("字符型变量yesChar="+yesChar);System.out.println("字节型变量finByte="+finByte);System.out.println("整型变量intValue="+intValue);System.out.println("长整型变量longValue="+longValue);System.out.println("短整型变量shortValue="+shortValue);System.out.println("浮点型变量floatValue="+floatValue);System.out.println("双精度浮点型变量doubleValue="+doubleValue); }//end main()}//习题2.9import java.util.*;class PubTest1{private int ivar1;private float fvar1,fvar2;public PubTest1(){fvar2=0.0f;}public float sum_f_I(){fvar2=fvar1+ivar1;return fvar2;}public void print(){System.out.println("fvar2="+fvar2);}public void setIvar1(int ivalue){ivar1=ivalue;}public void setFvar1(float ivalue){fvar1=ivalue;}}public class PubMainTest {public static void main(String[] args) {PubTest1 pubt1=new PubTest1();pubt1.setIvar1(10);pubt1.setFvar1(100.02f);pubt1.sum_f_I();pubt1.print();}}//习题2.10import java.util.*;class Date {private int year;private int month;private int day;public Date(int day, int month, int year) { //构造函数,构造方法this.year = year;this.month = month;this.day = day;} //end public MyDate(int y,int m,int d)public int getYear() { //返回年return year;} //end getYear()public int getMonth() { //返回月return month;} //end getMonth()public int getDay() { //返回日return day;} //end getDay()} //end class Datepublic class Teacher {String name;//教师名字boolean sex;//性别,true表示男性Date birth;//出生日期String salaryID;//工资号String depart;//教师所在系所String posit;//教师职称String getName() {return name;}void setName(String name) { = name;}boolean getSex() {return sex;}void setSex(boolean sex) {this.sex = sex;}Date getBirth() {return birth;}void setBirth(Date birth) {this.birth = birth;}String getSalaryID() {return salaryID;}void setSalaryID(String salaryID) {this.salaryID = salaryID;}String getDepart() {return depart;}void setDepart(String depart) {this.depart = depart;}String getPosit() {return posit;}void setPosit(String posit) {this.posit = posit;}public Teacher(){System.out.println("父类无参数的构造方法!!!!!!!"); }//如果这里不加上这个无参数的构造方法将会出错!!!!public Teacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit){ =name;this.sex=sex;this.birth=birth;this.salaryID=salaryid;this.depart=depart;this.posit=posit;}//end Teacher()public void print(){System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if(this.getSex()==false){System.out.println("女");}else{System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear()+"-"+this.getBirth().getMonth()+"-"+this.getBirth().getDay()); System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());}//end print()public static void main(String[] args) {Date dt1=new Date(11,23,1989);Date dt2=new Date(2,6,1975);Date dt3=new Date(11,8,1964);Date dt4=new Date(10,4,1975);Date dt5=new Date(8,9,1969);//创建各系教师实例,用来测试Teacher t1=new Teacher("王莹",false,dt1,"123","经济学","prefessor");ResearchTeacher rt=new ResearchTeacher("杨zi青",true,dt2,"421","软件工程","associate prefessor","software");LabTeacher lat=new LabTeacher("王夏瑾",false,dt3,"163","外语","pinstrucor","speech lab");LibTeacher lit=new LibTeacher("马二孩",true,dt4,"521","大学物理","prefessor","physicalLib");AdminTeacher at=new AdminTeacher("王xi",false,dt5,"663","环境","prefessor","dean");/////////分别调用各自的输出方法,输出相应信息////////////////////////////System.out.println("-------------------------------");t1.print();//普通教师信息System.out.println("-------------------------------");rt.print();//研究系列教师信息System.out.println("-------------------------------");lat.print();//普通教师信息System.out.println("-------------------------------");lit.print();//实验系列教师信息System.out.println("-------------------------------");at.print();//行政系列教师信息System.out.println("-------------------------------");}//end main()}//end public class Teacherclass ResearchTeacher extends Teacher{private String resField;public ResearchTeacher(String name, boolean sex, Date birth, String salaryid,String depart, String posit, String resField) { = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.resField = resField;} //end public ResearchTeacher(){}String getResField(){return resField;}void setResField(String resField){this.resField=resField;}public void print() {System.out.print("research teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'resField:");System.out.println(this.getResField());} //end print()}//end class ResearchTeacherclass LabTeacher extends Teacher{private String labName;public LabTeacher(String name, boolean sex, Date birth,String salaryid, String depart,String posit, String labName) { = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;bName = labName;} //end public ResearchTeacher(){}String getLabName(){return labName;}void setLabName(String labName){bName=labName;}public void print() {System.out.print("lab teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'labName:");System.out.println(bName);} //end print()}//end class LabTeacherclass LibTeacher extends Teacher{private String libName;public LibTeacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit,String libName){ = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.libName=libName;}//end public ResearchTeacher(){}String getLibName(){return libName;}void setLibName(String libName){this.libName=libName;}public void print() {System.out.print("lib teacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'libName:");System.out.println(this.libName);} //end print()}//end class LibTeacherclass AdminTeacher extends Teacher{private String managePos;public AdminTeacher(String name,boolean sex,Date birth,String salaryid,String depart,String posit,String managePos){ = name;this.sex = sex;this.birth = birth;this.salaryID = salaryid;this.depart = depart;this.posit = posit;this.managePos=managePos;}//end public ResearchTeacher(){}String getManagePos(){return managePos;}void setManagePos(String managePos){this.managePos=managePos;}public void print() {System.out.print("adminteacher info is:");System.out.print("the teacher'name:");System.out.println(this.getName());System.out.print("the teacher'sex:");if (this.getSex() == false) {System.out.println("女");}else {System.out.println("男");}System.out.print("the teacher'birth:");System.out.println(this.getBirth().getYear() + "-" +this.getBirth().getMonth() + "-" +this.getBirth().getDay());System.out.print("the teacher'salaryid:");System.out.println(this.getSalaryID());System.out.print("the teacher'posit:");System.out.println(this.getPosit());System.out.print("the teacher'depart:");System.out.println(this.getDepart());System.out.print("the teacher'managePos:");System.out.println(this.managePos);} //end print()}//end class AdminTeacher习题2.11public class Course {private String courseID;private String courseName;private String courseType;private int classHour;private float credit;public Course(String courseID, String courseName, String courseType,int classHour, float credit) {this.courseID=courseID;this.courseName=courseName;this.courseType=courseType;this.classHour=classHour;this.credit=credit;}//end public Course(){}String getID() {return courseID;}void setID(String id) {this.courseID = id;}String getName() {return courseName;}void setName(String name) {this.courseName = name;}String getType() {return courseType;}void setType(String type) {this.courseType = type;}int getClassHour() {return classHour;}void setClassHour(int hour) {this.classHour = hour;}float getCredit() {return classHour;}void setCredit(float credit) {this.credit= credit;}public void print(){System.out.println("the basic info of this course as followed:");System.out.println("courseID="+this.getID());System.out.println("courseName="+this.getName());System.out.println("courseType="+this.getType());System.out.println("classHour="+this.getClassHour());System.out.println("credit="+this.getCredit());}public static void main(String[] args) {Course cs=new Course("d12","java程序设计(第二版)","cs",64,3.0f);System.out.println("----------------------------------");cs.print();System.out.println("修改课程学分为4.0f");cs.setCredit(4);cs.print();}}//习题2.12public class MyGraphic {String lineColor;String fillColor;MyGraphic(String lc,String fc){this.lineColor=lc;this.fillColor=fc;}void print(){System.out.println("line color is "+this.lineColor+"\t fill color is "+this.fillColor);}public static void main(String[] args) {float rd=(float)4.5;MyCircle mc=new MyCircle(rd,"black","white");MyRectangle mr=new MyRectangle(4,6,"red","blue");System.out.println("Circle info ");mc.print();System.out.println("circumference is " + mc.calCircum());System.out.println("square is " + mc.calSquare());System.out.println("rectangle info: ");mr.print();System.out.println("circumference is " + mr.calCircum());System.out.println("square is " + mr.calSquare());}//end main(){}}//end public class MyGraphicclass MyRectangle extends MyGraphic{float rLong;float rWidth;MyRectangle (float rl,float rw,String lc,String fc){super(lc,fc);this.rLong=rl;this.rWidth=rw;}//end MyRectangle (){}float calCircum(){return ((float)((this.rLong+this.rWidth)*2));}float calSquare(){return ((float)(this.rLong*this.rWidth));}}//end class MyRectangleclass MyCircle extends MyGraphic{float radius;MyCircle (float rd,String lc,String fc){super(lc,fc);this.radius=rd;}//end MyRectangle (){}float calCircum(){return (float)((this.radius*3.12*2));}float calSquare(){return ((float)(this.radius*this.radius*3.14));}}//end class MyCircle//习题2.13public class Vehicle {String brand;String color;int price;int number;public Vehicle(String b, String c) {this.brand = b;this.color = c;}public Vehicle(String b, String c, int p, int n) {this(b, c);this.price = p;this.number = n;}void print() {System.out.println("\n-------------------------");System.out.println("the vehicle info as followed :");System.out.println("brand=" + this.brand + "\t");System.out.println("color=" + this.color + "\t");System.out.println("price=" + this.price + "\t");System.out.println("number=" + this.number + "\t"); } //end void print()public static void main(String[] args) {V ehicle c1=new Vehicle("vehicle1","white");V ehicle c2=new Vehicle("vehicle2","white",300,1);Car cr=new Car("car1","red",300,4,400);Truck tk2=new Truck("truck1","black",300,400);c1.print();c2.print();cr.print();tk2.print();} //end main()} //end public class Vehicleclass Car extends Vehicle{int speed;Car(String b, String c, int p, int n,int s){super(b,c,p,n);this.speed=s;}void print(){super.print();System.out.print("speed="+this.speed); }}//end class Carclass Truck extends Vehicle{int speed;int weight;Truck(String b, String c, int s,int w){super(b,c);this.speed=s;this.weight=w;}void print(){super.print();System.out.print("speed="+this.speed);System.out.print("weight="+this.weight); }}//end class Truck//习题3.3public class Test {public static void main(String[] args) {int b1=1;int b2=1;System.out.println("b1=" + b1);System.out.println("b2=" + b2);b1<<=31;b2<<=31;System.out.println("b1=" + b1);System.out.println("b2=" + b2);b1 >>= 31;System.out.println("b1=" + b1);b1 >>= 1;System.out.println("b1=" + b1);b2 >>>= 31;System.out.println("b2=" + b2);b2 >>>= 1;System.out.println("b2=" + b2);}}//习题3.4public class Factorial {private int result,initVal;public static int Factorial(int n){if(n==0){return 1;}return n*Factorial(n-1);}public void print(){System.out.println(initVal+"!="+result); }public void setInitVal(int n){initVal=n;}public static void main(String[] args) {Factorial ff=new Factorial();for(int i=0;i<=4;i++){ff.setInitVal(2*(i+1));ff.result=Factorial(ff.initVal);ff.print();}//end for()}//end main()}//end public class Factorialpublic class Factorial2 {private int result,initVal;public void print(){System.out.println(initVal+"!="+result);}public void setInitVal(int n){initVal=n;}public static void main(String[] args) {Factorial2 ff=new Factorial2();for(int i=0;i<=4;i++){ff.setInitVal(2*(i+1));ff.result=1;for(int j=2;j<=ff.initV al;j++){ff.result*=j;}ff.print();}//end for()}//end main()}//习题3.5public class MathRandomTest {public static void main(String[] args) {int count=0,MAXof100,MINof100;int num,i;MAXof100=(int)(100*Math.random());MINof100=(int)(100*Math.random());System.out.print(MAXof100+" ");System.out.print(MINof100+" ");if(MAXof100>50)count++;if(MINof100>50)count++;if( MAXof100<MINof100){num=MINof100;MINof100=MAXof100;MAXof100=num;}//end if()for(i=0;i<98;i++){num=(int)(100*Math.random());System.out.print(num+((i+2)%10==9?"\n":" "));if(num>MAXof100){MAXof100=num;}else if(num<MINof100){MINof100=num;}if(num>50){count++;}}//end for()System.out.println("the max of 100 random integers is "+MAXof100);System.out.println("the min of 100 random integers is "+MINof100);System.out.println("the number of random more than50 is "+count); }//end main()}//end public class MathRandomTest//习题3.7public class PrintAst {public void printAstar() {System.out.print("*");}public void printSpace() {System.out.print(" ");}public static void main(String[] args) {PrintAst pa = new PrintAst();int initNum = 13;for (int i = 1; i <= initNum / 2 + 1; i++) {for (int n = 1; n <= i; n++) {pa.printSpace();pa.printSpace();}for (int m = 1; m <= initNum - 2 * i + 2; m++) {pa.printSpace();pa.printAstar();}System.out.println();} //end forif (initNum % 2 == 0) {for (int i = 1; i <= initNum / 2; i++) {pa.printSpace();pa.printSpace();}pa.printSpace();pa.printAstar();pa.printSpace();pa.printAstar();System.out.println();}for (int i = initNum / 2 + 2; i <= initNum; i++) {for (int n = 1; n <= initNum - i + 1; n++) {pa.printSpace();pa.printSpace();}for (int m = 1; m <= 2 * i - initNum; m++) {pa.printSpace();pa.printAstar();}System.out.println();} //end forSystem.out.println();} //end main()} //end public class PrintAst//习题3.8public class PrintTriag {public void printAstar() {System.out.print("*");}public static void main(String[] args) {int initLine = 10;int initNum = 10;PrintTriag pt = new PrintTriag();for (int i = 0; i < initLine; i++) {for (int j = 0; j < initNum - i; j++) {pt.printAstar();}System.out.println();}}//end main()}//end public class PrintTriag习题3.9import java.util.*;public class MultipleTable {public void printFormula(int i,int j,int res){System.out.print(i+"*"+j+"="+res+" "); }public static void main(String[] args) {MultipleTable mt=new MultipleTable();int initNum=9;int res=0;for(int i=1;i<=initNum;i++){for(int j=1;j<=i;j++){res=i*j;mt.printFormula(i,j,res);}System.out.println();}//end for}//end main()}//end public class MultipleTable习题3.10import java.io.*;public class HuiWen {boolean isHuiWen(char str[], int n) {int net = 0;int i, j;for (i = 0, j = n - 1; i < n / 2; i++, j--) {if (str[i] == str[j]) {net++;} //end if} //end forif (net == (int) (n / 2)) {return true;} //end ifelse {return false;}} //end boolean isHuiWen(char str[], int n)public static void main(String[] args) {HuiWen hw1 = new HuiWen();String pm = "";try {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your test string:\n");pm = input.readLine();System.out.println(pm);} //end trycatch (IOException e) {System.out.print(e);} //end catchboolean bw = hw1.isHuiWen(pm.toCharArray(), pm.length());if (bw == true) {System.out.println("是回文");}else {System.out.println("不是回文");}} //end main()} //end public class HuiWenimport java.io.*;public class HuiWen2 {String reverse(String w1){String w2;char[]str1=w1.toCharArray();int len=w1.length();char[]str2=new char[len];for(int i=0;i<len;i++){str2[i]=str1[len-1-i];}w2=new String(str2);return w2;}public static void main(String[] args) {HuiWen2 hw1 = new HuiWen2();String pm = "";try {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your test string:\n");pm = input.readLine();} //end trycatch (IOException e) {System.out.print(e);} //end catchString w2=hw1.reverse(pm);if(pareTo(pm)==0){System.out.println("是回文");}else {System.out.println("不是回文");}}}//习题3.11import java.io.*;public class PrimeNumber {private int pm;public void setPm(int pm){this.pm=pm;}public boolean isPrime(){boolean bl=true;int i=2;for(i=2;i<=Math.sqrt(pm);){if(pm%i==0){bl=false;break;}else{i++;}}//end forreturn bl;}//end public boolean isPrime()public static void main(String[] args) {PrimeNumber prim=new PrimeNumber();int testNum=0;try{InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your test number:\n");testNum=Integer.parseInt(input.readLine());}//end trycatch(IOException e){System.out.println(e);}//end catchprim.setPm(testNum);boolean bl=prim.isPrime();if(bl==true){System.out.println(testNum+"是质数");}else {System.out.println(testNum+"不是质数");}}//end main}//end public class PrimeNumber习题3.12import java.io.*;public class Tempconverter {double celsius(double y){return ((y-32)/9*5);}public static void main(String[] args) {Tempconverter tc=new Tempconverter ();double tmp=0;try{InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("give your fahrenheit number:\n");tmp=Double.parseDouble(input.readLine());}//end trycatch(NumberFormatException e){System.out.println(e);}//end catchcatch(IOException e){System.out.println(e);}//end catchSystem.out.println("the celsius of temperature is "+tc.celsius(tmp));}//end main()}//end public class Tempconverter习题3.13import java.io.*;public class Trigsquare {double x, y, z;Trigsquare(double x, double y, double z) {this.x = x;this.y = y;this.z = z;}boolean isTriangle() {boolean bl = false;if (this.x > 0 && this.y > 0 && this.z > 0) {if ( (this.x + this.y) > this.z && (this.x + this.z) > this.y && (this.z + this.y) > this.x) {bl = true;} //ebd ifelse {bl = false;} //end else} //end if(this.x>0&&this.y>0&&this.z>0)return bl;} //end boolean isTriangle()double getArea() {double s = (this.x + this.y + this.z) / 2.0;return (Math.sqrt(s * (s - this.x) * (s - this.y) * (s - this.z)));} //end double getArea()public static void main(String[] args) {double s[] = new double[3];try {InputStreamReader reader = new InputStreamReader(System.in);BufferedReader input = new BufferedReader(reader);System.out.print("输入三角形的三边的长度:\n");。
java语言程序设计教程第二版习题解答

习题一1.简述面向对象软件开发方法的重要意义。
【答】:面向对象的软件开发方法按问题论域来设计模块,以对象代表问题解的中心环节,力求符合人们日常的思维习惯,采用―对象+消息‖的程序设计模式,降低或分解问题的难度和复杂性,从而以较小的代价和较高的收益获得较满意的效果,满足软件工程发展需要。
2.解释下面几个概念:1)对象2)实例3)类 4)消息 5)封装 6)继承 7)多态【答】:1)对象:就是现实世界中某个具体的物理实体在计算机中的映射和体现,是由属性和操作所构成的一个封闭整体。
2)实例:是对象在计算机内存中的映像。
3)类:是描述对象的―基本原型‖,是描述性的类别或模板,即对一组对象的抽象。
它定义一组对象所能拥有的共同特征,用以说明该组对象的能力与性质。
4)消息:消息是对象之间进行通信的一种数据结构。
5)封装:封装性是保证软件部件具有优良的模块性的基础。
面向对象的类是封装良好的模块,类定义将其说明(用户可见的外部接口)与实现(用户不可见的内部实现)显式地分开,其内部实现按其具体定义的作用域提供保护。
6)继承:继承性是子类自动共享父类数据结构和方法的机制,这是类之间的一种关系。
7)多态:多态性是指一个名字具有多种语义,即指同一消息为不同对象所接受时,可以导致不同的操作。
3.对象―汽车‖与对象―小汽车‖是什么关系,对象―汽车‖与―轮胎‖又是什么关系?【答】:对象―汽车‖与对象―小汽车‖具有继承关系,即对象―小汽车‖继承了对象―汽车‖。
―轮胎‖是对象―汽车‖的一个属性,所以对象―汽车‖包含―轮胎‖,二者是包含关系。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java语言程序设计第二版习题答案
Java语言程序设计第二版习题答案
Java语言程序设计是一门广泛应用于软件开发领域的编程语言。
无论是初学者还是有经验的开发人员,都可以通过学习Java语言来提升自己的编程能力。
为了帮助读者更好地掌握Java语言的知识,本文将提供《Java语言程序设计第二版》中一些习题的答案,并对其中一些重要的概念进行解释和讨论。
第一章:计算机、程序和Java
1.1 问题:
编写一个Java程序,输出“Hello, World!”。
答案:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```
1.2 问题:
Java应用程序的执行过程是怎样的?
答案:
Java应用程序的执行过程可以分为三个阶段:编辑、编译和运行。
首先,我们使用文本编辑器编写Java源代码文件,文件的扩展名为.java。
然后,使用Java
编译器将源代码文件编译成字节码文件,文件的扩展名为.class。
最后,使用Java虚拟机(JVM)加载字节码文件并执行程序。
第二章:基本程序设计
2.1 问题:
编写一个Java程序,计算两个整数的和。
答案:
```java
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println("The sum is " + sum);
}
}
```
2.2 问题:
什么是变量?如何在Java中声明和使用变量?
答案:
变量是用于存储数据的内存位置。
在Java中,可以使用关键字`int`、`double`、`char`等来声明变量的类型,并使用赋值操作符`=`将值赋给变量。
例如,`int num = 10;`声明了一个整型变量`num`并将其值设置为10。
在后续的代码中,可以使用变量名来引用该变量的值。
第三章:选择结构和循环结构
3.1 问题:
编写一个Java程序,判断一个年份是否为闰年。
答案:
```java
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
```
3.2 问题:
什么是循环结构?Java中有哪些循环结构?
答案:
循环结构是一种重复执行特定代码块的结构。
在Java中,有三种常用的循环结构:`for`循环、`while`循环和`do-while`循环。
`for`循环适用于已知循环次数的情况,`while`循环适用于未知循环次数但有条件的情况,`do-while`循环适用于至少执行一次的情况。
第四章:方法
4.1 问题:
编写一个Java方法,计算两个整数的最大公约数。
答案:
```java
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
int gcd = findGCD(num1, num2);
System.out.println("The greatest common divisor is " + gcd); }
public static int findGCD(int num1, int num2) {
int gcd = 1;
int k = 2;
while (k <= num1 && k <= num2) {
if (num1 % k == 0 && num2 % k == 0) {
gcd = k;
}
k++;
}
return gcd;
}
}
```
4.2 问题:
什么是方法?如何在Java中声明和调用方法?
答案:
方法是一组执行特定任务的语句的集合。
在Java中,可以使用关键字`void`或
方法的返回类型来声明方法,并使用方法名来调用方法。
例如,`public static void printHello()`声明了一个没有参数和返回值的方法,可以使用`printHello()`来调用该方法。
如果方法需要接收参数,可以在方法名后的括号中指定参数的类
型和名称,例如`public static void printMessage(String message)`。
调用带有参
数的方法时,需要提供相应的参数。
通过以上习题的答案,读者可以更好地理解和掌握《Java语言程序设计第二版》中的内容。
编程是一门实践性很强的学科,除了理论知识外,更重要的是通过
不断练习和实践来提升自己的编程能力。
希望读者能够通过学习Java语言,掌
握编程的基本技巧,并能够应用于实际项目中。