0% found this document useful (0 votes)
7 views

JAVA LAB 8.docx

Uploaded by

Sahaj jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

JAVA LAB 8.docx

Uploaded by

Sahaj jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA LAB 8

Vibhuti purohit
(22MIM10029)

QUOTIENT: -
import java.util.Scanner;

public class Quotient{


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

//prompt the user to enter two integers


System.out.println("Enter two integers :");
int number1 = input.nextInt();
int number2 = input.nextInt();

System.out.println(number1 + "/" + number2 + "is" + (number1/number2)


);

}
}
Output: -

Quotient with method: -


import java.util.Scanner;

public class QuotientMethod{


public static int quotient(int number1, int number2){
if(number2==0){
System.out.println(" Divisor cannot be zero");
System.exit(1);
}
return number1/number2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter two integers:");


int number1 = input.nextInt();
int number2 = input.nextInt();

int result = quotient(number1, number2);


System.out.println(number1 + "/" + number2 + "is" + result);

}
}
}

Output: -

QUOTIENT WITH EXCEPTION: -


import java.util.Scanner;

public class QuotientwithException{


public static int quotient(int number1, int number2){
if(number2==0)
throw new ArithmeticException("Divisor Cnnot be zero");

return number1/number2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter two integers:");


int number1 = input.nextInt();
int number2 = input.nextInt();
try {
int result = quotient(number1 , number2);
System.out.println(number1 + " / " + number2 + " is " + result);

}
catch (ArithmeticException ex) {
System.out.println("Exception: an integer" + "Cannot be divided by
zero");

}
System.out.println("Execution continues.....");

}
Output : -

INPUT MISMATCH EXCEPTION DEMO:


import java.util.*;

public class InputMismatchExceptionDemo{


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueInput = true;

do {
try {
System.out.println("Enter an integer:");
int number = input.nextInt();

System.out.println("The number entered is : "+ number);


continueInput= false;

} catch (InputMismatchException ex) {


System.out.println("Try again. (" + "Incorrect input: an integer is
required)");
input.nextLine(); //discard input

}
} while (continueInput);
}
}

OUTPUT:

Invalid radius exception: -


public class InvalidRadiusException extends Exception{
private double radius;
public InvalidRadiusException(double radius){
super("Invalid radius" +radius );
this.radius=radius;

}
public double getRadius(){
return radius;

}
public static void main(String[] args) {
System.out.println("The radius is 3.14 approx: ");
}
}

OUTPUT:

You might also like