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(\单词\ } } }