1 package com.kai.li.HomeWork0923; 2 3 /** 4 *work class for all the class 5 */ 6 public class HomeWork0923{ 7 8 /** 9 *main fuction for all the text 10 */ 11 public static void main(String[] args){ 12 //questions of part 1 13 for(int i=0;i<4;i++){ 14 int k; 15 switch(i){ 16 case 0: 17 try{ 18 int zero=0; 19 k=911/zero; 20 break; 21 }catch(ArithmeticException e){ 22 System.out.println(e.getMessage()); 23 }catch(Exception e){ 24 e.printStackTrace(); 25 System.out.println(e.getMessage()); 26 } 27 case 1: 28 try{ 29 int[] b=null; 30 k = b[0]; 31 break; 32 }catch(NullPointerException e){ 33 System.out.println(e.getMessage()); 34 }catch(Exception e){ 35 e.printStackTrace(); 36 System.out.println(e.getMessage()); 37 } 38 case 2: 39 try{ 40 int c[]=new int[2]; 41 k = c[9]; 42 break; 43 }catch(ArrayIndexOutOfBoundsException e){ 44 System.out.println(e.getMessage()); 45 }catch(Exception e){ 46 e.printStackTrace(); 47 System.out.println(e.getMessage()); 48 } 49 case 3: 50 try{ 51 char ch = "abc".charAt(99); 52 break; 53 }catch(StringIndexOutOfBoundsException e){ 54 System.out.println(e.getMessage()); 55 }catch(Exception e){ 56 e.printStackTrace(); 57 System.out.println(e.getMessage()); 58 } 59 } 60 } 61 //questions of part 2 62 Bank bank=new Bank(100); 63 try{ 64 bank.withDrawal(150); 65 }catch(InsufficientFundsException e){ 66 e.printStackTrace(); 67 }catch(NagativeFundsException e){ 68 e.printStackTrace(); 69 } 70 bank=new Bank(100); 71 try{ 72 bank.withDrawal(-15); 73 }catch(NagativeFundsException e){ 74 e.printStackTrace(); 75 }catch(InsufficientFundsException e){ 76 e.printStackTrace(); 77 } 78 //questions of part 3 79 InterfaceX ix=()->{}; 80 ix.forl('a',26); 81 ix.forl('A',26); 82 } 83 } 84 /** 85 *this example is texted of exception 86 */ 87 //define the class bank ,it has some functions with text 88 class Bank{ 89 private double balance; 90 Bank(){ 91 this.balance=0; 92 } 93 Bank(double balance){ 94 this.balance=balance; 95 } 96 public double getBalance(){ 97 return this.balance; 98 } 99 public void withDrawal(double dAmount) throws InsufficientFundsException,NagativeFundsException{ 100 if(balance<dAmount) 101 throw new InsufficientFundsException(); 102 if(dAmount<0) 103 throw new NagativeFundsException(); 104 this.balance-=dAmount; 105 } 106 } 107 //define the first exception by self 108 class InsufficientFundsException extends Exception{ 109 @Override 110 public void printStackTrace(){ 111 super.printStackTrace(); 112 System.out.println("------------"); 113 } 114 } 115 //define the seconde exception by self 116 class NagativeFundsException extends Exception{ 117 @Override 118 public void printStackTrace(){ 119 super.printStackTrace(); 120 System.out.println("||||||||||||"); 121 } 122 } 123 /** 124 *define a function interface 125 *it is created for the lembda 126 */ 127 @FunctionalInterface 128 interface InterfaceX{ 129 void printLetter(); 130 default public void forl(int a,int b){ 131 for(int i=a;i<a+b;i++){ 132 System.out.print((char)i+" "); 133 } 134 System.out.println(); 135 } 136 }