java练习题答案 下载本文

内容发布更新时间 : 2024/5/4 2:19:22星期一 下面是文章的全部内容请认真阅读。

System.out.print(b1+\

A)true false B) false true C) true true D)false false

5、应用程序的main方法中有以下语句,则输出的结果是 ( A )。 String s1=\double x=Double.parseDouble(s1); int y=Integer.parseInt(s2); System.out.println(x+y);

A) 12.5 B) 120.5 C) 12 D) “12.5” 三、程序设计题:

按以下要求编写程序 (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(\周长是:\ System.out.println(\面积是:\ } }