Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exception Handling in Java 1. Answer the following related to exception handling

ID: 3823681 • Letter: E

Question

Exception Handling in Java

1. Answer the following related to exception handling in Java.

       (a)    An exception is an object that is thrown by a method back to the method that called it to indicate

                that a certain type of error occurred that the called method cannot resolve. TRUE / FALSE (1 pt.)

       (b)   Create an Exception class type named InvalidPasswordException. (1 pt.)

       (c)    Suppose that there is a void method named ValidatePassword that is passed a username and password,
                both as a String type, and throws an InvalidPasswordException if the provided password is
                incorrect for the username passed, otherwise produces no result. Make the changes in the following
                code to prompt for a specific username and password, and display either “Invalid Password Provided,”
                or “You are now logged in.” (3 pts.)

        public static void main(String args[])

        {

                  int num1, num2;

                  boolean valid_input = false;

                  Scanner input = new Scanner(System.in);

              while (!valid_input)

                  {

                             System.out.println(“Enter numerator <space> denominator “);

                             num1 = input.nextInt();    

                             num2 = input.nextInt();

                             try

                        {

                                      result = quotient(num1, num2);

                                      System.out.println(“The result of “ + num1 + “/” + num2 + “ is “ + result);

                                      valid_input = true;

                             }

                             catch ( ArithmeticException ex)  

                        {

                                System.out.println(“Second number entered must be non-zero”);

                             }

                 }

       }

Linked Lists

1. (a) Declare a class named User that contains the following information:

            String name

            String username

            String password

            Include in the class a single constructor that is passed a particular name, username, and
            password; setters and getters, and a toString method that returns the information in
            a given User object as follows:

            "Ashley Smith   username: asmith    password: aWjPP406!"

      (b) Declare a class named UserNode that contains a single User object and a link to another
            UserNode. Include an appropriate constructor and an appropriate set of methods for this
            class. (4 pts).

                                   

2. Create a class named UserAccounts that maintains a list of users, with an appropriate
      constructor, and the following methods: (24 pts.)

            isEmpty() – returns true if list is empty, and false otherwise

            findPassword – returns the password for a given user name

            changePassword – updates the password for a given user name and password. If they do
                                           not match, the password is not updated.

            addUser – passed a User object to add

            deleteUser – deletes a user for a given user name and password. If they do not match,
                                    the user is not deleted.

            printUsers – prints a list of all users, including name, username and password

Stacks and Queues

3. Answer the following questions related to use of stacks.

       (a) What is left on the stack after the following stack operations? (3 pts.)

                  push(10)

                  push(20)

                  pop()

                  push(15)

                  push(5)

                  push(40)

                  pop()

      (b)   if top = 0 indicates that a stack is empty (for a particular array implementation of a stack) indicate what
                  values are logically on the stack in the following when top = 5. (4 pts.)

0

1

2

3

4

5

6

7

8

9

10

5

28

14

29

32

27

18

34

52

42

23

4.    What is left on the queue after the following queue operations? Be sure to indicate where the front of the
queue is. (3 pts.)

                  enqueue(10), enqueue(5), enqueue(20), dequeue(), enqueue(8), dequeue(), enqueue(15)

Recursion

2. (a) Complete the line below for recursive function (method) factorial. (2 pts.)

      public static int factorial(int n)

        {

               if (n == 0)

                    return 1;

               else

                    return (_____________________);

        }

     (b) Determine what the following recursive function returns for any value n >= 1. (3 pts.)

                  public static int func(int n)

                           {

                                    if(n == 1)

                                             return 2;

                                    else

                                             return 2 * func(n-1);

                  }

Sorting and Searching / Algorithm Analysis

3. Answer the following questions related to algorithm rate of growth.

       (a)    If the rate of growth of Algorithm A is O(n2) and Algorithm B is O(n3), it is possible that
                Algorithm B will be faster than Algoritihm A for certain size input. TRUE / FALSE   (2 pts.)

       (b) Match the following (draw connecting lines). (4 pts.)

                       Sequential Search                                                                                                                      O(nlogn)

                       Binary Search                                                                                                                               O(n)

                       Simple Sorts (Selection Sort, Insertion Sort, BubbleSort)                                 O(logn)

                       Fastest Sorting Algorithms (QuickSort, MergeSort)                                              O(n2)

4.   Give the results of the list below after the first two passes of each of the following sorting algorithms.
         (4 pts.)

              List                                                  SELECTION SORT                  INSERTION SORT                  BUBBLESORT

            10

                  6

                  21

                  14

                  19

                  25

                  8

                  29

                  11

                  4

0

1

2

3

4

5

6

7

8

9

10

Explanation / Answer

2(a)   

public static int factorial(int n)

        {

               if (n == 0)

                    return 1;

               else

                    return n*factorial(n-1);

        }

2(b) Determine what the following recursive function returns for any value n >= 1. (3 pts.)
It computer the Power of 2 , i.e for n = 1 returns 2 ,

i.e for n = 2 returns 4 ,

i.e for n = 3 returns 8 ,

3 (a) What is left on the stack after the following stack operations? (3 pts.)

                  push(10) //10 is pushed

                  push(20)//20 is pushed

                  pop()//20 is popped

                  push(15)//15 is pushed

                  push(5)//5 is pushed

                  push(40)//40 is pushed

                  pop()//40 is popped

5,15,10 is left in the stack

       (a)    If the rate of growth of Algorithm A is O(n2) and Algorithm B is O(n3), it is possible that
                Algorithm B will be faster than Algoritihm A for certain size input. FALSE

B can never be faster than A for any input size

(b) Match the following (draw connecting lines). (4 pts.)

                       Sequential Search -----> O(n)

                       Binary Search =====>    O(logn)

                       Simple Sorts (Selection Sort, Insertion Sort, BubbleSort) =====>    O(n2)   

                       Fastest Sorting Algorithms (QuickSort, MergeSort) ====> O(nlogn)     

Thanks, let me know if there is any concern.