Thursday, 1 September 2016

CORE JAVA LAB@HOME 9

Question-1 Differentiate Between the checked and unchecked also write algorithm.


Answer:-

view sourceprint?

01 package com.javacodegeeks.snippets.basics;

02

03 import java.io.File;

04 import java.io.IOException;

05

06

07 public class CheckedUncheckedExceptions {

08

09 public static void main(String[] args) {

10

11

12 // We must catch the checked exception ‐ to test use an existing file!

13

14 try {

15

16 CheckedUncheckedExceptions.checkSize("testFile.txt");

17

18 } catch (IOException e) {

19

20

21 e.printStackTrace();

22

23 }

24

25

26 // The unchecked exception doesn't requires you to catch it

27

28 CheckedUncheckedExceptions.divide(1, 0);

29 }

30

31 /**

32 * This method throws a Checked Exception, so it must declare the

33 * Exception in its method declaration

34 *

35 * @param fileName given file name

36 * @throws IOException when the file size is to large.

37 */

38

39 public static void checkSize(String fileName) throws IOException {

40

41 File file = new File(fileName);

42

43 if (file.length() > Integer.MAX_VALUE) {

44

45

46 throw new IOException("File size is too large!");

47

48 }

49 }

50

51 /**

52 * This method throws a RuntimeException.

53 * There is no need to declare the Exception in the method declaration

54 *

55 * @param x the dividend

56 * @param y the divisor

57 *

58 * @return the division result

59 * @throws ArithmeticException when arithmetic exception occurs (divided by zero)

60 */

61 public static int divide(int x, int y) {

62

63 return x / y;

64 }

65

66 }


Question-2 Create a calculator.

Answer:-























Question-3 David has assign the task to take the input from the user and display the appropriate result                    to the user.

Answer:-














Question-3 Registered Member Details.

Answer:-





No comments:

Post a Comment