public Rectangle(double length, double width)//构造方法 {
this.length = length; this.width = width; }
public double area() //计算矩形面积,实现Area接口中的抽象方法 {
return this.width * this.length; }
public double perimeter() //计算矩形周长 {
return (this.width+this.length)*2; } }
15、阅读下列程序,请写出该程序的输出结果。 class A { int x, y; A(int a, int b) { x= a; y= b;
} }
public class sample {
public static void main(String args[]) { A pl, p2;
p2 = new A(12, 15); p1 = p2; p2.x++;
System.out.println(\ } }
程序的输出结果:________________________
16、已有日期类MyDate在mypackage包中,Person类使用字符串和MyDate对象作为成员变量,Student类继承Person类。请在空白处填上适当的语句。 public class Person {
private String name; //姓名 private MyDate birthday; //生日 public Person(String name, MyDate birthday) //构造方法 {
this.name = name;
}
public Person( ____________________ ) //拷贝构造方法,重载,复制对象 {
this(p.name, p.birthday); } }
_______________________________ //声明Student类是Person类的子类 {
private String speciality; //专业,子类增加的成员变量
......(略)
public static void main(String args[]) {
Person
p1
=
new
Person(\李小明\
new
MyDate(1979,3,15));
Person p2 = ___________________ ; //拷贝构造方法,p2复制p1
Student s1 = ___________________ ; //默认构造方法创建实例
... …(略) } }
17.方法int maxDigit(int n)的功能是找出十进制整数n各位中最大的一位数字。例如,n=23765,该方法的返回值是7。 static int maxDigit(int n){ int c=0,d; while(n>0){ d=_______; if(d>c) c=d; n/=10; } ________; }
18.方法int sigmaEvenNum(int[]a)的功能是求已知数组中偶数的个数。
static int sigmaEvenNum(int []a){
int s =0;
for ( int i=0;______;i++ ) {
if(______) s++;
} return s; }
19.方法boolean isPrime(int n)的功能是判断正整数n(n>1)是否为质数。
static boolean isPrime(int n) {
int i;
if(n==2)return true; if(n%2==0)return false; for(i=3;i*i<=n;i+=2) if(_________) break; if(_________) return true; return false; }
20.阅读下列程序,请写出该程序的输出结果。 public class Test33{
public static void main(String[]args){
int[][]a={{2,3},{1,6}}; int[][]b={{4,2}, int[][]c=new int[2][2]; int i,j; for(i=0;i
,5}}; {3
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java程序设计第1-4章练习题参考答案(7)在线全文阅读。
相关推荐: