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

Easy Java programming: FILL IN MISSING CODE in the specified areas to achieve th

ID: 3724443 • Letter: E

Question

Easy Java programming:

FILL IN MISSING CODE in the specified areas to achieve the Sample Program Dialogue. (or close to it)

TAKE SCREENSHOT OF THE Compiled Output.

//-------------------------------------------------------

// Chapter #11, Extra Problem #1

// ProblemX1.java

//-------------------------------------------------------

import java.util.Scanner;

//---------------------------------------------------------

public class ProblemX1

//---------------------------------------------------------

{

   //---------------------------------------------------------

   public static void main(String[] args)

   //---------------------------------------------------------

   {

      Scanner IN = new Scanner(System.in);

  

      System.out.print("type [1-7+]? ");

      while ( IN.hasNext() )

      {

   Student ensures that valid input is guaranteed here. Hint Add a try-statement like the one found in 11.3. (bottom of page)

         int type = IN.nextInt();

         try

        {

            ThrowException(type);

      //==================================================

      // *Optional Question* Under what conditions, if any, does flow-of-control "get to" the following statement?

      //==================================================

          System.out.printf("In main() try block after reference to ThrowException ");

         }

         catch ( MyException1 object )

         {

            System.out.printf("In main() MyException1 was handled ");

         }

   Student combines exception handlers (catch blocks) for MyException2 and MyException3. Hint See 11.3. (bottom of page)

Student adds single exception handler for MyException4 that distinguishes between

      StudentException1 and StudentException2 using the instanceof operator and displays

      the object.getMessage() clearly identified. Hint ¡BE CAREFUL! Consider that both

      ( object instanceof StudentException1 ) and ( object instanceof StudentException2 )

      are true when object is a reference to a StudentException2 object!

         catch ( Exception object )

         {

            System.out.println("In main() unexpected exception was handled");

         }

         finally

         {

            System.out.println("In main() finally clause executed");

         }

        

         System.out.print(" type [1-7+]? ");

      }

   }

  

   //-------------------------------------------------------

   static void ThrowException(int type) throws Exception

   //-------------------------------------------------------

   {

      try ( MyClass theObject = new MyClass() )

      {

         switch ( type )

         {

            case 1: throw( new MyException1() );

            case 2: throw( new MyException2() );

            case 3: throw( new MyException3() );

            case 4: Student adds throw-ing of a StudentException1 object here.

            case 5: Student adds throw-ing of a StudentException2 object here.

            case 6: System.out.println("ThrowException() does not throw an exception");

                     break;

            case 7: System.out.println("ThrowException() terminates program execution");

                     System.exit(0);

            default: throw( new Exception("Unexpected") );

         }

      }

      catch ( MyException1 object )

      {

         System.out.println("In ThrowException() MyException1 partially handled, re-throwed");

         throw( object );

      }

   Student defines a finally block here that outputs the message

      "In ThrowException() finally clause executed" to the standard error stream.

   }

}

//-------------------------------------------------------

class MyException1 extends Exception

//-------------------------------------------------------

{

   public MyException1()

   {

      super("MyException1");

   }

}

//-------------------------------------------------------

class MyException2 extends Exception

//-------------------------------------------------------

{

   public MyException2()

   {

      super("MyException2");

   }

}

//-------------------------------------------------------

class MyException3 extends Exception

//-------------------------------------------------------

{

   public MyException3()

   {

      super("MyException3");

   }

}

   Student adds definition for class StudentException1 thrown when type = 4 and

      StudentException2 thrown when type = 5. StudentException1 must be a subclass of

      the class MyException4 (which the student also must define) and StudentException2

      must be a subclass of StudentException1. The constructor for the three classes

      MyException4, StudentException1, and StudentException2 must take a String message

      parameter.

//-------------------------------------------------------

class MyClass implements AutoCloseable

//-------------------------------------------------------

{

   public MyClass()

   {

      System.out.println("MyClass constructor called");

   }

  

   @Override

   public void close()

   {

      System.out.println("MyClass close() method called");

   }

}

Explanation / Answer

here is your Program : ----------->>>>>>>>

import java.util.Scanner;

//---------------------------------------------------------
public class ProblemX1
//---------------------------------------------------------
{
//---------------------------------------------------------
public static void main(String[] args)//---------------------------------------------------------
{
  Scanner IN = new Scanner(System.in);
  System.out.print("type [1-7+]? ");
  while ( IN.hasNext() )
  {
   try{
    int type = IN.nextInt();
    try{
     ThrowException(type);
     //================================================
     // *Optional Question* Under what conditions, if any, does flow-of-control "get to" the following statement?
     //==================================================
     System.out.printf("In main() try block after reference to ThrowException ");
    }catch ( MyException1 object ){
     System.out.printf("In main() MyException1 was handled ");
    }
    catch(MyException2 obj){
     System.out.printf("In main() MyException2 was handled ");
    }
    catch(MyException3 obj){
     System.out.printf("In main() MyException3 was handled ");
    }
    catch(MyException4 obj){
     if(obj instanceof StudentException1){
      System.out.printf("In main() StudentException1 was handled ");
     }
     if(obj instanceof StudentException2){
      System.out.printf("In main() StudentException2 was handled ");
     }
    }
    /*
Student adds single exception handler for MyException4 that distinguishes between
      StudentException1 and StudentException2 using the instanceof operator and displays
      the object.getMessage() clearly identified. Hint ¡BE CAREFUL! Consider that both
      ( object instanceof StudentException1 ) and ( object instanceof StudentException2 )
      are true when object is a reference to a StudentException2 object!
*/
    catch ( Exception object ){
     System.out.println("In main() unexpected exception was handled");
    }
    finally{
     System.out.println("In main() finally clause executed");
    }
   }catch(Exception e){
    System.out.println("*** Invalid Input ***");
    System.exit(0);
   }
   System.out.print(" type [1-7+]? ");
  }
}
//-------------------------------------------------------
static void ThrowException(int type) throws Exception//-------------------------------------------------------
{
  try ( MyClass theObject = new MyClass() )
  {
   switch ( type )
   {
    case 1: throw( new MyException1() );
    case 2: throw( new MyException2() );
    case 3: throw( new MyException3() );
    case 4: throw(new StudentException1("throwing StudentException1"));
    case 5: throw(new StudentException2("throwing StudentException2"));
    case 6: System.out.println("ThrowException() does not throw an exception");
      break;
    case 7: System.out.println("ThrowException() terminates program execution");
      System.exit(0);
    default: throw( new Exception("Unexpected") );
   }
  }
  catch ( MyException1 object )
  {
   System.out.println("In ThrowException() MyException1 partially handled, re-throwed");
   throw( object );
  }/*

   Student defines a finally block here that outputs the message
      "In ThrowException() finally clause executed" to the standard error stream.
*/
}
}
//-------------------------------------------------------
class MyException1 extends Exception
//-------------------------------------------------------
{
public MyException1()
{
  super("MyException1");
}
}
//-------------------------------------------------------
class MyException2 extends Exception
//-------------------------------------------------------
{
public MyException2()
{
  super("MyException2");
}
}
//-------------------------------------------------------
class MyException3 extends Exception
//-------------------------------------------------------
{
public MyException3()
{
  super("MyException3");
}
}
/*

   Student adds definition for class StudentException1 thrown when type = 4 and
      StudentException2 thrown when type = 5. StudentException1 must be a subclass of
      the class MyException4 (which the student also must define) and StudentException2
      must be a subclass of StudentException1. The constructor for the three classes
      MyException4, StudentException1, and StudentException2 must take a String message
      parameter.

*/
class MyException4 extends Exception{
public MyException4(String msg){
  super(msg);
}
}
class StudentException1 extends MyException4{
public StudentException1(String msg){
  super(msg);
}
}
class StudentException2 extends MyException4{
public StudentException2(String msg){
  super(msg);
}
}
//-------------------------------------------------------
class MyClass implements AutoCloseable
//-------------------------------------------------------
{
public MyClass()
{
  System.out.println("MyClass constructor called");
}

@Override
public void close()
{
  System.out.println("MyClass close() method called");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote