Sie sind auf Seite 1von 8

PIJ MR SUPPORT DOCUMENT

Source Topic/Activity Name Time Duration in min


Chapter 9 Exercise 1 30
Chapter 9 Exercise 2 20
Chapter 9 Exercise 3 20
Chapter 9 Exercise 4 10

Exercise 1

David is assigned a task of creating a Application to store Employee details. For this David decides
to display the following menu when the Application starts.

1. Enter Data.
2. Display Data
3. Exit

Objective: To learn creating custom Exceptions.

Lab steps:
1. Open the Netbeans. Create a new
project chapter09_ex1.

2. Task: Create a custom Exception to


check proper option from menu is
selected
a. Create a class named
selectionException .
b. This class must extend the
Exception class.
c. Add a constructor with no
parameters to display the text.
"Invalid input, Please select a valid
menu option"

3. Task: create a custom Exception Hint: public PatternException(String msg) {


a. Create a class PatternException
extending the RuntimeException. super(msg);
b. create a public constructor with a }
string parameter.In this constructor
call the base class constructor
passing the same string as a
parameter.
c. Create one constructor with no
parameters displaying the text
Invalid pattern for Employee ID...

4. Code the EmployeeDetails class Hint:


a. Add a new class
EmployeeDetails. String employeeDetails[][] = new String[100][8];
b. Create a array of 100 rows 8 public void showMenu() throws SelectionException
columns at the class level. {
d. Declare a variable option of int type
e. Create a method showMenu() int option;
containing the statements to
// sop to display menu options.
display the choices.
Select the option from the user Scanner sc = new Scanner(System.in);
f. Ensure that the user is doing the
selection between 1 to 3, otherwise option = sc.nextInt();
raise the selection Exception.
if (option < 1 || option > 3) {
g. When case 1 is selected call the
method enterData(),showMenu throw new SelectionException();
h. When case 2 call } else {
DisplayData(),showMenu()
i. When case 3 is selected call switch (option) {
exitMenu()
j. Otherwise call the showMenu()

5. Create a method enterData() to accept public void enterData() {


the details Scanner sc = new Scanner(System.in);
char choice = 'y';
int i = 0;
do {
System.out.println("Enter Employee Id: ");
String empid = sc.next().toLowerCase();
char c = empid.charAt(0);

if (c != 'e') {
throw new PatternException();
}

employeeDetails[i][0] = empid;

System.out.println("Enter Employee Name:


");
employeeDetails[i][1] = sc.next();
System.out.println("Enter Department: ");
employeeDetails[i][2] = sc.next();
System.out.println("Enter Designation: ");
employeeDetails[i][3] = sc.next();
System.out.println("Enter Date of Joining: ");
employeeDetails[i][4] = sc.next();
System.out.println("Enter Date of Birth: ");
employeeDetails[i][5] = sc.next();
System.out.println("Enter marital status: ");
employeeDetails[i][6] =
sc.next().toLowerCase();
if (employeeDetails[i][6].equals("married"))
{
System.out.println("Enter date of
marriage: ");
employeeDetails[i][7] = sc.next();
} else {
employeeDetails[i][7] = "na";
}
i++;
System.out.println("Do you wish to add
more records:");
String ch = sc.next().toLowerCase();
choice = ch.charAt(0);
} while (choice == 'y');
}
6. Create a method in Employeedetails Hint:
class to display the Data. public void displayData() throws
SelectionException {
System.out.println("Employee Details:");
for (int i = 0; i < employeeDetails.length; i++) {
for (int j = 0; j < employeeDetails[i].length;
j++) {
if (employeeDetails[i][j] == null) {
showMenu();
} else {

System.out.println(employeeDetails[i][j]);
}

}
7. Create the method exitMenu to exit the public void exitMenu() {
Application System.exit(0);
}
8. Coding the main method. int flag = 0;
EmployeeDetails obj = new EmployeeDetails();
do {
flag = 0;
{
try {
obj.showMenu();
} catch (SelectionException Obja) {
System.out.println("Exception caught: "
+ Obja);
}

} while (flag != 1);

9. Compile and Test the App.


10. Observe that all requirements are
satisfied.
Objective: To implement Assertions in the program

Lab Steps:

1. Create a new Java Application using


NetBeans .Name it as chapter09_ex2

2. Include the selctionException class


created in exercise 1.

3. Add a new class to the project NewAxis. static void showAge(int mage) throws
a. Add data members to the class name and SelectionException {
contactno of string type. Age for Integer if ((mage < 18) || (mage > 55)) {
throw new SelectionException();
type.
}
b. Create scanner class object to accept the }
details for the user. public void form() {
c. Create a static method showAge taking a System.out.println("*******Disco
int parameter and throws the Registration Form*******");
selectionException. System.out.println("Enter the name: ");
d. If the age accepted is not between 18 name = details.nextLine();
System.out.println("Enter the contact
and 55 throw the selectionException
number: ");
e. Create a method form to accept the contactno = details.nextLine();
details line contactno
System.out.println("Enter the age: ");
age = details.nextInt();
try {
showAge(age);
} catch (SelectionException Obja) {
}
}
4. Coding the main method in NewAxis. NewAxis obj1 = new NewAxis();
a. Create a object of NewAxis and call
the method form. obj1.form();

5. Compile and run the Application. run:


*******Disco Registration Form*******
Enter the name:
chhaya
Enter the contact number:
87687687687
Enter the age:
30
BUILD SUCCESSFUL (total time: 19 seconds)

Lab Steps:
1. Create a new Java Application name it as
chapter09_ex3
2. Create a class named CalculatorApp.

3. Coding the CalculatorApp class int num1, num2, res = 0;


a. Include the main method in the class. Scanner numbers = new Scanner(System.in);
b. In the main method Declare variable Scanner operator = new Scanner(System.in);
String op;
to accept the numbers.you can name
System.out.println("Enter the 1st number");
them as num1, num2, res num1 = numbers.nextInt();
c. Create a object of Scanner class to assert (num1 > 0) : "The number should be
accept the numbers greater than 0.";
d. Use assert statements to check System.out.println("Enter the 2nd number");
number entered by the user is num2 = numbers.nextInt();
greater than 0 assert (num2 > 0) : "The number should be
greater than 0.";
e. Check the operator entered by the
System.out.println("enter the operator");
user in the if condition and perform op = operator.nextLine();
the correct calculation. assert ((op.equals("+")) || (op.equals("-")) ||
(op.equals("*")) || (op.equals("/"))) : "The
operator is not valid.";
if (op.equals("+"))
{
res = num1 + num2;
}
else if (op.equals("-"))
{
if (num1 > num2)
{
res = num1 - num2;
} else
{
res = num2 - num1;
}
}
else if (op.equals("*"))
{
res = num1 * num2;
}
else if (op.equals("/"))
{
res = num1 / num2;
}
else
{
System.out.println("Wrong operator");
}
System.out.println("The result is " + res);
}

4. Testing the Application Enable the assertions for the Application.

Right click the project- select properties- Run


In the VM options type ea

5. Test
Case 1: Input negative numbers
Case 2: Input positive numbers
Objective: To handle Divide by zero Exception

Lab Steps:

1. Create a new Java Application using


NetBeans. Name it as chapter09_ex4

2. Add a class named Division Hint:


a. Create a main method in the class. int num1, num2, result;
b. Declare the variables num1, num2, Scanner input = new Scanner(System.in);
System.out.println("Enter the 1st number");
result of int types
num1 = input.nextInt();
c. Use Scanner class to input two System.out.println("Enter the 2nd number");
numbers. num2 = input.nextInt();
d. Include the try block followed by
catch to handle Arithmetic Exception try {
e. Perform the division operation in the result = num1/num2;
try block. System.out.println("The result is " +result);
}
catch (ArithmeticException e) {
System.out.println("Division by zero not
Possible!");
}
3. Test the Application to valid and invalid Expected Result:
data. run:
Enter the 1st number
23
Enter the 2nd number
0
Division by zero not Possible!
BUILD SUCCESSFUL (total time: 5 seconds)

Happy Learning!!

Das könnte Ihnen auch gefallen