Question: Question 1: Consider the following java class 1.1 The output of the class is : 1.2 Change the instruction System.out.println(1/0) thus the exception NullPointerException is



Question 1: Consider the following java class 1.1 The output of the class is : 1.2 Change the instruction "System.out.println(1/0)" thus the exception NullPointerException is caught. 1.3 Change the instruction "System.out.println(1/0)" thus the exception "IndexOutOfBoundsException" is caught. Question 2: user defined exception. Consider the following java classes: class Negative Exception extends Exception 1. private int e; Negative Exception (int num1) I. e= num 1 ) public String toString0) 1 return " (+e+) is less than 0n; 1 1 class Demol 1 static void sum(int num,int num1] throws NegativeException 1 in(num1\&0) I throw new Negative Exception (num1): 1 else 1 System.out.printin(num+num1); 1 1 public static void main(Stringil args) I try 1 sum(5,4) : 1 catch(NegativeException e1) I System.out.printin(e1): 1 1 ) 2,1: The class output is : 2.2 Similarly to the classes above write the follewing classes : - "DemoDivision" that contains a method : - static int division(int num1, int num2) throws ZeroException (...) that calculates aam1am2 and throws the ZeroException when the num 2 is equal to 0 . - Write the main function. Question 3: Working with Files 3.1 : Ordinary file creation - Create a new folder named "FileDemo" - Open NotePad and create in "FileDemo" a new file named "fname.txt". Write your Student Id and Your name and save the file. - Check if the file "fname.txt" exists in the folder "FileDemo". The size of the file is - The absolute path to the file is 3.2 : A java code to read the file content : import java.io.File; import java.util.Scanner; public class ReadFromFileUsingScanner public static void main(String[] args) throws Exception \{ File file = new File(".......................................................................); Scanner sc = new Scanner(file); while (sc.hasNextLine()) System.out.println(sc.nextLine ()); \} - The output is - Add 3 new names in the file and run the class. The new output is public class WriteFile \{ // Main driver method public static void main(String[] args) \{ // Content to be assigned to a file // Custom input just for illustration purposes String text = "Salam Alaykom"; /I Try block to check if exception occurs try 1 // Create a FileWriter object // to write in the file Path / file name FileWriter fW riter = new FileWriter( fWriter.write(text); // Closing the file writing connection friter.elose(); // Display message for successful execution of // program on the console System.out.println("File is created suecessfully with the content."); 1 I/ Catch block to handle if exception oecurs catch (IOException e) I I/ Print the exception System.out.print(e.getMessage()); 1 "); and run your program - Check the content of your file. Conclude - Write a new java code to append a new content to your file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
