Java课堂作业第十周
1 标准库异常练习,编写程序,产生如下列表的异常,捕获并打印getMessage()字符串
要求,不允许使用throw new AarrayIndexOutOfBoundException方式抛出。
2 自定义异常练习
目的:检查学生分数的有效性并统计输出结果。
1)设计student类,包含私有整型成员变量score
2)创建student数组(包含15000个student对象)并为每个student对象生产[1,150]的随机整数分数
3)定义分数超出正常值的异常类,命名为scoreexceedmaxvalueexception,在类中设置成员变量,存储导致异常产生的原因(大于100的分数值)
4)编写iscore(student)函数,分析传递过来的student的学生分数,检查学生分数的合法性,若分数大于100,抛出上述异常。
题目1
package ch10;
import java.util.Scanner;
public class a {
public static int test1(){
int a[]={0,1};
try{
int b=a[101];
return b;
}catch (Exception e){
e.printStackTrace();
String msg=e.getMessage();
System.out.println(msg);
return -1;
}
}
public static int test2(){
int a=5,b=0;
Scanner scanner=new Scanner(System.in);
//b=scanner.nextInt();
try{
int c=a/b;
return c;
}catch (Exception e){
e.printStackTrace();
String msg=e.getMessage();
System.out.println(msg);
return -1;
}
}
public static String test3(){
try{
Object object=null;
String string=object.toString();
return string;
}catch (Exception e){
e.printStackTrace();
String msg=e.getMessage();
System.out.println(msg);
return "error";
}
}
public static int test4(){
try{
String num="1a";
System.out.println(Integer.parseInt(num));
int b=Integer.parseInt(num);
return b;
}catch (Exception e){
e.printStackTrace();
String msg=e.getMessage();
System.out.println(msg);
return -1;
}
}
public static int test5(){
try{
Scanner scanner=new Scanner(System.in);
int a=scanner.nextInt();
return a;
}catch (Exception e){
e.printStackTrace();
String msg=e.getMessage();
System.out.println(msg);
return -1;
}
}
public static void main(String[] args) {
a t1=new a();
t1.test1();
//System.out.println(t1.test1());
a t2=new a();
t2.test2();
//System.out.println(t2.test2());
a t3=new a();
t3.test3();
//System.out.println(t3.test3(3));
a t4=new a();
t4.test4();
a t5=new a();
t5.test5();
}
}
题目2
package ch10;
public class Main {
public static void main(String[] args) {
int a[] = new int[15000];
for (int i = 0; i < 15000; i++) {
a[i] = (int) (Math.random() * 150 + 1);
}
Student stu = new Student();
for (int i = 0; i < 15000; i++) {
try {
stu.isScoreValid(a[i]);
} catch (Exception e) {
System.out.println(e);
}
}
int zhengchang=0,yichang=0,zhengchangscore=0,yichangscore=0;
for(int i=0;i<15000;i++){
if(a[i]>100){
yichang+=1;
yichangscore+=a[i];
}
else{
zhengchang+=1;
zhengchangscore+=a[i];
}
}
System.out.println("正常分数人数"+zhengchang);
System.out.println("正常分数平均分"+zhengchangscore/zhengchang);
System.out.println("异常分数人数"+yichang);
System.out.println("异常分数平均分"+yichangscore/yichang);
}
}
class Student {
private int score;
public void isScoreValid(int a) throws ScoreExceedsMaxValueException {
String yichang="";
if (a > 100){
yichang+=a;
throw new ScoreExceedsMaxValueException(yichang+" ");
}
}
}
class ScoreExceedsMaxValueException extends Exception {
public ScoreExceedsMaxValueException() {
super();
}
public ScoreExceedsMaxValueException(String message) {
super(message);
}
}
题目3
package ch10;
public class c {
public static int calculate(int upLimit) {
int maxLen = 0;
int maxId = 3;
for (int i = 3; i < upLimit; i++) {
int len = calculateCollatzLen(i);
if (len > maxLen) {
maxLen = len;
maxId = i;
}
}
return maxId;
}
public static int calculateCollatzLen(int i) {
long n = i;
int count = 1;
while( n != 1 ){
if( n % 2 == 0){
n = n / 2;
}else{
n = n * 3 + 1;
}
count ++;
}
return count;
}
public static void main(String[] args) {
System.out.println(c.calculate(1000000));
}
}