学年第 学期 考试科目: 面向对象的程序设计 考试类型:(闭卷) 考试时间: 120 分钟
学号 姓名 年级专业 题号 得分 评阅人
一 二 三 四 总分 一、 选择题(单选,每题2分,共30分)
1 B 11 C 2 A 12 D 3 C 13 A 4 D 14 C 5 C 15 D 6 D 7 B 8 A 9 B 10 D 二、 判断题(正确的画勾,错误的画叉)
1 2 3 4 5 6 7 8 9 10 × × × × √ × √ × × √ 三、简答题
1、什么是标识符?什么是保留字?java中标识符的命名规则是?(6分)
答:编写程序的时候使用的各种单词称为标识符。在编程语言中有特殊意义的标识符称为保留字。Java语言中程序自定义的标识符何以是任何字母、数字、下划线(_)和美元符号($)的组合,但不能以数字开头。
2、按要求完成下面问题。(8分)
(1) 编写一条赋值语句,计算num1、num2之和的平方根,并将结果保存在num3(2) 方法hypotenuse 带两个参数 ,第一个参数是整型数据side1,第二个是浮中。(2分)
点型参数side2. 并且返回一个双精度浮点型结果。(3分)
(3) Object类的意义是什么? (3分)
1
(1) num3=Math.sqrt(num1+num2);
(2) double thpotenuse(int side1, float side2);
(3) java中所有类都是直接或者间接地有object类派生,因此object类的所有public方法可供每个对象使用。
3、编写一个方法
isIsosceles,以表示三角形的三边长的三个整数为参数,如果该三角形是等腰
三角形则返回true,其他情况返回false。(6分) public boolean isIsoceles (int side1, int side2, int side3) {
boolean result = false;
if ( (side1 == side2) && side1 != side3) || (side2 == side3) && side2 != side1) || (side1 == side3) && side1 != side2) ) result = true; return result; }
4、声明一个枚举型Season变量,可能的取值是winter、spring、summer和fall (4分)
enum Season{ winter, spring, summer, fall}
5、什么是多态?如何实现多态?(6分)
多态性是指一个引用变量在不同时刻可以指向不同类型的对象。通过这种方式调用的方法在不同时刻将与不同的方法实现代码绑定。被绑定的方法代码取决于被引用的对象的类型。
实现多态性的方法:利用继承实现和利用接口实现。
2
四、 程序题(共30分)
1、编写一个distance方法,其参数为多个整型数(每个参数代表一段旅程距离),并返回总旅程距离 (6分)
public int distance( ) {
int sum = 0; for(int leg : legs) {
rum +=leg; }
return sum; }
2、写一个程序画如图所示的同心圆,每个圆之间间隔为10 pixels。(10分)
3
3、编写一个应用程序,提示用户输入三角形的三条边长。使用Heron公式计算三角形的面积并保留2位小数。公式中s代表三角形半周长,a、b、c分别代表三条边。 面积=s(s?a)(s?b)(s?c) (14分)
import java.util.Scanner; import java.text.DecimalFormat;
public class TriangleArea
{ // Computes the area of a triangle given the length of each // side. Uses Heron's formula.
4
public static void main (String[] args) {
double side1, side2, side3, s, area;
System.out.print (\ side1 = scan.nextDouble();
System.out.print (\ side2 = scan.nextDouble();
System.out.print (\ side3 = scan.nextDouble();
s = (side1 + side2 + side3) / 2; // half of the perimeter area = Math.sqrt (s * (s-side1) * (s-side2) * (s-side3));
DecimalFormat fmt = new DecimalFormat (\ System.out.println (\ }
Scanner scan = new Scanner(System.in);
5