JAVA试题库(7)

2019-03-29 17:25

double getBottomRadius() { return bottom.getRadius(); }

public void setBottomRadius(double r){ bottom.setRadius(r); } }

public class Example5_14 {

public static void main(String args[]) {

Circle circle=new Circle(5);

System.out.println(\方法中circle的半径\ Circular circular=new Circular(circle,10);

System.out.println(\圆锥的bottom的半径:\ System.out.println(\圆锥的体积:\olme()); double r=20;

circular.setBottomRadius(r);

System.out.println(\圆锥的bottom的半径:\ System.out.println(\方法中circle的半径:\ } } 答:

main方法中circle的半径5 圆锥的bottom的半径:5 圆锥的体积:261

圆锥的bottom的半径:20 main方法中circle的半径:20

6. 请给出下面程序的输出结果

public class Example9_4 {

public static void main(String args[]) {

String path=\ System.out.println(path); int index=path.indexOf(\ index=path.indexOf(\ System.out.println(index); String sub=path.substring(index); System.out.println(sub); index=path.lastIndexOf(\ sub=path.substring(index+1); System.out.println(sub);

System.out.println(path.regionMatches(4,\

} } 答:

c:\\book\\javabook\\xml.doc 2 \\book\\javabook\\xml.doc xml.doc true

四、 设计题

1. 实现一个People类,异常类IntegerException,以及EX41类。People类包含一个age的

属性以及读写该属性的两个方法,当读入的年龄不在[0,160]时抛出异常。在EX41类中实现main方法,使用两个年龄(200和20)进行验证。

答:

class IntegerException extends Exception{ String message; public IntegerException(int m){ message=\ } public String toString(){ return message; } }

class People{ private int age=1; public void setAge(int age) throws IntegerException { if (age>=160||age<=0){ throw new IntegerException(age); } else { this.age=age; } } public int getAge(){ System.out.println(\ return age; } }

public class Example5_14 {

public static void main(String args[]) {

People wang=new People(), zhang=new People();

try{ wang.setAge(180);

System.out.println(wang.getAge()); }

catch(IntegerException e) { System.out.println(e.toString()); }

try { zhang.setAge(37);

System.out.println(zhang.getAge()); }

catch(IntegerException e) {

System.out.println(e.toString()); } } }

2. 编写程序实现字节文件的复制

import java.io.File;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

public class FileCopy {

public static void main(String args[]) { int b;

byte tom[]=new byte[18]; try{ File f1=new File(\ File f2=new File(\

FileInputStream in=new FileInputStream(f1);

FileOutputStream out=new FileOutputStream(f2);

while((b=in.read(tom,0,18))!=-1) { out.write(tom,0,b); }

in.close(); out.close(); }

catch(IOException e) {

System.out.println(\ } } }

3. 将一个字符创数组按字典序重新排序

import java.util.Arrays; class SortString { public static void sort(String a[]) { for(int i=0;i

public class Example9_3 {

public static void main(String args[]) {

String [] a={\ String [] b=Arrays.copyOf(a,a.length);

System.out.println(\使用用户编写的SortString类,按字典序排列数组a:\ SortString.sort(a); for(String s:a) {

System.out.print(\ \ }

System.out.println();

System.out.println(\使用类库中的Arrays类,按字典序排列数组b:\ Arrays.sort(b); for(String s:b) {

System.out.print(\ \ } } }

4. 编写程序实现字符文件的复制

import java.io.*;

public class FileCopy {

public static void main(String args[]) { int b;

char tom[]=new char[18]; try{ File f1=new File(\ File f2=new File(\

FileReader in=new FileReader(f1); FileWriter out=new FileWriter(f2);

while((b=in.read(tom,0,18))!=-1) { out.write(tom,0,b); }

in.close(); out.close(); }

catch(IOException e) {

System.out.println(\ } } }

5. 用户从键盘输入一行文本,程序输出其中的单词。用户从键盘输入“How are (hello)”的

运行效果如图所示: 一行文本:

How are you(hello) 单词1:How 单词2:are 单词3:you 单词4:hello

答:

import java.util.Scanner; public class Example9_10 {

public static void main (String args[ ]) { System.out.println(\一行文本:\

Scanner reader=new Scanner(System.in); String str= reader.nextLine();

//空格字符、数字和符号(!\组成的正则表达式: String regex=\ String words[]=str.split(regex);

for(int i=0;i

System.out.println(\单词\ } } }


JAVA试题库(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:西工大航空学院2015-2016秋至冬课表

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: