Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Exception Handling

phillyluvr
phillyluvr 


Objectives Learn how to handle exceptions Key Concepts Exceptions are raised when something unusual happens in your program and can be handled with exception handlers. Lab Assignment This lab assignment will ask you to write a program that catches exceptions. Exercise 1 1. Create a New Project named DivideTwo. 2. Cut and paste from the DivideTwo.java file into the DivideTwo class. 3. Enter a zero for the denominator. What exception gets thrown? 4. Modify the program so that it handles the exception thrown in a catch statement when a zero is entered and informs the user. 5. The output should look as follows: Enter the numerator: 25 Enter the denominator: 5 25/5=5 or Enter the numerator: 5 Enter the denominator: 0 Cannot divide by zero! Submit a document with the source code and sample output in Blackboard. DivideTwo.java import java.util.*; public class DivideTwo { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the numerator: "); int numerator = keyboard.nextInt(); System.out.print("Enter the denominator: "); int denominator = keyboard.nextInt(); System.out.println(numerator + "/" + denominator + "=" + numerator / denominator); } } Attachments Tags Clarifications

Answers

Solution for Exception

mariobeka
mariobeka 



Below you'll find the solution you are looking for, as well as the screen shot of the program running. Attachments
DivideTwo.java (1 K) Download Preview
denominator is 0 // we have to surround the following with a try-catch. try { System.out.println(numerator + "/" + denominator + "=" + numerator

Screen Shot 2015-11-14 at 02.00.29.png (40 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Exception Handling

sudheer488
sudheer488 



Please find the code along with the output screenshots as per your requirements Attachments
Divide Two.rar (14 K) Download







Preview
used for execution of your tasks. These targets are usually executed before and after some main targets. They are: -pre-init: called before initialization of project properties -post-init: called after initialization of project properties -pre-compile: called before javac compilation -post-compile: called after javac compilation

Preview
testClass="${test.class}" testincludes="${javac.includes}"/> </target> <target depends="init,compile-test-single,-pre-test-run-single" if="have.tests" name="-debug-start-debuggee-test-method"> <fail unless="test.class">Must select one file in the IDE or set test.class</fail> <fail unless="test.method">Must select some method in the IDE or set test.method</fail> <j2seproject3:test-debug excludes="" includes="${javac.includes}" testClass="${test.class}" testMethod="${test.method}" testin

Preview
<?xml version="1.0" encoding="UTF-8"?> <project-private xmlns="http://www.netbeans.org/ns/project-private/1">

Preview
</source-roots> <test-roots> <root id="test.src.dir"/> </test-roots>

Preview
try { Scanner keyboard = new Scanner(System.in); System.out.print("Enter the numerator: "); int numerator = keyboard.nextInt(); System.out.print("Enter the denominator: ");

Output1.PNG (15 K) Download Preview

output2.PNG (9 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer

Exception Handling

felo
felo 




find files plus output screen shots
Attachments
DivideTwo.rar (3 K) Download




Preview
System.out.print("Enter the numerator: "); numerator = keyboard.nextInt(); System.out.print("Enter the denominator: "); denominator = keyboard.nextInt(); if (denominator == 0) { System.out.println("Cannot divide by zero!."); } else { quotient = (double) numerator / denominator; System.out.println(numerator + "/" + denominator +

DIVIDE.png (141 K) Download Preview

dividebytwo.png (142 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer