Question

MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab...

MUST BE IN C++

The lab is called "7.5.9.1 Exceptions: including information in exceptions"

The lab is as follows:

Objectives

Familiarize the student with:

  • using exceptions in real programs;
  • the simplification of exception throwing;
  • throwing exceptions in constructors.

Scenario

IP header – imagine you have a class or struct (choose one) describing an IP header which holds (apart from other fields) two string fields that contain Source IP Address and Destination IP Address. Check these two fields and throw an exception in a constructor, and using a special check method, try to write one version of this code only. The condition of the check method is that both IP numbers are valid. Start with simple checks – the IP number must consist of four numbers in the 0-255 range, separated by a dot. Append appropriate information about which of the IP numbers is invalid.

212.112.212.11, 212.112.212.12
212.112.212.333, 212.112.212.33

Valid IP Header.
Invalid IP Header - Source IP Address is invalid.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ CODE:

#include<iostream>
#include<sstream>
using namespace std;
class IPHeader
{
public:
string srcip;
string destip;
IPHeader(string s,string d)
{
srcip=s;
destip=d;
}
bool valid()
{
if(checkValid(srcip,"Source IP")&&checkValid(destip,"Destination IP"))
return true;
return false;
}
bool checkValid(string ip,string side)
{
try
{
int n=ip.size();
int d1=0,d2=0,d3=0;
int count=0;
for(int i=0;i<n;i++)
{
if(ip[i]=='.')
{
if(count==0)
d1=i;
else if(count==1)
d2=i;
else if(count==2)
d3=i;
count++;
}
}
if(count!=3)
throw side;
stringstream s1(ip.substr(0,d1));
stringstream s2(ip.substr(d1+1,d2-d1-1));
stringstream s3(ip.substr(d2+1,d3-d2-1));
stringstream s4(ip.substr(d3+1,n-d3-1));
int n1=0,n2=0,n3=0,n4=0;
s1>>n1;
s2>>n2;
s3>>n3;
s4>>n4;
if(n1<0||n1>255||n2<0||n2>255||n3<0||n3>255||n4<0||n4>255)
throw side;
return true;
}
catch(string side)
{
cout<<"Invalid IP Header - "<<side<<" Address is invalid"<<endl;
return false;
}
}
};
int main()
{
IPHeader obj1("212.112.212.11","212.112.212.12");
if(obj1.valid())
cout<<"Valid IP Header"<<endl;
IPHeader obj2("212.112.212.333","212.112.212.33");
if(obj2.valid())
cout<<"Valid IP Header"<<endl;
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as...

    MUST BE IN C++ The lab is called "7.3.7.1 Exceptions: file checks" The lab is as follows: Objectives Familiarize the student with: situations when exceptions are thrown; handling file-related exceptions. Scenario Write a class that holds a (2×2) matrix, and add two methods to work with files: one method to load the matrix from a file (in any format) and one method to save the matrix to a file (in the same format). Add code to handle exceptional situations (file...

  • Part 2: Programming with Exceptions In this part of the lab , you will write a...

    Part 2: Programming with Exceptions In this part of the lab , you will write a program that fetches the information stored at a give URL on the web and saves that data to a file. This will also include networking and file operations and partly an exercise in using exceptions. For doing I/O, Java has a pair of nice abstractions: InputStream and OutputStream. These are abstract classes in the package java.io. An InputStream is a place from which you...

  • Introduction In this lab we will look at the information given by thrown exceptions. In particular,...

    Introduction In this lab we will look at the information given by thrown exceptions. In particular, we will see that a given design does not give the most useful information that it could. We will explore how to fix it. The Program We have three classes in the project. StringArray is similar to the class in the homework, with much functionality removed. Names is meant to be a collection of names that has a StringArray used to store them. It...

  • CSC151 JAVA PROGRAMMING LAB #7 OBJECTIVES . . . In this lab assignment, students will learn:...

    CSC151 JAVA PROGRAMMING LAB #7 OBJECTIVES . . . In this lab assignment, students will learn: To get an overview of exceptions and exception handling • To explore the advantages of using exception handling • To declare exceptions in a method header • To throw exceptions in a method • To write a try-catch block to handle exceptions To develop applications with exception handling To use the finally clause in a try-catch block To write data to a file using...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected...

    PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected situations. It is difficult to write a program that can handle all possible situations, as we have found out through the many programs we have written. For example, should your program accept accidental input? Does it use the metric system or the empirical system? Do users of your program know which system it uses? In order to deal with all these possibilities, exceptions were...

  • Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1...

    Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1 The value for Base = 2 and digits = 1 0 0 1 is 9 Enter base: 16 supply a list of digits separated by space: 99 All digits must be in the range [0,n) Enter base: 16 supply a list of digits separated by space: 9 9 The value for Base = 16 and digits = 9 9 is 153 Enter base: 2...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT