Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Each of the following files in teh Chapter12 folder of your downloadable student

ID: 3668994 • Letter: E

Question

Each of the following files in teh Chapter12 folder of your downloadable student files has a syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugTwelve1.java will become FixDebugTwelve1.java. You will also use a file named DebugEmployeeIDException.java with the DebugTwelve4.java file.

a. DebugTwelve1.java

// This class determines the logarithm of a number
public class DebugTwelve1
{
public static void main(String[] args) throws ArithmeticException
{
double num = -8.8, result;
try
{
if(num <= 0)
throw(new ArithmeticException());
result = Math.log(num);
System.out.println("Result is " + result);
}
catch()
{
System.out.println("Can't take logarithm for value of zero or lower");
}
}
}

b. DebugTwelve2.java

// A byte can't hold a value higher than 127
// This program throws an ArithmeticException
// if adding two bytes yields a value that is too high
public class DebugTwelve2
{
public static void main(String[] args)
{
byte num1 = 120, num2 = 120, result;
final byte HIGHBYTE = 127;
try
{
allowed = (byte)(HIGHBYTE - num1);
if(num2 > allowed)
throw(new Arithmeticexception());
result = (byte)(num1 + num2);
System.out.println("Result is " + result);
}
catch(ArithmeticException error)
{
System.out.println("Byte can't hold value higher than " + HIGHBYT);
}
}
}

c. DebugTwelve3.java

// Catch exceptions for array index out of bounds
// or dividing by 0
import javax.swing.*;
public class DebugTwelve3
{
public static void main(String[] args)
{
String inStr;
int num, result;
int[] array = {12, 4, 6, 8};
inStr = JOptionPane.showInputDialog(null, "Enter a number ");
num = Integer.parse(inStr);
try
{
for(int x = 0; x < array.length; ++x)
{
result = array[x] / num;
JOptionPane.showMessageDialog(null, "Result of division is " + result);
result = array[num];
JOptionPane.showMessageDialog(null, "Result accessing array is " + result);
}
}
catch(ArithmeticException error)
{
JOptionPane.showMessageDialog(null, "Arithmetic error - division by 0");   
}
catch(IndexException error)
{
JOptionPane.showMessageDialog(null, "Index error - subscript out of range");
}
}
}

d. DebugTwelve4.java

// An employee ID can't be more than 999
// Keep executing until user enters four valid employee IDs
// This program throws a FixDebugEmployeeIDException
import javax.swing.*;
public class DebugTwelve4
{
public static void main(String[] args)
{
String inStr, outString = "";
final int MAX = 999;
int[] emp = new int[4];
for(x = 0; x < emp.length; ++x)
{
inStr = JOptionPane.showInputDialog(null, "Enter employee ID number");
throw
{
emp[x] = Integer.parseInt(inStr);
if(emp[x] > MAX)
{
throw(new FixDebugEmployeeIDException("Number too high " + emp[x]));
}
}
catch(NumberFormatException error)
{  
--x;
JOptionPane.showMessageDialog(null, inStr + " Nonnumeric ID");
}
catc(FixDebugEmployeeIDException error)
{  
   --x;
JOptionPane.showMmrge");
}
}
for(int x = 0; x < emp.length; ++x)
{
outString = outString + emp[x] + " ";
}
JOptionPane.showMessageDialog(null, "Four valid IDS are: " + outString);
}
}

public class DebugEmployeeIDException extends Exception
{
public DebugEmployeeIDException()
{
super(s);
}
}

Explanation / Answer

a)

//This class determines the logarithm of a number
public class FixDebugTwelve1
{
public static void main(String[] args) throws ArithmeticException
{
double num = 8.8, result;
try
{
if(num <= 0)
throw(new ArithmeticException());
result = Math.log(num);
System.out.println("Result is " + result);
}
catch(Exception e)
{
System.out.println("Can't take logarithm for value of zero or lower");
}
}
}

b)

//A byte can't hold a value higher than 127
//This program throws an ArithmeticException
//if adding two bytes yields a value that is too high
public class FixDebugTwelve2
{
public static void main(String[] args)
{
byte num1 = 120, num2 = 120, result;
final byte HIGHBYTE = 127;
try
{
result = (byte)(num1 + num2);
if(result > HIGHBYTE)
throw(new ArithmeticException());
System.out.println("Result is " + result);
}
catch(ArithmeticException error)
{
System.out.println("Byte can't hold value higher than " + HIGHBYTE); // here was mistake
}
}
}

c)

//Catch exceptions for array index out of bounds
//or dividing by 0
import javax.swing.*;
public class FixDebugTwelve3
{
public static void main(String[] args)
{
String inStr;
int num, result;
int[] array = {12, 4, 6, 8};
inStr = JOptionPane.showInputDialog(null, "Enter a number ");
num = Integer.parseInt(inStr);
try
{
for(int x = 0; x < array.length; ++x)
{
   if(num == 0)
       throw new ArithmeticException();
result = array[x] / num;
JOptionPane.showMessageDialog(null, "Result of division is " + result);
if(num > array.length-1)
   throw new IndexOutOfBoundsException();
result = array[num];
JOptionPane.showMessageDialog(null, "Result accessing array is " + result);
}
}
catch(ArithmeticException error)
{
JOptionPane.showMessageDialog(null, "Arithmetic error - division by 0");   
}
catch(IndexOutOfBoundsException error)
{
JOptionPane.showMessageDialog(null, "Index error - subscript out of range");
}
}
}

d)

//An employee ID can't be more than 999
//Keep executing until user enters four valid employee IDs
//This program throws a FixDebugEmployeeIDException
import javax.swing.*;
public class FixDebugTwelve4
{
public static void main(String[] args)
{
String inStr, outString = "";
final int MAX = 999;
int[] emp = new int[4];
for(int x = 0; x < emp.length; ++x)
{
inStr = JOptionPane.showInputDialog(null, "Enter employee ID number");
try
{
emp[x] = Integer.parseInt(inStr);
if(emp[x] > MAX)
{
throw(new FixDebugEmployeeIDException("Number too high " + emp[x]));
}
}
catch(NumberFormatException error)
{
--x;
JOptionPane.showMessageDialog(null, inStr + " Nonnumeric ID");
}
catch(FixDebugEmployeeIDException error)
{
   --x;
   JOptionPane.showMessageDialog(null, inStr + " Nonnumeric ID");
}
}
for(int x = 0; x < emp.length; ++x)
{
outString = outString + emp[x] + " ";
}
JOptionPane.showMessageDialog(null, "Four valid IDS are: " + outString);
}
}
public class FixDebugEmployeeIDException extends Exception
{
public FixDebugEmployeeIDException(String s)
{
super(s);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote