2016年12月计算机二级Java复习题及答案
知识不常常巩固是很容易就忘记了,现在本站小编分享2016年12月计算机二级的文章" target="_blank" >计算机二级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( );
} }
二、程序阅读
1.写出以下程序的运行结果。
class First {
First( ) {
tln ("in First"); } }
public class Second extends First { Second( ) {
tln("in Second"); } public static void main(String[] args) { Second mine= new Second( ); } }
程序运行的'结果:
in First in Second
2.解释程序中语句的含义
纯文本文件中的内容是 abcd
下面的程序将文件中的内容写到文件中和屏幕上 import .*;
public class filecopy {
public static void main(String[] args) { try {
StringBuffer str=new StringBuffer( );
FileInputStream fin=new FileInputStream("");
意义
FileOutputStream fout=new FileOutputStream("");
意义
int c;
while((c=( ))!=-1) {
e(c); 意义
nd((char)c); 意义
}
e( ); e( );
String str2=ring( );
tln(str2); 显示的结果是
}catch(Exception c) {
tln(c); } } }
实例化输入流对象,指定输入流来源文件为 实例化输出流对象,指定输出流目标文件为 将C写入到输出流对象中
将整数C转化为字符,并添加到字符串str的尾部 abcd