Question: //Chapter 12; Java Programming; Joyce Farrell; 8th ed. // DebugTwelve1-4; need to fix, there are error // each debugTwelve1 - 4 output 1 public class
//Chapter 12; Java Programming; Joyce Farrell; 8th ed.
// DebugTwelve1-4; need to fix, there are error
// each debugTwelve1 - 4 output
1 public class DebugEmployeeIDException extends Exception 2 { 3 public DebugEmployeeIDException() 4 { 5 super(s); 6 } 7 }
-------------------------
1 // This class determines the logarithm of a number 2 public class DebugTwelve1 3 { 4 public static void main(String[] args) throws ArithmeticException 5 { 6 double num = -8.8, result; 7 try 8 { 9 if(num <= 0) 10 throw(new ArithmeticException()); 11 result = Math.log(num); 12 System.out.println("Result is " + result); 13 } 14 catch() 15 { 16 System.out.println("Can't take logarithm for value of zero or lower"); 17 } 18 } 19 }
------------------------------------
1 // A byte can't hold a value higher than 127 2 // This program throws an ArithmeticException 3 // if adding two bytes yields a value that is too high 4 public class DebugTwelve2 5 { 6 public static void main(String[] args) 7 { 8 byte num1 = 120, num2 = 120, result; 9 final byte HIGHBYTE = 127; 10 try 11 { 12 allowed = (byte)(HIGHBYTE - num1); 13 if(num2 > allowed) 14 throw(new Arithmeticexception()); 15 result = (byte)(num1 + num2); 16 System.out.println("Result is " + result); 17 } 18 catch(ArithmeticException error) 19 { 20 System.out.println("Byte can't hold value higher than " + HIGHBYT); 21 } 22 } 23 }
------------------------------------
1 // Catch exceptions for array index out of bounds 2 // or dividing by 0 3 import javax.swing.*; 4 public class DebugTwelve3 5 { 6 public static void main(String[] args) 7 { 8 String inStr; 9 int num, result; 10 int[] array = {12, 4, 6, 8}; 11 inStr = JOptionPane.showInputDialog(null, "Enter a number "); 12 num = Integer.parse(inStr); 13 try 14 { 15 for(int x = 0; x < array.length; ++x) 16 { 17 result = array[x] / num; 18 JOptionPane.showMessageDialog(null, "Result of division is " + result); 19 result = array[num]; 20 JOptionPane.showMessageDialog(null, "Result accessing array is " + result); 21 } 22 } 23 catch(ArithmeticException error) 24 { 25 JOptionPane.showMessageDialog(null, "Arithmetic error - division by 0"); 26 } 27 catch(IndexException error) 28 { 29 JOptionPane.showMessageDialog(null, "Index error - subscript out of range"); 30 } 31 } 32 }
----------------------------
1 // An employee ID can't be more than 999 2 // Keep executing until user enters four valid employee IDs 3 // This program throws a FixDebugEmployeeIDException 4 import javax.swing.*; 5 public class DebugTwelve4 6 { 7 public static void main(String[] args) 8 { 9 String inStr, outString = ""; 10 final int MAX = 999; 11 int[] emp = new int[4]; 12 for(x = 0; x < emp.length; ++x) 13 { 14 inStr = JOptionPane.showInputDialog(null, "Enter employee ID number"); 15 throw 16 { 17 emp[x] = Integer.parseInt(inStr); 18 if(emp[x] > MAX) 19 { 20 throw(new FixDebugEmployeeIDException("Number too high " + emp[x])); 21 } 22 } 23 catch(NumberFormatException error) 24 { 25 --x; 26 JOptionPane.showMessageDialog(null, inStr + " Nonnumeric ID"); 27 } 28 catc(FixDebugEmployeeIDException error) 29 { 30 --x; 31 JOptionPane.showMmrge"); 32 } 33 } 34 for(int x = 0; x < emp.length; ++x) 35 { 36 outString = outString + emp[x] + " "; 37 } 38 JOptionPane.showMessageDialog(null, "Four valid IDS are: " + outString); 39 } 40 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
