Make sure you study Background materials first before moving on to case assignment.
In Background materials, we learned that there are checked and unchecked exceptions in Java. The Java compiler requires methods that throw a checked exception to have an exception handling statement, yet does not have this requirement for programs that might throw unchecked exceptions. As a good programmer however, it is good practice for us to catch unchecked exceptions even if it is not required. Next, we will view some examples.
1) Unchecked exceptions
You have seen this program in CSC212. This program asks you to select one of the three kinds of gas, and how many gallons of gas you want to buy and then calculate how much you need to pay. Run this program, type in "three" when asked to put the amount of gas you need, and see what happens. Did the program crash? Congratulations, you just got an exception thrown at you. What kind of error message you get? Here is some information on the NumberFormatException class information. You will see that NumberFormatException is a subclass of IllegalArgumentExceptionwhich is a subclass of RuntimeException. In the background material, we learned that all exception types that ARE subclasses of RuntimeException are unchecked exceptions, which means that even there is a possibility that these exceptions would occur, and the compiler does not demand that these exceptions are handled. So your program still compiles okay but might still crash under certain conditions. Although it is not required to write exception handling statements to deal with unchecked exceptions, it is good practice to include these statements to avoid these problems.
The program should be modified to include a try/catch block to handle exceptions. An if block should be added before code that might throw a NumberFormatException. This exception will be thrown when the program has attempted to convert a string to one of the numeric types, but the string does not have the appropriate format. When users type in "three" instead of the number 3, the program is not able to convert the string into a number so a NumberFormatException is thrown.
Next, lets move on to discuss the catch statement:
catch (NumberFormatException e)
JOptionPane.showMessageDialog (null, "Invaid data input, try again");
You must spell NumberFormatException correctly since it is a standard java class. However, you can use whatever you like for the parameter name. In this case, I use name "e" in order to represent exception but you can use other names. Also, make sure you check JOptionPane class, and look for all the methods it has. Specifically look for the showMessageDialog method. To further experiment with this program, modify the catch block as show below, run the program again, and see what happens.
catch (NumberFormatException e)
JOptionPane.showMessageDialog (null, e.getMessage( ));
Download MathException.java. Run this program and see if you can crash it. Be a bad user and do anything other than what you are supposed to do: type in double variables, strings, or even type in 0 as a denominator. Then check the error message without error checking and you will see that different kinds of exceptions might occur. Either users put in incorrect data types (type in double instead of int variable, or a string variable "three" instead of 3, etc), or an integer might divide by 0 which is an arithmetic mistake. To correct these errors, we might need multiple catch blocks to handle these errors. Download the version which has built in error checking -MathExceptionWithErrorChecking.java from Math Exception With Error Checking. Run the revised program again and try to break it as before.
Comments about unchecked exception:
As we can see, the compiler does not require unchecked exceptions to be caught by the program. Without exception handling, programs will still compile but might still crash from user errors or other reasons.
2) checked exceptions
Programs that might have checked exceptions must provide exception handling statements or the compiler will not allow the program to compile. We will explore checked exceptions in module 2.
Case Assignment Overview
Write a Java application to calculate how much federal and state tax you need to pay. The program should accomplish the following tasks: ask your name, yearly income, federal tax rate, and state tax rate, then calculate and display the amount of tax you need to pay. Your program must catch user input errors. As the examples given above, use a dialog window to enter program input instead of console-based commands. After you are done, zip "compress" the java source file along with some screen shots of your output and submit.
Ability to use checked exceptions in a Java program.
Write a Java application that accomplishes the following tasks.
- Ask the number of homework assignments students have submitted in a semester
- Calculate the letter grade the student has earned based on the average of these assignments.
- Assume the full score for each assignment is 100. Student will get 0 for each un-submitted assignment.
- Student's letter grade is based on the average of all their assignments
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
0 - 60 F
- The program needs to have built in Exception handling.
After you are done, zip "compress" the source file along with a screen shot of the output and submit.
You are expected to understand and apply proper data structures in solving the programming task. Complete exception handling should be in place to protect the program from crashing due to invalid inputs.
Recently Asked Questions
- Please refer to the attachment to answer this question. This question was created from GEOL 101 Prob-3.docx. Additional comments: "I am in need with help with
- Please refer to the attachment to answer this question. This question was created from General Quality Concepts and Theories in Business.docx.
- Please refer to the attachment to answer this question. This question was created from Ch.8_-_Glossary_and_Problems.docx. Additional comments: "where can the