Vous êtes sur la page 1sur 12

ASSIGNMENT-1I(A)

Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of
code for errors. The catch block contains the code that says what to do if exception occurs. This
problem will test your knowledge on try-catch block. You will be given two integers x and y as
input, you have to compute x/y. If x and y are not 32 bit signed integers or if y is zero, exception
will occur and you have to report it. Read sample Input/Output to know what to report in case of
exceptions

CODE:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main (String args[]) throws Exception {
Scanner sc=new Scanner(System.in);
int num1=sc.nextInt();
int num2 =sc.nextInt();
int result = 0;
try {
result = num1/num2;
System.out.println(result);
}
catch(ArithmeticException e) {
System.out.println ("Can't be divided by Zero"+e);
}
}
}
ASSIGNMENT-1I(B)
In computer science, a set is an abstract data type that can store certain values, without any particular
order, and no repeated values. {1,2,3} is an example of a set, but is not a set. Today you will learn how to
use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are
identical if a==c and b==d . That also implies is not same as (b,a) . After taking each pair as input, you
need to print number of unique pairs you currently have.

CODE:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution{
public static void main(String[] args){
Scanner s=new Scanner(System.in);
int t=s.nextInt();
String[] pair_left=new String[t];
String[] pair_right=new String[t];
for(int i=0;i<t;i++){
pair_left[i]=s.next();
pair_right[i]=s.next(); }
int cnt=0;
boolean found;
if(t>=1 && t<=100000){
for(int i=0;i<pair_left.length && i<pair_right.length;i++){
found=false;
for(int j=0;j<i;j++){
if(pair_left[i].equals(pair_left[j])){
if(pair_right[i].equals(pair_right[j])){
found=true;
break;
}
}
}
if(!found){
cnt=cnt+1;
}
System.out.println(cnt);
}
}
}
}
ASSIGNMENT-I1(C)
You are given n lines. In each line there are zero or more integers. You need to answer a few queries
where you need to tell the number located in yth position of xth line. Take your input from System.in.

CODE:
import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int numLines = Integer.parseInt(sc.nextLine());

ArrayList<ArrayList> listArray = new ArrayList<ArrayList>();

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

int numOfIntegers = sc.nextInt();

ArrayList<Integer> intArrayList = new ArrayList<Integer>();

for(int j=0;j<numOfIntegers;j++){

intArrayList.add(new Integer(sc.nextInt()));

listArray.add(intArrayList);

sc.nextLine();

int numQueries = Integer.parseInt(sc.nextLine());

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

int x = sc.nextInt()-1;

int y = sc.nextInt()-1;

sc.nextLine();
if(x<listArray.size() && y<listArray.get(x).size()){

System.out.println(listArray.get(x).get(y));

}else{

System.out.println("ERROR!");

}
ASSIGNMENT-1I(D)
In this problem , it takes an ArrayList as input. In that ArrayList there is one or more integer
numbers, then there is a special string "###", after that there are one or more other strings. A
sample ArrayList may look like this: element[0]=>42 element[1]=>10 element[2]=>"###"
element[3]=>"Hello" element[4]=>"Java"The code only prints the elements after the special
string "###". For the sample above the output will be: Hello Java

CODE:
import java.util.*;

public class Solution

static Iterator func(ArrayList mylist)

Iterator it=mylist.iterator();

while(it.hasNext())

Object element = it.next();

if(element instanceof String) //Hints: use instanceof operator

break;

return it;

public static void main(String []argh)

ArrayList mylist = new ArrayList();

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();
int m=sc.nextInt();

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

mylist.add(sc.nextInt());

mylist.add("###");

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

mylist.add(sc.next());

Iterator it=func(mylist);

while(it.hasNext())

Object element = it.next();

System.out.println((String)element);

}
ASSIGNMENT-I1(E)
Two strings p and q have been given to this problem. Both of the string’s length is 2 and both
consisting only of characters 'x', 'y' and 'z'. Possible examples of strings s and t: "xy", "zx", "yy".
You have to find a string output consisting of 3n characters, n characters should be 'x', n
characters should be 'y' and n characters should be 'z' and p and q should not occur in output as
substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "xy",
"xz" and "zz" are substrings of the string "xyxzz", but the strings "yz", "xx" and "zy" are not
substrings of the string "xyxzz". If there are multiple answers, you can print any of them.

CODE:
import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

String s1=sc.next();

String s2=sc.next();

char x=s1.charAt(0);

char y=s1.charAt(1); char z=s2.charAt(1);

if(s1.charAt(0)!==x){

System.out.println("YES"); }

else{

System.out.println("NO"); }

System.out.println(s1.charAt(0).toUpperCase()+""+y+""+x+""+z+""+z);}}
ASSIGNMENT-1I(F)
Given a string, Matching the regular expression and split the string into tokens. We define a token to be
one or more consecutive English alphabetic letters. Then, print the number of String tokens, followed by
each token on a new line. Combination of Upper case, lower case and reverse string as per output format.

CODE:
import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

Scanner read = new Scanner(System.in);

String reverse = read.nextLine();

String str="";

for(int i = reverse.length() - 1; i >= 0; i--)

str = str + reverse.charAt(i);

System.out.println(str.substring(0,3));

System.out.println(reverse.substring(5,9).toUpperCase());

System.out.print(reverse.substring(3, 4).toUpperCase());

System.out.print(str.substring(10,12));

System.out.print(reverse.substring(0,1).toLowerCase());

}
ASSIGNMENT-1I(G)
Consider the string format, Count the number of each character and digits in each string which has A
regular expression is used to describe a set of strings. For this problem the alphabet is limited to 'a' and
'b'., Digits is limited to 1 to 9 and count the number of spaces.

CODE:
import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

String s=sc.nextLine();

int i, count=0,count1=0;

for (char c : s.toCharArray()) {

if (Character.isWhitespace(c)) {

count1++;

if(Character.isDigit(c)){

count++;

System.out.println(s.length()-count-count1);

System.out.println(count);

System.out.println(count1);

}
ASSIGNMENT-1I(H)
OddNumberException class is derived from the Exception class. To implement user defined exception we
need to throw an exception object explicitly. If the value of num variable is odd, then the throw keyword
will raise the user defined exception and the catch block will get execute.

CODE:
import java.io.*;

import java.util.*;

class Solution{

public static void main(String []args) throws Exception

Scanner sc = new Scanner(System.in);

// System.out.print("Enter any number : ");

// int n = sc.nextInt();

int n = 47;

try

if(n%2!=0)

throw new Exception("Error : Odd number exception");

catch(Exception e)

System.out.println("Error : Odd number exception\n End of program Sample Output");

Vous aimerez peut-être aussi