Write an Exception class called InvalidMenuOption 1.Use a loop that asks for the
ID: 3623268 • Letter: W
Question
Write an Exception class called InvalidMenuOption1.Use a loop that asks for the user if they want to enter a set of end points or quit
2.If they choice the first option-
A. ask the user to enter 4 double values, x1,x2,y1,y2 that is represented the endpoints of a line segment in the xy plane.
B.Compute the length of the line segment by using the distance formula
distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
C.Compute the slope of the line segment using this formula
slope = (y2 - y1)-(x2 - x1)
3.If the user selects the second option, it terminates.
However, this time
We want to make this program not able to crash
1.Make sure that the user enters the correct option. Use try and catch with the InvalidMenuOption.
2.Trap all invalid inputs entires for menu choices,x1,x2,y1,y2
3. trap a divide by zero error when computing the slope. Note that division by zero raises the ArithmeticException.
Explanation / Answer
#include
#include
#include
using namespace std;
int main()
{
int ch;
try
{
do
{
cin>>ch;
if(ch==1)
{
double x1,x2,y1,y2,dist,slope;
cout<<"Enter the line segment values:"<<endl;
cin>>x1>>x2>>y1>>y2;
if(x1 >= 1000000000000000 || x2 >= 1000000000000000 || y1 >= 1000000000000000 || y2 >= 1000000000000000)
throw "Exceeded double limit";
dist=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if(x2-x1==0)
throw "Divide by zero";
slope=(y2-y1)/(x2-x1);
cout<<"Length of the line segment is:"<<endl;
cout<<"Slope of the line segment is:"<<endl;
}
else if(ch==2)
break;
else cout<<"invalid!,Please enter right choice"<<endl;
}
while(ch!=2);
}
catch(const char* ms)
{
cout<<"Error" << ms;
}
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.