自考Java语言程序设计(一)第五章以后课后习题及答案
- 格式:docx
- 大小:36.53 KB
- 文档页数:24
Java语言程序设计 (郑莉 )第二章习题答案1.什么是对象、类,它们之间的联系答:1)对象是包括现实世界物体特点的抽象实体,它反响系统为之保存信息和与它交互的能力。
对象是一些属性及服务的封装体,在程序设计领域,能够用“对象=数据+作用于这些数据上的操作”来表示。
现实生活中对象是指客观世界的实体;在程序中对象是指一组变量和有关方法的会集。
2)类是既有相同操作功能和相同的数据格式的对象的会集与抽象!3)两者的关系:对象是类的详尽实例. 。
2.什么是面向对象的程序设计方法它有那些基本特点答:面向对象程序设计从所办理的数据下手,以数据为中心而不是以服务为中心来描述系统。
它把编程问题视为一个数据会集,数据有对于功能而言,拥有更强的牢固性。
它的特点:抽象,封装,继承,多态。
3(无用)4.请讲解类属性、实例属性及其差异。
答:实例属性,由一个个的实例用来储藏全部实例都需要的属性信息,不相同实例的属性值可能会不相同。
5.请讲解类方法、实例属性及其差异。
答:实例方法表示特定对象的行为,在声明时前面不加static修饰符,在使用时需要发送给一个类实例。
类方法也称为静态方法,在方法声明时前面需加static修饰符,类方法表示详尽实例中类对象的共有行为。
差异:实例方法能够直接接见实例变量,调用实例方法,实例方法能够直接接见类变量,调用类方法;类方法能够直接调用类变量和类方法,类方法不能够直接调用实例变量和实例方法;6.类的接见控制符有哪几种详尽含义及其差异。
答:类的接见控制符只有public (公共类)及无修饰符(默认类)两种。
差异:当使用 public修饰符时表示全部其他的类都能够使用此类;当没有修饰符时,则只有与此类处于同一包中的其他类能够使用类。
7类成员的接见控制符有哪几种他们对类成员分别有哪些接见限制的作用答 : 类成员的接见控制符有public,private,protecte及无修饰符.public( 公有的 ): 用 public修饰的成分表示公有的, 也就是它可以被其他任何对象接见( 前提是对累成员所在的类接见有接见权限).Private(保护的 ) :类中限制为private的成员只能被这个类本身接见,在类外不能见。
Chapter5Methods1.At least three benefits:(1)Reuse code;(2)Reduce complexity;(3)Easy to maintain.See the Sections5.2and5.3on how to declare and invoke methods.What is thesubtle difference between“defining a method”and“declaring a variable”?A declaration usually involvesallocating memory to store a variable,but a definitiondoesn’t.2.void3.Yes.return(num1>num2)?num1:num2;4.True:a call to a method with a void return type is always a statement itself.False:a call to a value-returning method is always a component of an expression.5.A syntax error occurs if a return statement is not used to return a value in a value-returning method.You can have a return statement in a void method,whichsimply exits the method.But a return statement cannot return a value such asreturn x+y in a void method.6.See Section5.2.puting a sales commission given the sales amount and the commission ratepublic static double getCommission(double salesAmount,doublecommissionRate)Printing a calendar for a monthpublic static void printCalendar(int month,int year)Computing a square rootpublic static double sqrt(double value)Testing whether a number is even and return true if it ispublic static boolean isEven(int value)Printing a message for a specified number of timespublic static void printMessage(String message,int times)Computing the monthly payment,given the loan amount,number of years,and annual interest rate.public static double monthlyPayment(double loan,intnumberOfYears,double annualInterestRate)Finding the corresponding uppercase letter given a lowercase letter.public static char getUpperCase(char letter)8.Line2:method1is not defined correctly.It does not have a return type or void.Line2:type int should be declared for parameter m.Line7:parameter type for n should be double to match method2(3.4).Line11:if(n<0)should be removed in method,otherwise a compile error is reported.9.public class Test{public static double xMethod(double i,double j){ while(i<j){j--;}return j;}}10.You pass actual parameters by passing the right type of value in the right order.The actual parameter can have the same name as its formal parameter.11."Pass by value"is to pass a copy of the value to the method.(A)The output of the program is0,because the variable max is not changed byinvoking the method max.(B)224248248162481632248163264(C)Before the call,variable times is3n=3Welcome to Java!n=2Welcome to Java!n=1Welcome to Java!After the call,variable times is3(D)12121421i is 512.Just before max is invoked.Space required for the main methodmax: 0Just entering max.Space required for the max methodmax: 0value2: 2 value1: 1Just before max is returnedSpace required for the main methodmax: 0Space required for the max methodmax: 2value2: 2 value1: 1 Space required for the main methodmax: 0Space required for the main methodmax: 0Right after max is returned13.Two methods with the same name,defined in the same class,is called method overloading.It is fine to have same method name,but different parameter types.You cannot overload methods based on return type,or modifiers.14.Methods public static void method(int x)and public static int method(int y)have the same signature method(int).15.Line 7:int n =1is wrong since n is already declared in the method signature.16.True17.(a)34+(int)(Math.random()*(55–34))(b)(int)(Math.random()*1000)(c)5.5+(Math.random()*(55.5–5.5))(d)(char)(‘a’+(Math.random()*(‘z’–‘a’+1))18.Math.sqrt(4)= 2.0Math.sin(2*Math.PI)=0Math.cos(2*Math.PI)=1Math.pow(2,2)= 4.0Math.log(Math.E)=1Math.exp(1)= 2.718Math.max(2,Math.min(3,4))=3 Math.rint(-2.5)=-2.0Math.ceil(-2.5)=-2.0Math.floor(-2.5)=-3.0Math.round(-2.5f)=-2Math.round(-2.5)=-2Math.rint(2.5)= 2.0Math.ceil(2.5)= 3.0Math.floor(2.5)= 2.0Math.round(2.5f)=3Math.round(-2.5)=-2Math.round(Math.abs(-2.5))=3。
Java程序设计各章习题及其答案第一章习题及思考题1、Java程序是由什么组成的?一个程序中必须有public类吗?Java源文件的命名规则是怎样的?答:一个Java源程序是由若干个类组成。
一个Java程序不一定需要有public类:如果源文件中有多个类时,则只能有一个类是public 类;如果源文件中只有一个类,则不将该类写成public也将默认它为主类。
源文件命名时要求源文件主名应与主类(即用public修饰的类)的类名相同,扩展名为.java。
如果没有定义public类,则可以任何一个类名为主文件名,当然这是不主张的,因为它将无法进行被继承使用。
另外,对Applet小应用程序来说,其主类必须为public,否则虽然在一些编译编译平台下可以通过(在BlueJ下无法通过)但运行时无法显示结果。
2、怎样区分应用程序和小应用程序?应用程序的主类和小应用程序的主类必须用public修饰吗?答:Java Application是完整的程序,需要独立的解释器来解释运行;而Java Applet则是嵌在HTML编写的Web页面中的非独立运行程序,由Web浏览器内部包含的Java解释器来解释运行。
在源程序代码中两者的主要区别是:任何一个Java Application 应用程序必须有且只有一个main方法,它是整个程序的入口方法;任何一个Applet小应用程序要求程序中有且必须有一个类是系统类Applet的子类,即该类头部分以extends Applet结尾。
应用程序的主类当源文件中只有一个类时不必用public修饰,但当有多于一个类时则主类必须用public修饰。
小应用程序的主类在任何时候都需要用public来修饰。
3、开发与运行Java程序需要经过哪些主要步骤和过程?答:主要有三个步骤(1)、用文字编辑器notepad(或在Jcreator,Gel, BuleJ,Eclipse, Jbuilder等)编写源文件;(2)、使用Java编译器(如Javac.exe)将.java源文件编译成字节码文件.class;(3)、运行Java程序:对应用程序应通过Java解释器(如java.exe)来运行,而对小应用程序应通过支持Java标准的浏览器(如Microsoft Explorer)来解释运行。
习题一、选择题1. 面向对象程序设计的基本特征是(BCD)。
(多选)A.抽象B.封装C.继承D.多态2.下面关于类的说法正确的是(ACD)。
(多选)A.类是Java 语言中的一种复合数据类型。
B.类中包含数据变量和方法。
C.类是对所有具有一定共性的对象的抽象。
D.Java 语言的类只支持单继承。
上机指导1.设计银行项目中的注册银行用户基本信息的类,包括账户卡号、姓名、身份证号、联系电话、家庭住址。
要求是一个标准Java类(数据私有,提供seter/getter),然后提供一个toString方法打印该银行用户的信息。
答:源代码请参见“CH05_LAB\src\com\inspur\ch05\BankUser.java”2.设计银行项目中的帐户信息,包括帐户卡号、密码、存款,要求如“练习题1”。
答:源代码请参见“CH05_LAB\src\com\inspur\ch05\Account.java”3.设计银行项目中的管理员类,包括用户名和密码。
要求如“练习题1”。
答:源代码请参见“CH05_LAB\src\com\inspur\ch05\Manager.java”4.创建一个Rectangle类。
添加两个属性width、height,分别表示宽度和高度,添加计算矩形的周长和面积的方法。
测试输出一个矩形的周长和面积。
答:源代码请参见“CH05_LAB\src\com\inspur\ch05\Rectangle.java”5.猜数字游戏:一个类A有一个成员变量v,有一个初值100。
定义一个类,对A类的成员变量v进行猜。
如果大了则提示大了,小了则提示小了。
等于则提示猜测成功。
答:源代码请参见“CH05_LAB\src\com\inspur\ch05\Guess.java”6.编写一个Java程序,模拟一个简单的计算器。
定义名为Computer的类,其中两个整型数据成员num1和num1,编写构造方法,赋予num1和num2初始值,再为该类定义加、减、乘、除等公有方法,分别对两个成员变量执行加减乘除的运算。
Chapter5Methods1.At least three benefits:(1)Reuse code;(2)Reduce complexity;(3)Easy to maintain.See the Sections5.2and5.3on how to declare and invoke methods.What is thesubtle difference between“defining a method”and“declaring a variable”?A declaration usually involvesallocating memory to store a variable,but a definitiondoesn’t.2.void3.Yes.return(num1>num2)?num1:num2;4.True:a call to a method with a void return type is always a statement itself.False:a call to a value-returning method is always a component of an expression.5.A syntax error occurs if a return statement is not used to return a value in a value-returning method.You can have a return statement in a void method,whichsimply exits the method.But a return statement cannot return a value such asreturn x+y in a void method.6.See Section5.2.puting a sales commission given the sales amount and the commission ratepublic static double getCommission(double salesAmount,doublecommissionRate)Printing a calendar for a monthpublic static void printCalendar(int month,int year)Computing a square rootpublic static double sqrt(double value)Testing whether a number is even and return true if it ispublic static boolean isEven(int value)Printing a message for a specified number of timespublic static void printMessage(String message,int times)Computing the monthly payment,given the loan amount,number of years,and annual interest rate.public static double monthlyPayment(double loan,intnumberOfYears,double annualInterestRate)Finding the corresponding uppercase letter given a lowercase letter.public static char getUpperCase(char letter)8.Line2:method1is not defined correctly.It does not have a return type or void.Line2:type int should be declared for parameter m.Line7:parameter type for n should be double to match method2(3.4).Line11:if(n<0)should be removed in method,otherwise a compile error is reported.9.public class Test{public static double xMethod(double i,double j){ while(i<j){j--;}return j;}}10.You pass actual parameters by passing the right type of value in the right order.The actual parameter can have the same name as its formal parameter.11."Pass by value"is to pass a copy of the value to the method.(A)The output of the program is0,because the variable max is not changed byinvoking the method max.(B)224248248162481632248163264(C)Before the call,variable times is3n=3Welcome to Java!n=2Welcome to Java!n=1Welcome to Java!After the call,variable times is3(D)12121421i is 512.Just before max is invoked.Space required for the main methodmax: 0Just entering max.Space required for the max methodmax: 0value2: 2 value1: 1Just before max is returnedSpace required for the main methodmax: 0Space required for the max methodmax: 2value2: 2 value1: 1 Space required for the main methodmax: 0Space required for the main methodmax: 0Right after max is returned13.Two methods with the same name,defined in the same class,is called method overloading.It is fine to have same method name,but different parameter types.You cannot overload methods based on return type,or modifiers.14.Methods public static void method(int x)and public static int method(int y)have the same signature method(int).15.Line 7:int n =1is wrong since n is already declared in the method signature.16.True17.(a)34+(int)(Math.random()*(55–34))(b)(int)(Math.random()*1000)(c)5.5+(Math.random()*(55.5–5.5))(d)(char)(‘a’+(Math.random()*(‘z’–‘a’+1))18.Math.sqrt(4)= 2.0Math.sin(2*Math.PI)=0Math.cos(2*Math.PI)=1Math.pow(2,2)= 4.0Math.log(Math.E)=1Math.exp(1)= 2.718Math.max(2,Math.min(3,4))=3 Math.rint(-2.5)=-2.0Math.ceil(-2.5)=-2.0Math.floor(-2.5)=-3.0Math.round(-2.5f)=-2Math.round(-2.5)=-2Math.rint(2.5)= 2.0Math.ceil(2.5)= 3.0Math.floor(2.5)= 2.0Math.round(2.5f)=3Math.round(-2.5)=-2Math.round(Math.abs(-2.5))=3。
一.程序设计题1.定义一个汽车类Car,要求如下:1)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型),并且所有属性为私有。
2)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
3)为私有属性提供访问器方法。
注意:汽车品牌一旦初始化之后不能修改。
定义测试类CarTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
public class Car{private String brand;private String color;private double speed;public Car(String brand,String color){this.brand=brand;this.color=color;}public String getColor(){return color;}public void setColor(String color){this.color=color;}public double getSpeed(){return speed;}public void setSpeed(double speed){this.speed=speed;}public String getBrand(){return brand;}}public class CarTest{public static void main(String[]args){Car c1=new Car("benz","black");}}2.定义一个图书类Book,要求如下:1)属性包括:书名name(String类型)、作者author(String类型),单价price(double类型),数量amount(int类型),并且所有属性为私有。
第一章课后习题1.编译Java程序的命令是什么?2.执行Java程序的命令是什么?3.Java应用程序和小程序的区别是什么?4.编写一个application ,实现在屏幕上打印自己名字的功能。
第一章课后习题答案1.编译Java程序的命令是什么?答案:javac 源文件名2.执行Java程序的命令是什么?java 主类名3.Java应用程序和小程序的区别是什么?Java application⏹由Java解释器独立运行字节码⏹由专门的命令行启动程序执行⏹程序中有定义了main()方法的主类Java applet⏹不能独立运行,字节码必须嵌入HTML文档⏹当浏览器调用含applet的Web页面时执行⏹程序中含有java. applet. Applet 类的子类4.编写一个application ,实现在屏幕上打印自己名字的功能。
class Test{public static void main(String[] args){System.out.println(“张三”);}}第二章课后习题(1)一、选择题1.下列变量定义错误的是。
A) int a; B) double b=4.5; C) boolean b=true; D)float f=9.8;2.下列数据类型的精度由高到低的顺序是:a)float,double,int,longb)double,float,int,bytec)byte,long,double,floatd)double,int,float,long3.执行完下列代码后,int a=3;char b='5';char c=(char)(a+b);c的值是?A)’8’ b)53 c)8 d)564.Unicode是一种_____________A) 数据类型 B)java包 C)字符编码 D)java类5.6+5%3+2的值是___________A)2 B)1 C) 9 D)106.下面的逻辑表达式中合法的是__________A)(7+8)&&(9-5) B)(9*5)||(9*7) C)9>6&&8<10 D)(9%4)&&(8*3) 7.java语言中,占用32位存储空间的是__________。
第5章习题解答1.使用抽象和封装有哪些好处?答:抽象是人们解决问题的基本手段,程序设计过程中需要对问题领域进行分析、设计中得出的抽象概念,然后封装成一些类。
封装也称为信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节,只保留一些对外接口使之与外部发生联系。
系统的其他部分只有通过包裹在数据外面的被授权的操作来与这个抽象数据类型交流与交互。
也就是说,用户无需知道对象内部方法的实现细节,但可以根据对象提供的外部接口(对象名和参数)访问该对象。
把对象中相同或相似地地方抽象出来,从特殊到一半,从具体到抽象的过程,对象经过抽象得到类,类的实例化成了对象。
也可以高度抽象成接口,让不完全相同,但包含相同点的对象实现此接口,也就是利用多态实现。
把相同点抽象出来,抽象成此类或接口的方法、属性、字段等,封装就是隐藏某个对象的与其基本特性没有很大关系的所有详细信息的过程,就是将需要让其他类知道的暴露出来,不需要让其他类了解的全部隐藏起来,封装可以阻止对不需要信息的访问,我们可以使用访问指定符实现封装,也可以使用方法实现封装,可以将隐藏的信息作为参数或者属性值、字段指传给公共的接口或方法,以实现隐藏起来的信息和公开信息的交互。
封装的目的就是为了实现“高内聚,低耦合”。
高内聚就是类的内部数据操作细节自己完成,不允许外部干涉,就是这个类只完成自己的功能,不需要外部参与;低耦合,就是仅暴露很少的方法给外部使用。
2.构造方法的作用是什么?它与一般的成员方法在使用和定义方面有什么区别?答:构造方法用于生成一个对象实例,并对对象实例中的成员变量初始化。
当用new创建一个类的新的对象时,构造方法立即执行。
构造方法名字必须与类名相同。
3.Overload和Override的区别?答:方法重载(overloading)与方法覆盖(overriding)是实现多态性的基本手段,但两者的机制不同。
第5章习题解答1.流的主要特征有哪些,用流来实现JAVA中的输入输出有什么优点?答: 一是单向性,即数据只能从数据源流向数据宿;二是顺序性,先从数据源流出的数据一定比后流出的数据先到达数据宿;三是数据流必须而且只能和一个数据源与一个数据宿相连。
优点是体现了面向对象程序设计的概念,通过流可以把对不同类型的输入/输出设备的操作统一为用流来实现。
2.对字节流和字符流进行读写操作的一般步骤是什么?答:声明流对象,创建流对象,通过流对象进行读(写)操作,关闭流对象。
3. 有哪些常用的字节流和字符流,他们的主要区别是什么?答: InputStream/OutputStrem:普通字节流,所有字节流的基类。
FileInputStream/ FileOutputStream:用于从文件中读写数据。
BufferedInputStream/ BufferedOutputStream:用于从缓冲区输入流中读写数据。
采用缓冲区流可减少实际上从外部输入设备上读写数据的次数,从而提高效率。
DataInputStream/ DataOutputStream:按读写数据对象的大小从字节流中读写数据,而不是象其它字节流那样以字节为基本单位。
PipedInputStream/ PipedOutputStream:管道输流用于从另一个线程中读写数据。
4.么是异常?说明Java中的异常处理机制?试述JAVA中异常的抛出和传递过程?答: 异常是程序设计语言提供的一种机制,它用于在程序运行中的非常规情况下,控制程序对非常规情况进合理的处理。
Java提供了try-catch-finally语句来对异常进行处理。
先按照正常顺序执行try子句中的语句,若在执行过程中出现异常,则try子句中还未被执行的语句将再也不会被执行。
而程序控制立即转移到catch子句,将发生的异常与catch子句中的异常进行匹配,若找到一个匹配,就执行该catch子句中的语句。