当前位置:首页 > 2013-2014学年第一学期JAVA程序设计期中考试卷(试做)
}
void y1(){System.out.println(\我是父类y1\ (2)
class B extends A{ }
答案:a可以使用的方法是(1)和(3) 9.补充程序,运行后出现如下结果。 public interface Animal{ }
class Dog implements Animal{ }
class Cat implements Animal{ }
class UseAnimal{ }
public class TestPho {
public static void main(String[] args) {
Cat c=new Cat();
UseAnimal u=new UseAnimal(); u.useCry(c);
Dog d=new Dog(); u.useCry(d);
void useCry( Animal a ){ }
a.cry(); public void cry(){ }
System.out.println(\猫在叫....\public void cry(){ }
System.out.println(\狗在叫....\void cry();
void y1(){System.out.println(\我是子类y1\ (3) void y2(){System.out.println(\我是子类!\ (4) public static void main(String[] args) { }
A a=new B();
}
}
程序运行结果: 猫在叫.... 狗在叫....
10.补足代码,完成读取文件并显示在屏幕上的功能。
FileInputStream fis = new FileInputStream(\
byte[] buf = new byte[1024]; int len = 0;
while( (len=fis.read(buf))!=-1 )
{ }
System.out.println( new String (buf,0,len) ); fis.close();
四.编程题(共30分)
1.创建一个点类(Coordinate),该类具有横、纵坐标;具有构造函数完成类的初始化。创建一个圆类继承点类,用继承的点来表示圆心的坐标,该圆类有半径属性,有构造方法,构造方法初始化圆心和半径;该圆类有求周长、面积和显示图形信息的方法。 public class Coordinate { }
public class Circle extends Coordinate { double radius;
public Circle(int x,int y,double radius) {
super(x, y); this.radius=radius; }
public double getPerimeter() {
return 2*Math.PI*this.radius; }
public double getArea(){
return Math.PI*Math.pow(this.radius,2);
int x,y;
public Coordinate(int x, int y) { }
this.x = x; this.y = y;
}
public void show() {
System.out.println(\圆心的坐标是\ System.out.println(\圆的周长是\ System.out.println(\圆的面积是\ } }
2.写一个函数,使用冒泡排序对int 型数字元素排序。 //冒泡排序的实现
public static void bubbleSort(int[] arr)
{ }
for(int x=0; x for(int y=0; y if(arr[y] /* int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; */ swap(arr,y,y+1); //交换数组中两个元素的位置。 public static void swap(int[] arr,int a,int b) { } int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; 3.写一个函数,实现对字符串中部分元素反转。(不能使用StringBuffer字符串) public static String reverseString(String s,int start,int end) { } //字符串变数组。 char[] chs = s.toCharArray(); //反转数组。 reverse(chs,start,end); //将数组变成字符串。 return new String(chs); public static String reverseString(String s) { } private static void reverse(char[] arr,int x,int y) { } private static void swap(char[] arr,int x,int y) { } char temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; for(int start=x,end=y-1; start swap(arr,start,end); return reverseString(s,0,s.length()); 4.编程实现从键盘读入的字符串写入文件。 public class TestIOFour { public static void main(String[] args) throws IOException{ byte buf[]=new byte[1024]; System.in.read(buf); FileOutputStream fos=new FileOutputStream(\ fos.write(buf); fos.close(); }
共分享92篇相关文档