1.Write a class with a static method that accepts a string, displays the number
ID: 3532911 • Letter: 1
Question
1.Write a class with a static method that accepts a string, displays the number of characters, and throws an
exception if any character in the string is not in the set {'a'..'z', '0'..'9', 'A'..'Z'}. The exception should be
thrown but not caught (i.e., no explicit catch block). Write a client program that calls this method and prints
"Error in Input" if the method throws an exception..
2.Design a class named Triangle that extends GeometricObject (corresponding to UML shown
below). The class contains:
Explanation / Answer
Q3:
class DivideByZero extends Exception
{
public String toString()
{
return "Cannot Divide by Zero !!";
}
}
class Overflow extends Exception
{
public String toString()
{
return "Integer range Overflow !!";
}
}
class Underflow extends Exception
{
public String toString()
{
return "Integer range Underflow !!";
}
}
public class Arithmetic
{
public static int divide(int a, int b) throws DivideByZero
{
int result=0;
try
{
result=a/b;
}
catch(ArithmeticException e){}
return result;
}
public static int add(int a, int b) throws Underflow, Overflow
{
if(a>0 && b>0)
{
if(a+b<0)
{
throw new Overflow();
}
}
else if(a<0 && b<0)
{
if(a+b>0)
{
throw new Underflow();
}
}
else
{
return a+b;
}
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.