Lab 7 exceptions | Computer Science homework help

CSE 1302 J

Fall 2014

Lab 7

 

Exception handling

 

Exceptions Aren’t Always Errors

            Use the phrase “I won the lottery for 2 million!!!”

 

Placing Exception Handlers

            Use the 3 phrases:        10 20 30 40 50

                                                1 fish, 2 fish

                                                We have 3 dogs and 2 cats.

 

Throwing Exceptions

            Run the program with:   a negative number

                                                            a value between 0-16

                                                            a value > 16

 

For each part of the lab, turn in the source and output.  Staple all three labs together in the order given.

 

Exceptions Aren’t Always Errors

 

File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. Save it to your directory and study it, then compile and run it to see how it works. In reading the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0..25 (by subtracting ‘A’) for use as an index. No test is done to ensure that the characters are in fact letters.

 

1.   Run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArrayIndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter characters, but not count them. Of course, you could explicitly test the value of the character to see if it is between ‘A’ and ‘Z’. However, an alternative is to go ahead and use the translated character as an index, and catch an ArrayIndexOutOfBoundsException if it occurs. Since you don’t want to do anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows:

         Put the body of the first for loop in a try. NOTE: The body of a for loop is the code in braces under the for  heading line.

         Add a catch that catches the exception, but don’t do anything with it.

Compile and run your program.

 

2.   Now modify the body of the catch so that it prints a useful message (e.g., “Not a letter”) followed by the exception. Compile and run the program. Although it’s useful to print the exception for debugging, when you’re trying to smoothly handle a condition that you don’t consider erroneous you often don’t want to. In your print statement, replace the exception with the character that created the out of bounds index. Run the program again; much nicer!

 

 

// ****************************************************************

// CountLetters.java

//

// Reads a phrase from the standard input and prints the number of

// occurrences of each letter in that phrase.

//         

// ****************************************************************

import java.util.Scanner;

public class CountLetters

{

    public static void main(String[] args)

    {

      int[] counts = new int[26];

      Scanner scan = new Scanner(System.in);

 

      //get word from user

      System.out.print(“Enter a phrase: “);

      String word = scan.nextLine();

 

      //convert to all upper case

      word = word.toUpperCase();

 

      //count frequency of each letter in string

      for (int i=0; i < word.length(); i++)

          counts[word.charAt(i)-‘A’]++;

 

      //print frequencies

      System.out.println();

      for (int i=0; i < counts.length; i++)

          if (counts [i] != 0)

            System.out.println((char)(i +’A’) + “: ” + counts[i]);

 

    }

 

}

 

Placing Exception Handlers

 

File ParseInts.java contains a program that does the following:

 

         Prompts for and reads in a line of input

         Uses a second Scanner to take the input line one token at a time and parses an integer from each token as it is extracted.

         Sums the integers.

         Prints the sum.

 

Save ParseInts to your directory and compile and run it. If you give it the input

 

        10 20 30 40

 

it should print

 

        The sum of the integers on the line is 100.

 

Try some other inputs as well. Now try a line that contains both integers and other values, e.g.,

 

        We have 2 dogs and 1 cat.

 

You should get a NumberFormatException when it tries to call Integer.parseInt on “We”, which is not an integer. One way around this is to put the loop that reads inside a try and catch the NumberFormatException but not do anything with it. This way if it’s not an integer it doesn’t cause an error; it goes to the exception handler, which does nothing. Do this as follows:

 

         Modify the program to add a try statement that encompasses the entire while loop. The try and opening { should go before the while, and the catch after the loop body. Catch a NumberFormatException and have an empty body for the catch.

         Compile and run the program and enter a line with mixed integers and other values. You should find that it stops summing at the first non-integer, so the line above will produce a sum of 0, and the line “1 fish 2 fish” will produce a sum of 1. This is because the entire loop is inside the try, so when an exception is thrown the loop is terminated. To make it continue, move the try and catch inside the loop. Now when an exception is thrown, the next statement is the next iteration of the loop, so the entire line is processed. The dogs-and-cats input should now give a sum of 3, as should the fish input.

 

 

 

// ****************************************************************

// ParseInts.java

//

// Reads a line of text and prints the sum of the integers in the line. 

//         

// ****************************************************************

 

import java.util.Scanner;

 

public class ParseInts

{

    public static void main(String[] args)

    {

      int val, sum=0;

      Scanner scan = new Scanner(System.in);

      String line;

 

      System.out.println(“Enter a line of text”);

      Scanner scanLine = new Scanner(scan.nextLine());

 

      while (scanLine.hasNext())

          {      

            val = Integer.parseInt(scanLine.next());

            sum += val;

          }

      System.out.println(“The sum of the integers on this line is ” + sum);

    }

 

}

 

 

Throwing Exceptions

 

File Factorials.java contains a program that calls the factorial method of the MathUtils HYPERLINK “https://www.sweetstudy.com/content/student%20code/MathUtils.java” HYPERLINK “https://www.sweetstudy.com/content/student%20code/MathUtils.java” HYPERLINK “https://www.sweetstudy.com/content/student%20code/MathUtils.java”.java class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers.

1.   Returning 1 as the factorial of any negative integer is not correct—mathematically, the factorial function is not defined for negative integers. To correct this, you could modify your factorial method to check if the argument is negative, but then what? The method must return a value, and even if it prints an error message, whatever value is returned could be misconstrued. Instead it should throw an exception indicating that something went wrong so it could not complete its calculation. You could define your own exception class, but there is already an exception appropriate for this situation—IllegalArgumentException, which extends RuntimeException. Modify your program as follows:

         Modify the header of the factorial method to indicate that factorial can throw an IllegalArgumentException.

         Modify the body of factorial to check the value of the argument and, if it is negative, throw an IllegalArgumentException. Note that what you pass to throw is actually an instance of the IllegalArgumentException class, and that the constructor takes a String parameter. Use this parameter to be specific about what the problem is.

         Compile and run your Factorials program after making these changes. Now when you enter a negative number an exception will be thrown, terminating the program. The program ends because the exception is not caught, so it is thrown by the main method, causing a runtime error.

         Modify the main method in your Factorials class to catch the exception thrown by factorial and print an appropriate message, but then continue with the loop. Think carefully about where you will need to put the try and catch.

 

2.   Returning a negative number for values over 16 also is not correct. The problem is arithmetic overflow—the factorial is bigger than can be represented by an int. This can also be thought of as an IllegalArgumentException—this factorial method is only defined for arguments up to 16. Modify your code in factorial to check for an argument over 16 as well as for a negative argument. You should throw an IllegalArgumentException in either case, but pass different messages to the constructor so that the problem is clear.

 

 

// ****************************************************************

// Factorials.java

//

// Reads integers from the user and prints the factorial of each.

//         

// ****************************************************************

import java.util.Scanner;

 

public class Factorials

{

    public static void main(String[] args)

    {

      String keepGoing = “y”;

      Scanner scan = new Scanner(System.in);

 

      while (keepGoing.equals(“y”) || keepGoing.equals(“Y”))

          {

            System.out.print(“Enter an integer: “);

            int val = scan.nextInt();

            System.out.println(“Factorial(” + val + “) = ”

                           + MathUtils.factorial(val));

            System.out.print(“Another factorial? (y/n) “);

            keepGoing = scan.next();

          }

    }

}

 

 

// ****************************************************************

// MathUtils.java

//

// Provides static mathematical utility functions.

//         

// ****************************************************************

public class MathUtils

{

    //————————————————————-

    // Returns the factorial of the argument given

    //————————————————————-

    public static int factorial(int n)

    {

      int fac = 1;

      for (int i=n; i>0; i–)

          fac *= i;

      return fac;

    }

}

EssayProffesor.com
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.