[go: up one dir, main page]

0% found this document useful (0 votes)
20 views4 pages

Workshop 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Workshop 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Workshop #2: Exceptions

Learning Outcomes:

Upon successful completion of this workshop, you will have demonstrated the abilities
to:

 Practice handling errors in your program.


 Describe to your instructor what you have learned in completing this workshop.

Requirements:

Part1

Write a Java program to accept a number and print out it. If the number is below 1 then a
message “the number is invalid” is printed out. Using do..while to input again, try-catch block to
handle errors.

The user interface may be:


Enter the number: - 2
The number is invalid”

Enter the number: world


The number is invalid”

Enter the number: 2


The number is 2

Step by step workshop instructions:


- Create a new project named “workshop2”
- In the project, create a new file named “Part1.java”
- In the method main, you type:

1
Part 2

Write a Java program to accept a string and print out it. If the string does not match SExxx(x is
digit) then a message “the string is invalid” is printed out. Using do..while to input again.
Hint: In library class String, you should use the method matches() to do this, use try-
catch block and use throws to handle errors.

The user interface may be:


Input the string 1: I love u
the string is invalid

Input the string 1: SE123


the string is SE123

Step by step workshop instructions:

Background: In this workshop, you will use the pattern string( also called regular expression,
see more What is a Regular Expression? - Definition from Techopedia). You should read the
document to complete the exercise.
Task 1: use try-catch

- In the project “Workshop2”, create a new file named “Part2.java”


- In the method main, you type:

2
At the row 9, use rules of the regular expression to create a pattern string “SExxx”, x is digit

Task 2: use throws keyword

- create a new file named “Part3.java”


- in the method main, type:

3
Part 3

- create a new file named “Part4.java” to build program by out as screen

Part 4

- Create a new file named “Part5.java” to build program for calculating the expression. In
there, x is real number and n is integer positive number by keyboard input

You might also like