計算機二級Java備考試題附答案

來源:文萃谷 2.86W

計算機二級考試是全國計算機等級考試四個等級中的一個等級,考核計算機基礎知識和使用一種高級計算機語言編寫程序以及上機調試的基本技能。下文是為大家精選的計算機二級Java備考試題附答案,歡迎大家閲讀參考。

計算機二級Java備考試題附答案

 編程題

1、 編寫一個Java Application 程序,main程序輸入10個整數給數組,通過函數getMinAndMax(int a[])得到這10個整數的最大值和最小值並輸出結果。

class App {

static void getMinAndMax(int a[]) {

int min,max;

min = max = a[0];

for(int i=1;i if(a[i]>max)

max=a[i];

if(a[i] min=a[i]; }

tln(“Array’Max Value:”+max);

tln(“Array’Min Value:”+min);

}

public static void main(String[] args) {

int arr[] = {4,6,72,9,14,3,8,23,56,32};

getMinAndMax(arr); } }

2、編寫一個完整的Java Application 程序。包含接口ShapeArea, Rectangle

類,Triangle類及Test類,具體要求如下:

⑴接口ShapeArea:

double getArea( ):

求一個形狀的面積

double getPerimeter ( ):

求一個形狀的'周長

⑵類 Rectangle:實現ShapeArea接口,並有以下屬性和方法:

① 屬性

width: double類型,表示矩形的長 height: double類型,表示矩形的高

② 方法

Rectangle(double w, double h):構造函數

toString( )

方法 :輸出矩形的描述信息,如“width=1.0,height=2.0, perimeter=6.0, area=2.0”

⑶類Triangle:實現ShapeArea接口,並有以下屬性和方法:

① 屬性

x,y,z: double型,表示三角形的三條邊

s: 周長的1/2(注:求三角形面積公式為))( )((zsysxss,s=(x+y+z)/2 ,開方可用(double)方法)

② 方法

Triangle(double x, double y, double z):

構造函數,給三條邊和s賦初值。

toString( ):

輸出矩形的描述信息,如“three sides:3.0,4.0,5.0,perimeter=12.0,area=6.0”

⑷Test類作為主類要完成測試功能

① 生成Rectangle對象

調用對象的toString方法,輸出對象的描述信息

interface ShapeArea { double getArea( );

double getPerimeter( );

}

class Rectangle implements ShapeArea { double width,height;

Rectangle(double w,double h) {ko width =w;

height=h;

}

public void toString( )

{

tln("width="+width+",height="+height+", perimeter="+ getPerimeter( )+", area="+ getArea( ));

}

public double getArea( )

{ return width*height;

}

public double getPerimeter( )

{ return 2*(width+height);

} }

class Triangle implements ShapeArea { double x,y,z,s; Triangle(double x, double y, double z) { this.x =x; this.y=y;

this.z=z; s = (x+y+z)/2; }

public void toString( )

{

tln("Three Sides:"+x+","+y+","+z+",Perimeter="+ getPerimeter( )+", area="+ getArea( ));

}

public double getArea( )

{

return (s*(s-x)*(s-y)*(s-z));

}

public double getPerimeter( )

{ return x+y+z;

} }

class test { public static void main(String[] args) { Rectangle rct = new Rectangle(4,5);

_String( );

} }

熱門標籤