Lab 3
Step One
First, create an empty directory for lab3. There is no starter code for this lab.
You will be throwing and catching exceptions in this exercise.
Create a file called RuntimeException.h and put the following code in it.
#include <string>
class RuntimeException {
private:
string errorMsg;
public:
RuntimeException(const string& err) { errorMsg = err; }
string getMessage() const { return errorMsg; }
}
Step Two
In a new .cpp file in your directory, write a main function that will call another function test1. Test1 should return void and take no arguments.
In the test1 function, put a try/catch statement. Inside the try
block, first throw a RuntimeException, giving it some error message
of your choice. You will need to #include "RuntimeException.h" at
the top of the file in order to do this.
After the throw statement, print the string "failed to throw in
test1" and an endline to cout. In the what-to-catch part
of the catch clause, put RuntimeException& exception.
In the catch body, print out "passed. " and the error message from
the runtime exception.
For this time, I will give you what the code should look like, but in the rest of the exercise I won't. Part of the exercise is interpreting my words into code.
void test1() {
try {
throw RuntimeException("Replace this error message");
cout << "failed to throw in test1" << endl;
}
catch (RuntimeException& exception) {
cout << "passed. " << exception.getMessage() << endl;
}
}
Compile and run your program to make sure it passes the test.
Step Three
Now create another void function called test2. test2 should have a for loop that takes i from 1 to 10. Inside the for loop, put a try/catch statement. Make the try part throw a RuntimeException if i is equal to 4. Again catch a RuntimeException. When you catch the exception (in the catch body) print out "passed at i=" followed by the value of i, followed by a space and then the exception's error message. After printing this, return from the function test2. Finally, after the for loop, print "failed to throw in test2".
Now add a call to test2 after the call to test1 in the function main. Compile and run your program and make sure it passes both tests. Also make sure it prints "passed at i=4 …" for the second test.
Step Four
Create a subclass of RuntimeException called LaboratoryException. Be sure that LaboratoryException's constructor accepts a string argument that it then passes to RuntimeException's constructor as part of its initializers.
Now include that subclass's .h file in your main file. Create a function test3 that is a copy of test2, except that it throws a LaboratoryException and reports a failure to throw in test3. It should still catch a RuntimeException. Add test3 to the tests called from main. Compile and run and verify that all three tests are passed.
Step Five
Create a new class called Laboratory. Laboratory should have one member function, which returns void, called execute(). It takes no arguments and may throw a LaboratoryException. Be sure to declare the fact that it can throw this exception. execute should be coded so that it always throws a LaboratoryException, with some appropriate error message. After the throw statement it should print a "failed" message.
Now go back to your main file and create a function called
test4. test4 should have a try/catch statement where the try body
creates a Laboratory object (preferably as a local variable, not
with the new operator), calls its execute() member
function, and prints out a failure message.
The catch block should catch a LaboratoryException this time, and
print out a "passed" message with the exception's error
message.
Add test4 to the tests called by your main routine. Compile and run to verify that all four tests pass. Then call the TA to demonstrate your code and get your mark.
Provided the code in C++ ASAP with comments
/* Step1 */
#pragma once
#include<iostream>
#include<string>
using namespace std;
class RuntimeException {
private:
string errorMsg;
public:
RuntimeException(const string& err) { errorMsg =
err; }
string getMessage() const { return errorMsg; }
};
/*
step2
*/
#include"RuntimeException.h"
/*Test1 should return void and take no arguments.*/
void test1();
int main()
{
//call the function
test1();
//to hold the output window
system("pause");
return 0;
}
void test1()
{
try
{
throw RuntimeException("test1()
throw Runtime Exception!!! ");
cout << "failed to throw in
test1" << endl;
}
catch (RuntimeException& exception)
{
cout << "passed. " <<
exception.getMessage() << endl;
}
}
//output

//Step 3
/*
step2
*/
#include"RuntimeException.h"
/*Test1 should return void and take no arguments.*/
void test1();
void test2();
int main()
{
//call the function
test1();
test2();
//to hold the output window
system("pause");
return 0;
}
void test1()
{
try
{
throw RuntimeException("test1()
throw Runtime Exception!!! ");
cout << "failed to throw in
test1" << endl;
}
catch (RuntimeException& exception)
{
cout << "passed. " <<
exception.getMessage() << endl;
}
}
/*
step 3
*/
void test2()
{
for (int i = 1; i <= 10;
i++)
{
try
{
/*throw a RuntimeException if i is equal to 4.
*/
if (i == 4)
throw
RuntimeException("test2() throw Runtime Exception");
}
catch
(RuntimeException &exception)
{
cout << "passed at i = " << i
<< " " << exception.getMessage() << endl;
}
}
}
//output

//step 4
/*
step 4
*/
/* Step1 */
#pragma once
#include<iostream>
#include<string>
using namespace std;
class RuntimeException {
private:
string errorMsg;
public:
RuntimeException(const string& err) { errorMsg =
err; }
string getMessage() const { return errorMsg; }
};
#pragma once
#include"RuntimeException.h"
class LaboratoryException:public RuntimeException
{
/*
LaboratoryException's constructor accepts
a string argument that it then passes to
RuntimeException's
constructor as part of its initializers.
*/
public:
LaboratoryException(const string &msg)
:RuntimeException(msg)
{
}
};
/*
step2
*/
#include"RuntimeException.h"
#include"LaboratoryException.h"
/*Test1 should return void and take no arguments.*/
void test1();
void test2();
void test3();
int main()
{
//call the function
test1();
test2();
test3();
//to hold the output window
system("pause");
return 0;
}
void test1()
{
try
{
throw RuntimeException("test1()
throw Runtime Exception!!! ");
cout << "failed to throw in
test1" << endl;
}
catch (RuntimeException& exception)
{
cout << "passed. " <<
exception.getMessage() << endl;
}
}
/*
step 3
*/
void test2()
{
for (int i = 1; i <= 10;
i++)
{
try
{
/*throw a RuntimeException if i is equal to 4.
*/
if (i == 4)
throw
RuntimeException("test2() throw Runtime Exception");
}
catch
(RuntimeException &exception)
{
cout << "passed at i = " << i
<< " " << exception.getMessage() << endl;
}
}
}
void test3()
{
for (int i = 1; i <= 10; i++)
{
try
{
/*throw a
LaboratoryException if i is equal to 4. */
if (i ==
4)
throw LaboratoryException("test3() throw
Laboratory Exception");
}
catch (RuntimeException
&exception)
{
cout <<
"passed at i = " << i << " " <<
exception.getMessage() << endl;
}
}
}
//output

======================================================================
//This is complete code from step1 to step 5
//please put all the file in sepcified header files
/* RuntimeException.h */
#pragma once
#include<iostream>
#include<string>
using namespace std;
class RuntimeException {
private:
string errorMsg;
public:
RuntimeException(const string& err) { errorMsg =
err; }
string getMessage() const { return errorMsg; }
};
===============================================
/*
LaboratoryException.h
*/
#pragma once
#include"RuntimeException.h"
class LaboratoryException:public RuntimeException
{
/*
LaboratoryException's constructor accepts
a string argument that it then passes to
RuntimeException's
constructor as part of its initializers.
*/
public:
LaboratoryException(const string &msg)
:RuntimeException(msg)
{
}
};
======================================
/*Laboratory.h*/
#pragma once
#include"LaboratoryException.h"
class Laboratory
{
/**
aboratory should have one member function,
which returns void, called execute().
*/
public:
void execute()
{
throw
LaboratoryException("Laboratory Exception is thrown by Laboratory
class..");
cout << "Failed" <<
endl;
}
};
==================================
/*
Main.cpp
*/
#include"Laboratory.h"
/*Test1 should return void and take no arguments.*/
void test1();
void test2();
void test3();
void test4();
int main()
{
//call the function
test1();
test2();
test3();
test4();
//to hold the output window
system("pause");
return 0;
}
void test1()
{
try
{
throw RuntimeException("test1()
throw Runtime Exception!!! ");
cout << "failed to throw in
test1" << endl;
}
catch (RuntimeException& exception)
{
cout << "passed. " <<
exception.getMessage() << endl;
}
}
/*
step 3
*/
void test2()
{
for (int i = 1; i <= 10;
i++)
{
try
{
/*throw a RuntimeException if i is equal to 4.
*/
if (i == 4)
throw
RuntimeException("test2() throw Runtime Exception");
}
catch
(RuntimeException &exception)
{
cout << "passed at i = " << i
<< " " << exception.getMessage() << endl;
}
}
}
void test3()
{
for (int i = 1; i <= 10; i++)
{
try
{
/*throw a
LaboratoryException if i is equal to 4. */
if (i ==
4)
throw LaboratoryException("test3() throw
Laboratory Exception");
}
catch (RuntimeException
&exception)
{
cout <<
"passed at i = " << i << " " <<
exception.getMessage() << endl;
}
}
}
void test4()
{
try
{
Laboratory lab;
lab.execute();
}
catch (LaboratoryException &exception)
{
cout << "Passed " <<
exception.getMessage() << endl;
}
}
//output

//if you need any help regarding this solution ......... please leave a comment .......... thanks
passed. test1() throw Runtime Exception!!! Press any key to continue
passed. test1() throw Runtime Exception!!! passed at i = 4 test2() throw Runtime Exception Press any key to continue
passed. test1() throw Runtime Exception!!! passed at i = 4 test2() throw Runtime Exception passed at i 4 test3() throw LaboratoryException Press any key to continue
passed. test1( ) throw Runtime Exception ! ! ! passed at i = 4 test2() throw Runtime Exception passed at i = 4 test3() throw Laboratory Exception Passed Laboratory Exception is thrown by Laboratory class.. Press any key to continue
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
Problem 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named Complement with the following: • A public static method named onesComplement that takes a String as a parameter and returns a String that is the 1’s complement of the parameter. • If the String is not a valid binary number (i.e. only has 0s and 1s), you should throw an IllegalArgumentException with the message "Not a valid binary number". •...
Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....
Problem 1
1. In the src → edu.neiu.p2 directory, create a package named
problem1.
2. Create a Java class named StringParser with the
following:
• A public static method named findInteger that takes a String
and two char variables as parameters (in that order) and does not
return anything.
• The method should find and print the integer value that is
located in between the two characters. You can assume that the
second char parameter will always follow the first...
Question 5 (10 points) Assume you have the following code, with blanks to be filled in below. public static void g () ( throw new () public static void f() try ) catch ( 12), e) System.out.print ("A") ; return } catch ( System.out. print ("B") throw ei e) ( 13) catch ( 14) e) ( System.out.print ("C") ; } finally System.out.print ("D") ; System.out.print ("E") ; } public static void main (String (] args) try f() } catch (...
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...
JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...
Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer to exercise 13 in chapter 3 (page 232)?? "A. Create a "standard" exception class called ThirteenException. B. Write a program that repeatedly prompts the user to enter a string. After each string is entered the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words...
What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...