1.列出5个常见的异常。
2.编写一个程序,以说明catch(Exception e)如何捕获各种异常。
3.根据所给创建自定义异常类的使用格式,编写一个自定义异常的小程序。
4 String和StringBuffer各自有什么特点?
5. String cha="JavaApplication",下面结果是什么?
cha.length()
cha.concat("Applet")
cha.substring(3,8)
cha.replace('a','A')
6. 设定一个含有大小写字母的字符串,先将所有大写字母输出,再将所有小写字母输出。
7. 编写程序,接收用户从键键盘上输入的三个整数x,y,z..从中选出最大和最小者,并编程实现
3.根据所给创建自定义异常类的使用格式,编写一个自定义异常的小程序。
package model;
public class ExceptionClass {
ExceptionClass(int val) {
size = val;
try {
checkSize();
} catch (ArraySizeException e) {
System.out.println(e);
}
}
private int size;
private int[] array;
/**
* 检查数组长度的方法. @ throws 一个 ArraySizeException
*/
public void checkSize() throws ArraySizeException {
if (size < 0) {
throw new ArraySizeException();
}
array = new int[3];
for (int count = 0; count < 3; count++) {
array[count] = count + 1;
}
}
public static void main(String[] args) {
ExceptionClass ec=new ExceptionClass(-2);
}
}
package model;
class ArraySizeException extends NegativeArraySizeException {
/**
*
*/
private static final long serialVersionUID = 1L;
ArraySizeException() {
super("您传递的数组大小非法");
}
}
6. 设定一个含有大小写字母的字符串,先将所有大写字母输出,再将所有小写字母输出。
/**
*
*/
package model;
/**
* @author Administrator
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
//设定一个含有大小写字母的字符串,先将所有大写字母输出,再将所有小写字母输出。
String s="aHbIcJdKeLft";
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)>='A' && s.charAt(i)<='Z')
{
System.out.print(s.charAt(i));
}
}
System.out.print(" ");
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)>='a' && s.charAt(i)<='z')
{
System.out.print(s.charAt(i));
}
}
}
}
7. 编写程序,接收用户从键键盘上输入的三个整数x,y,z..从中选出最大和最小者,并编程实现
/**
*
*/
package model;
import java.io.*;
/**
* @author Administrator
*
*/
public class homeWork3 {
/**
* @param args
*/
public static void main(String[] args) {
String s1="";
String s2="";
String s3="";
int i1,i2,i3;
System.out.print("请输入三个整数:");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s1=in.readLine();
s2=in.readLine();
s3=in.readLine();
}catch(IOException e){}
System.out.print("");
i1=Integer.parseInt(s1);
i2=Integer.parseInt(s2);
i3=Integer.parseInt(s3);
homeWork3 hw=new homeWork3();
hw.Max(i1, i2, i3);
}
void Max(int x,int y,int z)
{
if(x>y)
{
if(x>z)
{
System.out.print("最大是:"+x);
System.out.print("最小是:"+z);
}
else
{
System.out.print("最大是:"+z);
System.out.print("最小 是:"+y);
}
}
else if(y<z)
{
System.out.print("最大值"+z);
System.out.print("最小是:"+x);
}
else
{
System.out.print("最大值"+y);
}
}
}