Java Reference
In-Depth Information
{
Scanner inFile = new
Scaner( new FileReader("test.txt"));
PrintWriter outFile =
new PrintWriter("testavg.out");
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
outFile.printf("Test scores: %.2f %.2f %.2f %.2f %n",
test1, test2, test3, test4);
average = (test1 + test2 + test3 + test4) / 4.0;
outFile.println("Average test score: %.2f",
average);
outFile.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
}
}
}
14. Define the exception class TornadoException . The class should have two
constructors, including one default constructor. If the exception is thrown
with the default constructor, the method getMessage should return:
"Tornado! Take cover immediately!"
The other constructor has a single parameter, say, m ,of type int . If the exception
is thrown with this constructor, the method getMessage should return:
"Tornado m miles away and approaching!"
15. Write a Java program to test the class TornadoException specified in
Exercise 14.
Suppose the exception class MyException is defined as follows:
public class MyException extends Exception
{
16.
public MyException()
{
super ("MyException thrown!");
System.out.println("Immediate attention required!");
}
Search WWH ::




Custom Search