Workshop 08-Tran-Handling PDF

Title Workshop 08-Tran-Handling
Author Long Hoàng
Course Object-Oriented Programming
Institution FPT University
Pages 5
File Size 254.3 KB
File Type PDF
Total Downloads 105
Total Views 184

Summary

workshop...


Description

Subject: PRO192 Workshop 08: Exception handling ------------------------------------------------------------------------------------------------------------

RULES: Ws 08 should be submited on the LMS system Part 1: Compulsory Part 2: Based on what Lecturer requires. @_@ Contact me @ https://www.facebook.com/quynhtran.ly.94 ------------------------------------------------------------------------------------------------------------

Part 1: Students kindly practice Part 1 at home. Thank you :-D In this workshop, you’ll learn: •

Exception Handling



Multiple Handlers



Code Finalization and Cleaning Up



Custom Exception Classes

Create 2 exception classes named IllegalTriangleException and IllegalRightTriangleException as customs exception classes. Write a class named RightTriangle with three sides a, b, c is integer numbers. In the constructor of class RightTriangle: •

If 3 integers is not 3 side of a triangle (The sum of any two sides is greater than the other side) then throw IllegalTriangleException.



If 3 integers is not 3 side of a right triangle (follow Pythagorean Theorem) then throw IllegalRightTriangleException.

class RightTriangle{ int a, b, c;

1

//Constructor public RightTriangle(int a, int b, int c) throws IllegalTriangleException, IllegalRightTriangleException{ //implement it } }

Write a Java program to test the RightTriangle class. Three sides are accepted from keyboard and check input validation

Sample output: Enter side a: 6a Wrong input! Try again! Enter side a: 1 Enter side b: 2 Enter side c: 8 This is not a triangle! Continue? (Y/N): Y

Enter side a: 6 Enter side b: 7 Enter side c: 8 This is not a right triangle! Continue? (Y/N): Y

Enter side a: 3 Enter side b: 4 Enter side c: 5 This is a right triangle! Continue? (Y/N): N

2

Main class should be like following:

public class Ex1_Ws2 { public static void main(String[] args) { int a, b, c; Scanner nhap = new Scanner(System.in); while (true) { //enter integer a here with input validation //enter integer b here with input validation //enter integer c here with input validation

try { //call constructor of RightTriangle class RightTriangle rt = new RightTriangle(a, b, c); System.out.println("This is a right triangle!"); } catch (IllegalTriangleException e1) { System.out.println(“This is not a triangle!”); } catch (IllegalRightTriangleException e2) { System.out.println(“This is not a right triangle”); } //continue? System.out.print("Continue?(Y/N):"); //Enter a character char chon = nhap.next().charAt(0); if(chon != 'Y') break; } } }

------------------------------------------------------------------------------------------------------------

3

Part 2: Students kindly practice Part 2 in class and at home based on the Lecturer’s guidance. Thank you :-D 1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package validationdatademo; 7 8 import java.util.Scanner; 9 10 /** 11 * 12 * @author Quynh Tran Ly 13 */ 14 public class ValidationDataDemo { 15 16 /** 17 * @param args the command line arguments 18 */ 19 20 private static final Scanner in = //Students write here appropriate statements to complete this function 21 22 //check user input string 23 public static String checkInputString() { 24 //loop until user input correct 25 while (true) { 26 String result //Students write here appropriate statements to complete this function 27 if (result.isEmpty()) { 28 System.err.println("Not empty"); 29 System.out.print("Enter again: "); 30 } else { 31 return result; 32 } 33 } 34 } 35 36 //check user input int 37 public static int checkInputInt() { 38 //loop until user input correct 39 while (true) { 40 try { 41 int result = //Students write here appropriate statements to complete this function 42 return result; 43 } catch (NumberFormatException e) { 44 System.err.println("Must be input integer."); 45 System.out.print("Enter again: "); 46 } 47 } 48 } 49 50 //check user input double 51 public static double checkInputDouble () { 52 //loop until user input correct 53 while (true) { 54 try { 55 double result = //Students write here appropriate statements to complete this function

4

56 return result; 57 } catch (NumberFormatException e) { 58 System.err.println("Must be input double"); 59 System.out.print("Enter again: "); 60 } 61 62 } 63 } 64 65 //check user input yes/ no 66 public static boolean checkInputYN() { 67 System.out.print("Do you want to continue (Y/N)? "); 68 //loop until user input correct 69 while (true) { 70 String result = checkInputString(); //return true if user input y/Y 71 72 if (//Students write here appropriate statements to complete this function 73 return true ; 74 } 75 //return false if user input n/N 76 if (//Students write here appropriate statements to complete this function 77 return false; 78 } 79 System.err.println("Please input y/Y or n/N."); 80 System.out.print("Enter again: "); 81 } 82 } 83 public static void main(String[] args) { 84 // TODO code application logic here 85 while (true) { 86 87 System.out.print("Enter fruit id: "); 88 String fruitId = //Students write here appropriate statements to complete this function 89 //check id exist 90 System.out.print("Enter fruit name: "); 91 String fruitName = //Students write here appropriate statements to complete this function 92 System.out.print("Enter price: "); 93 double price = checkInputDouble(); 94 System.out.print("Enter quantity: "); 95 int quantity = //Students write here appropriate statements to complete this function 96 System.out.print("Enter origin: "); 97 String origin = checkInputString(); 98 System.out.println(fruitId+" "+fruitName+" "+ price+" "+ quantity+" "+origin); 99 //check user want to continue or not 100 if (//Students write here appropriate statements to complete this function 101 return; 102 } 103 } 104 105 }} 106

5...


Similar Free PDFs