《使用Java理解程序逻辑》-B试题答案_doc
时间:2026-01-18
时间:2026-01-18
b卷
二、多选题
三、填空题
1、int x=10,y=10;
System.out.println(x--); System.out.println(--y);
该程序的运行结果是10和9。
2、Java程序中的单行注释符是 // ,多行注释符是/**/ 。
3、Java中用于定义小数的关键字有两个: float 和 double ,后者精度高于前者。
4、 抽象 方法是一种仅有方法头,没有具体方法体和操作实现的方法,该方法必须在抽象类之中定义。 final(最终) 方法是不能被当前类的子类重新定义的方法。 5、假设x=13,y=4,则表达式x%y != 0的值是 true ,其数据类型是 boolean 。
四、编程题
1、按以下要求编写程序
(1) 创建一个Rectangle类,添加width和height两个成员变量 (2) 在Rectangle中添加两种方法分别计算矩形的周长和面积 (3) 编程利用Rectangle输出一个矩形的周长和面积 解答:
public class Rectangle { float width, height;
public Rectangle(float width, float height) { this.width = width; this.height = height; }
public float getLength(){
return (this.width + this.height) * 2; }
public float getArea(){
return this.width * this.height; }
public static void main(String [] args) { Rectangle rect = new Rectangle(10, 20);
System.out.println("周长是:" + rect.getLength()); System.out.println("面积是:" + rect.getArea()); } }
2、 写出一个Point(点)类,该类具有x,y(表示点的横、纵坐标)两个属性,并定义两个个构造方法,一个无参数,将x,y均设置为0,另一对坐标值为参数,设置x,y为给定坐标值。该类的show方法输出该点的坐标值。(15分) class Point { int x,y;
Point(){x =0;y=0;} //得5’
Point(int x,int y){this.x=x;this.y=y;} //得5’
public void Show(){System.out.print("坐标为:("+x+","+y+”)”);} //}
得5’