Professional Documents
Culture Documents
ADT stack
2. import java.util.*;
3.
4. public class test {
5. public static void main(String args[]) {
6. Stack<Integer> stack=new Stack<>();
7. Scanner sc=new Scanner(System.in);
8. boolean breakLoop=false;
9. for(;;){
10. System.out.println("1.PUSH 2.POP 3.PEEK 4.DISPLAY 5.EXIT");
11. System.out.print("Enter a operation:");
12. int option=sc.nextInt();
13. switch(option){
14. case 1:
15. System.out.println("Enter a number to push:");
16. int push=sc.nextInt();
17. stack.push(push);
18. break;
19. case 2:
20. if(stack.empty()){
21. System.out.println("Stack is empty pop is not possible");
22. } else{
23. System.out.println("popped number:"+stack.pop());
24. }
25. break;
26. case 3:
27. if(stack.empty()){
28. System.out.println("Stack is empty peek is not possible");
29. } else{
30. System.out.println(stack.peek());
31. }
32. break;
33. case 4:
34. System.out.println(stack);
35. break;
36. case 5:
37. breakLoop=true;
38. break;
39. default:
40. System.out.println("Enter a correct operation");
41. }
42. if(breakLoop==true){
43. break;
44. }
45. }
46. }
47. }
2. Write a program to perform string operations using Array List. Write functions for the following
a. Append – add at end b. Insert – add at index 2 c. Search d. List all string starts with given letter
import java.util.*;
public class test {
public static void main(String args[]) {
ArrayList<String> arrList=new ArrayList<>();
Scanner sc=new Scanner(System.in);
for(;;){
boolean breakLoop=false;
System.out.println("1.APPEND 2.INSERT 3.SEARCH 4.DISPLAY 5.EXIT");
System.out.print("Enter a operation:");
int option=Integer.parseInt(sc.nextLine());
switch(option){
case 1:
System.out.print("Enter a string to append:");
String append=sc.nextLine();
arrList.add(append);
System.out.println(append+" Added Successfully");
break;
case 2:
System.out.print("Enter the position:");
int position=Integer.parseInt(sc.nextLine());
System.out.print("Enter a Element to insert:");
String str=sc.nextLine();
arrList.add(position-1,str);
break;
case 3:
if(arrList.size()==0){
System.out.println("ArrayList is empty search is not possible");
} else{
System.out.println("Enter a string to search:");
String search=sc.nextLine();
if(arrList.contains(search)){
System.out.println("Element found at index"+arrList.indexOf(search));
} else{
System.out.println("Element not found");
}
}
break;
case 4:
System.out.println(arrList);
break;
case 5:
breakLoop=true;
break;
default:
System.out.println("Incorrect operation");
}
if(breakLoop==true){
break;
}
}
}
}
15. Write 2 Java programs one implementing Arithmetic exception and the other implementing
ArrayIndexOutOfBound exception.
import java.util.*;
import java.util.*;
13. Write a java program that prints numbers from 1 to 10 line by line after every 5 seconds.
import java.util.*;
class test {
public static void main(String[] args) {
try {
for (int i = 1; i <= 10; i++) {
Thread.sleep(10000);
System.out.println(i);
}
} catch (InterruptedException e) {
System.out.println("Exception..." + e);
}
}
}
(i) Write a program to display Fibonacci series using recursion
import java.util.*;
interface classA{
void methodA();
}
public class test implements classA{
public void methodA(){
int end=10;
int firstNumber=0;
int secondNumber=1;
int nextNumber;
for(int i=0;i<end;i++){
nextNumber=firstNumber+secondNumber;
firstNumber=secondNumber;
secondNumber=nextNumber;
System.out.print(nextNumber+" ");
}
}
public static void main(String args[]){
test obj=new test();
obj.methodA();
}
}
(ii) Write a Java Program to Check a Leap Year using inheritance concept.
import java.util.*;
class B{
public static void method(){
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
if((num%400==0||num%100!=0)&&num%4==0){
System.out.print("Leap year");
} else{
System.out.print("Not an leap year");
}
}}
class test extends B{
public static void main(String[] args) {
method();
}
}
Write a Java program to implement user defined exception handling. Display the result for the
import java.util.*;
import java.util.*;
import java.io.File;