In: Computer Science
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program
2) Create a class called TestScore that has a constructor that accepts an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Create a driver to make the array and demonstrate the TestScore class.
3) Create a class called InvalidTestScore. Modify your TestScores class so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid.
Q 1: StringTooLongException:
Explanation part is given below the program in the form of table
import java.util.*;
class StringTooLongException extends Exception
{
public StringTooLongException(String s)
{
super(s);
}
public String toString(){
return ("Too many Characters in the given string") ;
}
}
public class Main
{
public static void main(String[] args) {
String str = "",entirestring = "";
Scanner sc = new Scanner(System.in);
for(int i=1;i>0;i++){
str = sc.nextLine();
if(str.equals("DONE")){
break;
}
else
entirestring += str;
}
System.out.println(entirestring);
System.out.println(entirestring.length());
try{
if(entirestring.length()>=20)
throw new StringTooLongException(entirestring);
}
catch(StringTooLongException exception){
System.out.println(exception);
}
}
}
Explanation for Q1:
Code |
Explanation |
class StringTooLongException extends Exception |
User defined exception class which inherits from “Exception” class. toString() is used to provide user defined message when this class is called in “catch” block in the main function |
Main() public static void main(String[] args) { |
Obtain the input from the user and store each input in “entirestring”. The input is obtained using “for” loop unless “DONE” is encountered. entirestring and its length are printed for the sake of understanding |
try{ throw new StringTooLongException(entirestring); } |
This is doing the actual logic of checking the length of the user input. Here I have assume if length>20, then throw the error. Error is thrown using throw new StringTooLongException(entirestring); The above statement will reach the catch block and it will print the message from toString() of StringTooLongException class. |
Q.2 Illegal Argument Exception:
import java.util.*;
class TestScores
{
private double[] scores;
double totalscore = 0;
public TestScores(double arrayscore[])
{
for (int i = 0; i < arrayscore.length; i++)
{
scores = new double[arrayscore.length];
if (arrayscore[i] < 0 || arrayscore[i] > 100)
{
throw new IllegalArgumentException("Bad scores" + "\n\tInvalid
score found." + "\n\tPosition: " + (i+1) + ", Score given: " +
arrayscore[i]);
}
else
{
scores[i] = arrayscore[i];
totalscore += arrayscore[i];
}
}
}
public double getAverageScore()
{
return totalscore / scores.length;
}
}
public class Main
{
public static void main(String[] args) {
double[] testScores1 = { 97.5, 66.7, 88.0, 10.0, 99.0
};
double[] testScores2 = { 97.5, 66.7, -88.0, 100.0, 99.0 };
TestScores test=null;
try
{
test = new TestScores(testScores1);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
try
{
test = new TestScores(testScores2);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
System.out.print("Good scores"+ "\n\tThe average of the good scores
is " + test.getAverageScore());
}
}
Explanation for Q2
Code |
Explanation |
class TestScores |
User Define method Test Score to through IllegalArgument Exception. As said in the question an array is used and iterated to check for the given condition. If the condition violates, it throws error. Otherwise it will calculate the total score. getAverageScore() is used to return the total score of that object |
double[] testScores1 = { 97.5, 66.7, 88.0, 10.0, 99.0 }; |
This is to declare and initialize array with varied score |
try |
This will not through Illegal Argument Exception because the score contains correct values. |
try |
testScore2 contains illegal values hence this will throw Exception (user defined). |
Answer for Q3:
import java.util.*;
class InvalidTestScoreException extends Exception
{
public InvalidTestScoreException(String s)
{
super(s);
}
public String toString(){
return ("There is an invalid score in the given list of score")
;
}
}
class TestScores
{
private double[] scores;
double totalscore = 0;
public TestScores(double arrayscore[])
{
try
{
for (int i = 0; i < arrayscore.length; i++)
{
scores = new double[arrayscore.length];
if (arrayscore[i] < 0 || arrayscore[i] > 100)
{
throw new InvalidTestScoreException("Invalid score");
}
else
{
scores[i] = arrayscore[i];
totalscore += arrayscore[i];
}
}
}
catch(InvalidTestScoreException e)
{
System.out.println(e.getMessage());
}
}
public double getAverageScore()
{
return totalscore / scores.length;
}
}
public class Main
{
public static void main(String[] args) {
double[] testScores1 = { 97.5, 66.7, 88.0, 10.0, 99.0
};
double[] testScores2 = { 97.5, 66.7, -88.0, 100.0, 99.0 };
TestScores test=null;
test = new TestScores(testScores1);
test = new TestScores(testScores2);
System.out.print("Good scores"+ "\n\tThe average of the good scores
is " + test.getAverageScore());
}
}
Explanation:
Same as previous only following modification done:
a) New class called InvalidTestScoreException added
b) try catch block removed from Main()
c) Test Score class handles the exception