0% found this document useful (0 votes)
5 views6 pages

Assignment3 Solution

The document contains multiple Java programs that demonstrate various programming concepts including exception handling, generics, recursion, and array manipulation. Each program is structured with a main method and performs specific tasks such as input validation, counting elements, calculating factorials, and reversing numbers. The code snippets illustrate practical applications of Java syntax and logic.

Uploaded by

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

Assignment3 Solution

The document contains multiple Java programs that demonstrate various programming concepts including exception handling, generics, recursion, and array manipulation. Each program is structured with a main method and performs specific tasks such as input validation, counting elements, calculating factorials, and reversing numbers. The code snippets illustrate practical applications of Java syntax and logic.

Uploaded by

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

Q1.

import java.util.*;
public class Q1
{
public static void main(String []Args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your lucky number ");
int n = sc.nextInt();
try
{
if (n<0)
throw new NumberFormatException("Lucky number cannot be -ve ");
else
System.out.println("Your lucky number is =
"+n);
}
catch (Exception e )
{
System.out.println(e);
}
sc.close();
}
}
Q2.
import java.util.*;
public class Q2
{
public static void main (String Args[])
{
Scanner sc = new Scanner (System.in);
try
{
String a[] = {"Red" , "Green" , "Blue" , null ,
"Black" };
System.out.println("Enter the colour number (0-
4)");
int x = sc.nextInt();
System.out.println("Color name length is =
"+a[x].length());

}
catch (ArrayIndexOutOfBoundsException e )
{
System.out.println(e);
}
catch(NullPointerException e)
{
System.out.println(e);
}
catch(Exception e )
{
System.out.println(e);
}
sc.close();

}
}
Q3.
import java.util.*;
class MarksOutOfBoundException extends Exception
{
MarksOutOfBoundException(String s)
{
System.out.println(s);
}
public String toString()
{
return "Invalid Mark ";
}

}
class Student
{
String name ;
double mark;
Student(String name , double mark)
{
this.name = name ;
this.mark = mark ;
}
public String toString()
{
return "Student [Name="+name+",Marks ="+mark+"]";
}
}
public class Q3
{
public static void main (String Args[])
{
Scanner sc = new Scanner(System.in);
int m = 0;
try
{
System.out.println("Input Marks ");
m = sc.nextInt();
System.out.println("Enter the name of the student
");
String n = sc.next();
if(m<0 || m>100)
{
throw new MarksOutOfBoundException("Wrong");
}
else
{
Student s = new Student (n,m);
System.out.println(s);
}
}
catch (Exception e )
{
System.out.println(e);
}
sc.close();
}

}
Q4.
public class Q4
{
public static void main(String args[])
{
Box<String> test1;
test1 = new Box<String>(null);
Box<String> var1=test1;
Box<String> var2=test1;
var1.setVar("Value 1");
System.out.println(var1.getVar());
var2.setVar("Value 2");
System.out.println(var1.getVar());
Box<Integer> test2;
test2 = new Box<Integer>(null);
Box<Integer> var3=test2;
Box<Integer> var4=test2;
var3.setVar(1);
System.out.println(var3.getVar());
var4.setVar(2);
System.out.println(var3.getVar());
Box<Object> test3;
test3 = new Box<Object>(null);
Box<Object> var5=test3;
Box<Object> var6=test3;
var5.setVar((Double)0.1);
System.out.println(var5.getVar());
var6.setVar("Value 2");
System.out.println(var5.getVar());
}
}
class Box<T>
{
public T var;
Box(T var)
{
this.var=var;
}
void setVar(T var)
{
this.var=var;
}
T getVar()
{
return var;
}
}

Q5.
import java.util.*;
public class Q5
{
public static <E>void printArray(E[] inputArray)
{
for(E var: inputArray)
{
System.out.println(var);
}
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Size of array :");
int n=in.nextInt();
Integer arr[] = new Integer[n];
for(int i=0; i<n; i++)
{
System.out.print("Enter for postion number
"+i+" :");
arr[i]=in.nextInt();
}
System.out.println("Displaying Elements of the Array ");
Q5.<Integer>printArray(arr);
}
}

Q6.
import java.util.*;
public class Q6
{
public static <T>int count(T[] array, T item)
{
int c=0;
for(T var:array)
{
c=(var.equals(item))?c+1:c;
}
return c;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n=0;
System.out.print("Enter the size of the Array :");
n=in.nextInt();
Integer arr[] = new Integer[n];
for(int i=0; i<n; i++)
{
System.out.print("Enter for position "+i+" :");
arr[i]=in.nextInt();
}
System.out.print("Enter the Element to count :");
Integer e = new Integer(in.nextInt());
int c=Q6.<Integer>count(arr,e);
System.out.println(c);
}
}

Q7.
import java.util.*;
public class Q7
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the positive number :");
int n=in.nextInt();
long f=fact(n);
System.out.println(f);
}
public static long fact(long n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}
}

Q8.
import java.util.*;
public class Q8
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("We will be Calculating a^n");
System.out.print("Enter the value of a :");
int a=in.nextInt();
System.out.print("Enter the value of n :");
int n=in.nextInt();
int p=pow(a,n);
System.out.print("Answer = "+p);
}
public static int pow(int a, int n)
{
if(n<=1)
return a;
else
return a*pow(a,n-1);
}
}

Q9.
import java.util.*;
public class Q9 {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number :");
int n=in.nextInt();
int r=rev(n);
System.out.print(r);
}
public static int rev(int n)
{
if(n<10)
return n;
else
{
int m=(int)(Math.log(n)/Math.log(10));
int r=n%10;
r=r*(int)Math.pow(10, m);
n=n/10;
return r+rev(n);
}
}
}

You might also like