Question: Copy Box.java (see listing at bottom of assignment) and re-name it NewBox,java . Change the class to be NewBox (so it matches the filenamk) Change

Copy Box.java (see listing at bottom of assignment) and re-name it NewBox,java.

Change the class to be NewBox (so it matches the filenamk)

Change the constructor name to be NewBox. Leave the parameters as is

In the constructor, just after the header you should see a println Box instantiated. If you do not see this, then please print out the message Box instantiated

Next. Create a double variable called ans and set it equal to 10/5

Print out ans

Set up an array called list with and initializer list of any three values; for example 5,6,7

Set up a for loop to print these values

The rest of the constructor should stay as is.

The toString should stay as is

Save and compile the program and fix any errors

Create a new driver class: CallBox.java having a main method

Print out Main has started

CallBox will instantiate an object called myNewBox by passing the constructor of NewBox class the parameter 9. (because this is lab 9)

We will NOT print the box

Add a statement to print out Main method is Ending

Make sure the program compiles and runs correctly.

Add a try to surround the instantiation of the NewBox

try

{

<<<<< instantiation here

}

Add a catch clause to catch ArithmeticException (parameter type) call the parameter name problem

Inside the catch clause printout the message from the system

Inside the catch clause printout the stack trace

HINT: See the class ExceptionScope level1 method (listing 11.4)

Copy the catch clause you just created and paste it after the end of the ArithmeticException

Change the second catch clause to catch IndexOutOfBoundsException (parameter type) call the parameter name problem

NOTE: the last line should still be a print out of Main method ending. before all the closing curly braces

Compile and Run

Listing of original Box.java and callBox.java

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

public class Box { int projectNum=0; public Box(int pnumb) { projectNum=pnumb; } public String toString() { String ans = "********************************** "+ "** Barbara Zimmerman " + "** CSC 2014 " + "** project number "+projectNum+" " + "********************************** "; return ans; } } // ---------------------------

public class callBox { public static void main (String[] args) { Box myBox = new Box(103); System.out.println (myBox); } }

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

Listing 11.4 Follows

//******************************************************************** // ExceptionScope.java Author: Lewis/Loftus // Demonstrates exception propagation. //******************************************************************** public class ExceptionScope { //----------------------------------------------------------------- // Catches and handles the exception that is thrown in level3. //----------------------------------------------------------------- public void level1() { System.out.println("Level 1 beginning."); try { level2(); } catch (ArithmeticException problem) { System.out.println (); System.out.println ("The exception message is: " + problem.getMessage()); System.out.println (); System.out.println ("The call stack trace:"); problem.printStackTrace(); System.out.println (); } System.out.println("Level 1 ending."); } //----------------------------------------------------------------- // Serves as an intermediate level. The exception propagates // through this method back to level1. //----------------------------------------------------------------- public void level2() { System.out.println("Level 2 beginning."); level3 (); System.out.println("Level 2 ending."); } //----------------------------------------------------------------- // Performs a calculation to produce an exception. It is not // caught and handled at this level. //----------------------------------------------------------------- public void level3 () { int numerator = 10, denominator = 0; System.out.println("Level 3 beginning."); int result = numerator / denominator; System.out.println("Level 3 ending."); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!