Programming Fundamentals Lab Manual 14
Programming Fundamentals Lab Manual 14
Statement Purpose:
One of the secrets of the low latency is that each implementation can generate a custom built fix engine based exactly on the schema it requires.
Activity Outcomes:
- Describe the notation of exception handling
- React correctly when certain exceptions occur
- Use Java’s exception-handling facilities effectively in classes and programs
1) Stage J (Journey)
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Java program that extends java Exception class.
Solution:
class MyException1 extends Exception1 { private int ex; MyException1(int a) { ex=a; } public String toString() { return "MyException1[" + ex +"] is less than zero"; } } class Test { static void sum(int a,int b) throws MyException1 { if(a<0) { throw new MyException1(a); } else { System.out.println(a+b); } } public static void main(String[] args) { try { sum(-10, 10); } catch(MyException1 me) { System.out.println(me); } } }
Activity 2:
NOTE: If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.
It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).
Subclass overridden method with exception.
Solution:
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Activity 3:
Subclass overridden method with no exception.
Solution:
import java.io.*; class Super { void show() throws Exception { System.out.println("parent class"); } } public class Sub extends Super { void show() //Correct { System.out.println("child class"); } public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Activity 4:
Subclass overridden method with parent Exception.
Solution:
import java.io.*; class Super { void show() throws ArithmeticException { System.out.println("parent class"); } } public class Sub extends Super { void show() throws Exception { System.out.println("child class"); } //Compile Time Error public static void main(String[] args) { try { Super s=new Sub(); s.show(); } catch(Exception e){} } }
Activity 5:
Using ObjectInputStream to Read from a File
Solution:
public class BinaryInput { public static void main(String[] args) { String fileName = "numbers.dat"; try { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName)); System.out.println("Reading the nonnegative integers"); System.out.println("in the file " + fileName); int anInteger = inputStream.(readInt); while (anInteger >= 0) { System.out.ptintln(anInteger); anInteger = inputStream.readInt(); } System.out.println("End of reading from file."); inputStream.close(); } catch(FileNotFoundException e) { System.out.println("Problem opening the file " + fileName); } catch(EOFException e) { System.out.println("Problem reading the file " + fileName); System.out.println("Reached end of the file."); } catch(IOException e) { System.out.println("Problem reading the file " + fileName); } } }
3) Stage v (verify)
Home Activities:
Activity 1: Write java code to create an input stream of type ObjectInputStream that is named from File and is connected to a file named stuff.data.
Activity 2: Write a Java program that asks the user for the name of a binary file and writes the first data item in that file to the screen. Assume that the first data item is a string that was written to the file with method WriteUTF.
4) Stage a2 (assess)
Assignment:
Write a complete Java program that asks the user for a file name, tests whether the file exists, and if the file does exist, ask the user whether or not it should be deleted and then does as the user requests.
Comments
Post a Comment