0% found this document useful (0 votes)
34 views8 pages

Big Number and Exception Handling

The document provides sample code and instructions for a pre-placement training assignment on Java that involves big number handling and exception handling. It includes 3 levels of questions - easy, medium, and hard - across different concepts like try-catch blocks, primality testing, exceptions, and sorting big integers. Students are asked to attempt specific number of questions from each level. The code snippets demonstrate how to work with exceptions, validate input, compare and sort big integers.

Uploaded by

s1062230092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

Big Number and Exception Handling

The document provides sample code and instructions for a pre-placement training assignment on Java that involves big number handling and exception handling. It includes 3 levels of questions - easy, medium, and hard - across different concepts like try-catch blocks, primality testing, exceptions, and sorting big integers. Students are asked to attempt specific number of questions from each level. The code snippets demonstrate how to work with exceptions, validate input, compare and sort big integers.

Uploaded by

s1062230092
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Pre-Placement Training - Java

Assignment 3 - Big Number and Exception Handling


● Easy Level questions (Attempt any 2) -
1. Exception Handling (Try-catch) [ YT Link ]

import java.io.*;

import java.math.*;

import java.text.*;

import java.util.*;

import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

/* Enter your code here. Read input from STDIN. Print


output to STDOUT. Your class should be named Solution. */

Scanner sc = new Scanner(System.in);

try {

System.out.println(sc.nextInt()/sc.nextInt());

} catch (ArithmeticException e) {

System.out.println("java.lang.ArithmeticException: / by
zero");
}catch(Exception e){

System.out.println("java.util.InputMismatchException");

sc.close();

● Medium Level Questions (Attempt any 2) -


○ Java Primality Test

import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) throws


IOException {

try(BufferedReader bufferedReader = new


BufferedReader(new InputStreamReader(System.in))){

BigInteger number = new


BigInteger(bufferedReader.readLine());

if(number.isProbablePrime(1)){

System.out.println("prime");

}else{

System.out.println("not prime");

}catch(IOException e){

System.out.println("Could not validate


input");

}
○ Exception Handling 2

import java.util.Scanner;

class MyCalculator {

/*

* Create the method long power(int, int) here.

*/

public long power(int n, int p) throws Exception{

if(n == 0 && p == 0) throw new Exception("n and p


should not be zero.");

if(n < 0 || p < 0) throw new Exception("n or p


should not be negative.");

return (long)Math.pow(n,p);

}
}

public class Solution {

public static final MyCalculator my_calculator = new


MyCalculator();

public static final Scanner in = new


Scanner(System.in);

public static void main(String[] args) {

while (in .hasNextInt()) {

int n = in .nextInt();

int p = in .nextInt();

try {

System.out.println(my_calculator.power(n,
p));

} catch (Exception e) {

System.out.println(e);

}
● Hard Level Questions (Attempt any 1) -
○ BigInteger [ YT Link ]

import java.math.BigDecimal;

import java.util.*;

class Solution{

public static void main(String []args){

//Input

Scanner sc= new Scanner(System.in);

int n=sc.nextInt();

String []s=new String[n+2];

for(int i=0;i<n;i++){

s[i]=sc.next();
}

sc.close();

for(int i = 0;i<n;i++){

BigDecimal max = new BigDecimal(s[i]);

int idx = i;

for(int j = i+1;j<n;j++)

BigDecimal curr = new BigDecimal(s[j]);

if(curr.compareTo(max) == 1){

max=curr;

idx=j;

String temp = s[i];

s[i] = s[idx];

s[idx] = temp;

//Output

for(int i=0;i<n;i++)

System.out.println(s[i]);
}

You might also like