Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

image text in transcribed

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

// 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");

}

}

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

What is the purpose of remedial statutes?

Answered: 1 week ago

Question

2. What appeals processes are open to this person?

Answered: 1 week ago

Question

4. How would you deal with the store manager?

Answered: 1 week ago