Assignment 2: Guide – SWE 619 – ALL – Spring 2010

 

Goal: Basic familiarity with Java Exceptions.

 

Basic Assignment

 

 1 // Fig. 13. 1: DivideByZeroNoExceptionHandling.java

 2 // An application that attempts to divide by zero.

 3

 4

 5 public class DivideByZeroNoExceptionHandling

 6 {

 7    // demonstrates throwing an exception when a divide-by-zero occurs

 8    public static int quotient( int numerator, int denominator )

 9    {

10       return numerator / denominator; // possible division by zero

11    }  // end method quotient

12

13    public static void main( String args[] )

14    {

15      

16

17       // Provide an implementation to input the numerator, input the denominator

18       // call the quotient method, and output the result

19      

20      

21

22      

23      

24      

25    }  // end main

26 } // end class DivideByZeroNoExceptionHandling

 

Assignment:

 

1)   Implement the above code fragment completing the main section based on the comment.

2)   Compile and execute bullet one with several test cases including a denominator = 0; Save the output to file.

3)   Augment the implementation in bullet one as follows:

a.   Write a requires clause that addresses a denominator = 0;

b.   Modify the implementation in bullet one to check for denominator = 0; make sure you prevent and provide the caller program with mechanism to recover from when the denominator = 0;

c.   Modify the implementation in bullet one to address the case with denominator = 0 by using an exception

 

 

Submit the following:

 

1)   Provide Java source files for all four implementations.

2)   Provide output files for all four implementations (include several valid and invalid test cases)