java语言程序设计第二版习题答案
- 格式:docx
- 大小:4.28 KB
- 文档页数:6
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版(郑莉)第二章习题答案1.什么是对象、类,它们之间的联系?答:1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信息和与它交互的能力。
对象是一些属性及服务的封装体,在程序设计领域,可以用“对象=数据+作用于这些数据上的操作”来表示。
现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和相关方法的集合。
2)类是既有相同操作功能和相同的数据格式的对象的集合与抽象!3)两者的关系:对象是类的具体实例.。
2.什么是面向对象的程序设计方法?它有那些基本特征?答:面向对象程序设计从所处理的数据入手,以数据为中心而不是以服务为中心来描述系统。
它把编程问题视为一个数据集合,数据相对于功能而言,具有更强的稳定性。
它的特征:抽象,封装,继承,多态。
3(无用)4.请解释类属性、实例属性及其区别。
答:实例属性,由一个个的实例用来存储所有实例都需要的属性信息,不同实例的属性值可能会不同。
5.请解释类方法、实例属性及其区别。
答:实例方法表示特定对象的行为,在声明时前面不加static修饰符,在使用时需要发送给一个类实例。
类方法也称为静态方法,在方法声明时前面需加static修饰符,类方法表示具体实例中类对象的共有行为。
区别:实例方法可以直接访问实例变量,调用实例方法,实例方法可以直接访问类变量,调用类方法;类方法可以直接调用类变量和类方法,类方法不能直接调用实例变量和实例方法;6.类的访问控制符有哪几种?具体含义及其区别。
答:类的访问控制符只有public(公共类)及无修饰符(默认类)两种。
区别:当使用public修饰符时表示所有其他的类都可以使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类可以使用类。
7类成员的访问控制符有哪几种?他们对类成员分别有哪些访问限制的作用?答:类成员的访问控制符有public,private,protecte及无修饰符.public(公有的):用public修饰的成分表示公有的,也就是它可以被其他任何对象访问(前提是对累成员所在的类访问有访问权限).Private(保护的):类中限定为private的成员只能被这个类本身访问,在类外不可见。
习题解答习题一(第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程序设计》课后习题参考答案】参考答案――武汉大学出版社习题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程序设计》(第二版)的全部课后答案,不包括简答题,概念题,只是程序设计题目。
//习题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");。
习题一1.简述面向对象软件开发方法的重要意义。
【答】:面向对象的软件开发方法按问题论域来设计模块,以对象代表问题解的中心环节,力求符合人们日常的思维习惯,采用―对象+消息‖的程序设计模式,降低或分解问题的难度和复杂性,从而以较小的代价和较高的收益获得较满意的效果,满足软件工程发展需要。
2.解释下面几个概念:1)对象2)实例3)类 4)消息 5)封装 6)继承 7)多态【答】:1)对象:就是现实世界中某个具体的物理实体在计算机中的映射和体现,是由属性和操作所构成的一个封闭整体。
2)实例:是对象在计算机内存中的映像。
3)类:是描述对象的―基本原型‖,是描述性的类别或模板,即对一组对象的抽象。
它定义一组对象所能拥有的共同特征,用以说明该组对象的能力与性质。
4)消息:消息是对象之间进行通信的一种数据结构。
5)封装:封装性是保证软件部件具有优良的模块性的基础。
面向对象的类是封装良好的模块,类定义将其说明(用户可见的外部接口)与实现(用户不可见的内部实现)显式地分开,其内部实现按其具体定义的作用域提供保护。
6)继承:继承性是子类自动共享父类数据结构和方法的机制,这是类之间的一种关系。
7)多态:多态性是指一个名字具有多种语义,即指同一消息为不同对象所接受时,可以导致不同的操作。
3.对象―汽车‖与对象―小汽车‖是什么关系,对象―汽车‖与―轮胎‖又是什么关系?【答】:对象―汽车‖与对象―小汽车‖具有继承关系,即对象―小汽车‖继承了对象―汽车‖。
―轮胎‖是对象―汽车‖的一个属性,所以对象―汽车‖包含―轮胎‖,二者是包含关系。
java程序设计第二版课后答案Java程序设计第二版课后答案涵盖了多个章节,每个章节都包含了不同的编程概念和练习题。
以下是一些常见章节的课后答案概要,以供参考:第1章:Java简介- 1.1 Java的起源和特点- 1.2 Java平台的组成- 1.3 Java开发环境的搭建第2章:基本语法- 2.1 数据类型- 2.2 变量声明- 2.3 运算符- 2.4 控制语句(if, switch, loop)第3章:控制流程- 3.1 条件语句(if-else, switch-case)- 3.2 循环语句(for, while, do-while)- 3.3 跳转语句(break, continue, return)第4章:数据结构- 4.1 数组的定义和使用- 4.2 字符串的处理- 4.3 集合框架简介第5章:面向对象编程- 5.1 类和对象- 5.2 构造方法- 5.3 继承- 5.4 封装和多态第6章:异常处理- 6.1 异常的概念- 6.2 异常的分类- 6.3 异常的处理方式(try-catch-finally)第7章:输入输出- 7.1 标准输入输出- 7.2 文件输入输出- 7.3 序列化第8章:Java集合框架- 8.1 集合的基本概念- 8.2 List接口及其实现- 8.3 Set接口及其实现- 8.4 Map接口及其实现第9章:泛型- 9.1 泛型的概念- 9.2 泛型的使用- 9.3 泛型的限制第10章:多线程- 10.1 线程的概念- 10.2 创建和启动线程- 10.3 线程的同步第11章:网络编程- 11.1 网络编程基础- 11.2 Socket编程- 11.3 URL和URLConnection第12章:图形用户界面- 12.1 AWT和Swing- 12.2 事件处理- 12.3 布局管理器第13章:Java数据库连接- 13.1 JDBC基础- 13.2 数据库连接和操作- 13.3 SQL语句的使用第14章:Java Web应用- 14.1 Servlet基础- 14.2 JSP技术- 14.3 MVC架构模式每个章节的课后答案通常包括理论问题和编程练习题的解答。
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以上版本的操作系统,可以打开[控制面板]窗口,双击其中的[系统]图标,在[系统特性]窗口中单击[高级]选项卡,进而单击[环境变量]按钮。
习题解答习题一(第1章)1.2.需3个步骤:1) 用文本编辑器编写源文件。
2) 使用编译源文件,得到字节码文件。
3) 使用解释器运行程序。
3. :\\\\;.;4. B5. 源文件的扩展名是,字节码的扩展名是。
6.D 。
习题二(第2章)1.2. { ( b) {;}( b) {;}}{() {("老师好");}}{( []) {();((12,236));((234,120));();();}}3.如果源文件中有多个类,但没有类,那么源文件的名字只要和某个类的名字相同,并且扩展名是就可以了,如果有一个类是类,那么源文件的名字必须与这个类的名字完全相同,扩展名是。
4.行尾风格。
习题三(第3章)1.用来标识类名、变量名、方法名、类型名、数组名、文件名的有效字符序列称为标识符。
标识符由字母、下划线、美元符号和数字组成,第一个字符不能是数字。
不是标识符。
2.关键字就是语言中已经被赋予特定意义的一些单词,不可以把关键字作为名字来用。
不是关键字。
3.,,,,,,,。
4.属于操作题,解答略。
5.属于操作题,解答略。
6. E {( [ ]) {'A''Z';( <)(" ");}}7.不可以。
习题四(第4章)1.110。
不规范。
2.新亲亲斤!!。
3.{( ) {(913112) {("是三等奖");}(20959627) {("是二等奖");}(87531659) {("是一等奖");{("未中奖");}}}4.;{( []) {();= 0; 存放电量= 0; 用户需要交纳的电费("输入电量:");();( <= 90 >=1){= *0.6计算的值}( <= 150 >=91){= 90*0.6+(90)*1.1计算的值}(>150){= 90*0.6+(150-90)*1.1+(150)*1.7计算的值}{("输入电量:""不合理");}("电费5.2f");}}5. E {( [ ]) {'A''Z';( <)("%2c");();( <)("%2c",(32));}}6. 5 {( []) {0;(1<=1000) {(0);}()("完数:");}}}7E {( []) {111;0;() {1;(1<){*i;};(>9876);;}("满足条件的最大整数:"+(1));}}习题五(第5章)1.用类创建对象时。
以下是《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程序设计教程第二版课后答案【篇一:《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以上版本的操作系统,可以打开[控制面板]窗口,双击其中的[系统]图标,在[系统特性]窗口中单击[高级]选项卡,进而单击[环境变量]按钮。
各章参考答案(1-9章)第一章【习题内容】1. Java语言有哪些特点?2.简述Java的运行机制。
3.简述Java应用程序的开发过程。
4.在计算机上安装、配置Java运行环境,并编辑运行本章中的例程。
【参考答案】1.面向对象、语法简单、平台无关性、安全性、分布式应用、多线程2. Java程序的运行必须经过编写、编译、运行三个步骤。
编写是指在Java开发环境中进行程序代码的输入,最终形成后缀名为.java的Java源文件。
编译是指使用Java编译器对源文件进行错误排查的过程,编译后将生成后缀名为.class的字节码文件,字节码文件是一种和任何具体机器环境及操作系统环境无关的中间代码,它是一种二进制文件,Java解释器负责将字节码文件翻译成具体硬件环境和操作系统平台下的机器代码,以便执行。
运行是指使用Java解释器将字节码文件翻译成机器代码,执行并显示结果。
Java虚拟机(JVM)是运行Java程序的软件环境,Java解释器就是Java虚拟机的一部分。
在运行Java 程序时,首先会启动JVM,然后由它来负责解释执行Java的字节码,JVM把不同软硬件平台的具体差别隐藏起来,从而实现了真正的二进制代码级的跨平台移植。
3.(1)安装JDK(2)配置环境变量(3)使用记事本编写java源文件(4)使用javac 编译java源文件(5)使用java运行java程序。
4.略。
第二章【习题内容】1. 现有语句:String s = "Example"; 则下面哪些语句是合法语句?A. s >>> = 3;B. s[3] = "x";C. int i = s.length();D. String t = "For " + s;E. s = s + 10;2.下面哪些是Java保留字?A. runB. defaultC. implementD. import3.下面声明float变量的语句合法的有:A. float foo = -1;B. float foo = 1.0;C. float foo = 42e1;D. float foo = 2.02f;E. float foo = 3.03d;F. float foo = 0x0123;4.以下哪两个表达式是等价的:A. 3/2B. 3<2C. 3*4D. 3<<2E. 3*2^2F. 3<<<25.分析下列程序的执行结果:(1)public class TestA{public static void main(String args[]){int i = oxFFFFFFF1;int j = ~i;System.out.println("j=" + j);}}(2)public class TestB{public static void main(String[] args){System.out.println(6 ^ 3);}}(3)public class FooBar{public static void main(String[] args){int i = 0, j = 5;tp:for(; ; i++){for( ; ; --j)if(i > j)break tp;}System.out.println("i=" + i + ",j=" + j);}}(4)public class TestC{public static void main(String[] args){int i = 1, j = 10;do{if(i++ > --j)continue;}while(i < 5);System.out.println("i=" + i + " j=" + j);}}【参考答案】1. C、D、E2.B、D3.A、D、F4.C、D5.(1)j=14(2)5(3)i=0,j=-1(4)i=5 j=6第三章【习题内容】1.什么叫引用类型,对象是引用类型吗?2.Java的访问限定修饰符有几种,各自的访问权限是什么?3.什么是类成员,什么是实例成员?它们之间有什么区别?4.如何创建自己的包,如何引入包?5.下面哪一个是类Myclass的构造方法?class Myclass{public void Myclass(){}public static Myclass(){}public Myclass(){}public static void Myclass(){}}6.设计一个动物类,它包含动物的基本属性,例如名称、身长、重量等,并设计相应的动作,例如跑、跳、走等。
第1章Java语言概述选择题1-1 在下列概念中,Java语言只保留了(B)A. 运算符重载B. 方法重载C. 指针D. 结构和联合1-2 下列关于Java语言特性的描述中,错误的是(D)A. 支持多线程操作B. Java程序与平台无关C. Java和程序可以直接访问Internet上的对象D. 支持单继承和多继承1-3 下列关于Java Application程序在结构上的特点的中,错误的是(C)A. Java程序是由一个或多个类组成的B. 组成Java程序的若干个类可以放在一个文件中,也可以放在多个文件中C. Java程序的文件名要与某个类名相同D. 组成Java程序的多个类中,有且仅有一个主类1-4 Java程序经过编译后生成的文件的后缀是(C)A. .objB. .exeC. .classD. .java1-5 下列关于运行字节码文件的命令行参数的描述中,正确的是(A)A. 第一个命令行参数(紧跟命令字的参数)被存放在args[0]中B. 第一个命令行参数被存放在args[1]中C. 命令行的命令字被存放在args[0]中D.数组args[]的大小与命令行参数的个数无关判断题1-1JavaC++的语言之前问世的。
(错)1-2Java语言具有较好的安全性和可移植性及与平台无关等特性。
(对)1-3Java语言中取消了联合的概念,保留了结构概念。
(错)1-4Java语言中数据类型占内在字节数与平台无关。
(对)1-5Java语言中可用下标和指针两种方式表示数组元素。
(错)1-6Java语言的源程序不是编译型的,而是编译解释型的。
(对)1-7操作系统中进程和线程两个概念是没有区别的。
(错)1-8Java语言既是面向对象的又是面向网络的高级语言。
(对)1-9Java程序分为两大类:一类是Application程序,另一类是Applet程序。
前者又称Java应用程序,后者又称为Java小应用程序。
(对)1-10Java Application程序是由多个文件组成的,其中可以有也可以没有主文件。
java程序设计(第二版)课后习题答案//习题2.2 import 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 MyDate class 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(\名字:\工资:\雇佣年份:\ }//end print()public void raiseSalary(double byPercent){ salary*=1+byPercent/100; }//endpublic int hireYear(){ return hireDay.getYear(); }}//end class Employee public class MyTestClass {public static void main(String[] args) { Employee[]staff=new Employee[3];staff[0]=new Employee(\staff[1]=new Employee(\ staff[2]=new Employee(\ester\ int integerValue;System.out.println(\ for(integerValue=0;integerValue for(integerValue=0;integerValue}//end class MyTestClass //习题2.4 import 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=floatValue*floatValue;System.out.println(\System.out.println(\布尔类型变量flag=\ System.out.println(\字符型变量yesChar=\ System.out.println(\字节型变量finByte=\ System.out.println(\整型变量intValue=\ System.out.println(\长整型变量longValue=\ System.out.println(\短整型变量shortValue=\ System.out.println(\浮点型变量floatValue=\System.out.println(\双精度浮点型变量doubleValue=\}//end main() }//习题2.9 import java.util.*; class PubTest1{ private int ivar1; privatefloat fvar1,fvar2; public PubTest1(){ fvar2=0.0f; }public float sum_f_I(){ fvar2=fvar1+ivar1; return fvar2; }public void print(){System.out.println(\ }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.10 import 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(\ System.out.println(this.getName()); System.out.print(\ if(this.getSex()==false){ System.out.println(\女\ } else{System.out.println(\男\ }System.out.print(\System.out.println(this.getBirth().getYear()+\this.getBirth().getMonth()+\ this.getBirth().getDay()); System.out.print(\ System.out.println(this.getSalaryID()); System.out.print(\ System.out.println(this.getPosit()); System.out.print(\ 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);。
Java语言程序设计第2版(郑莉)第二章习题答案1.什么是对象、类,它们之间的联系?答:1)对象是包含现实世界物体特征的抽象实体,它反映系统为之保存信息和与它交互的能力。
对象是一些属性及服务的封装体,在程序设计领域,可以用“对象=数据+作用于这些数据上的操作”来表示。
现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和相关方法的集合。
2)类是既有相同操作功能和相同的数据格式的对象的集合与抽象!3)两者的关系:对象是类的具体实例.。
2.什么是面向对象的程序设计方法?它有那些基本特征?答:面向对象程序设计从所处理的数据入手,以数据为中心而不是以服务为中心来描述系统。
它把编程问题视为一个数据集合,数据相对于功能而言,具有更强的稳定性。
它的特征:抽象,封装,继承,多态。
3(无用)4.请解释类属性、实例属性及其区别。
答:实例属性,由一个个的实例用来存储所有实例都需要的属性信息,不同实例的属性值可能会不同。
5.请解释类方法、实例属性及其区别。
答:实例方法表示特定对象的行为,在声明时前面不加stati c修饰符,在使用时需要发送给一个类实例。
类方法也称为静态方法,在方法声明时前面需加sta t ic修饰符,类方法表示具体实例中类对象的共有行为。
区别:实例方法可以直接访问实例变量,调用实例方法,实例方法可以直接访问类变量,调用类方法;类方法可以直接调用类变量和类方法,类方法不能直接调用实例变量和实例方法;6.类的访问控制符有哪几种?具体含义及其区别。
答:类的访问控制符只有publ ic(公共类)及无修饰符(默认类)两种。
区别:当使用publ ic修饰符时表示所有其他的类都可以使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类可以使用类。
7类成员的访问控制符有哪几种?他们对类成员分别有哪些访问限制的作用?答:类成员的访问控制符有public,private,protect e及无修饰符.public(公有的):用public修饰的成分表示公有的,也就是它可以被其他任何对象访问(前提是对累成员所在的类访问有访问权限).Private(保护的):类中限定为pr ivate的成员只能被这个类本身访问,在类外不可见。
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语言,掌
握编程的基本技巧,并能够应用于实际项目中。