You are on page 1of 6

1.

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.*;

public class test {


public static void main(String args[]) {
try{
int num1=10,num2=0;
int result=num1/num2;
System.out.print(result);
} catch(ArithmeticException exp){
System.out.println("Arithmetic Exception Occurred");
}
}
}

import java.util.*;

public class test {


public static void main(String args[]) {
try{
int arr[]={1,2,3,4,5,6,7};
System.out.println(arr[8]);
} catch(ArrayIndexOutOfBoundsException exp){
System.out.println("ArrayIndexOutOfBound Exception Occurred");
}
}
}

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

following input a=1, b=0 perform c=a/b.

import java.util.*;

class MyException extends Exception {


public String toString() {
return ("Denominator can't be zero - MyException");
}
}

public class test {


public static void main(String args[]) {
try {
int a = 12;
int b = 0;
if (b == 0) {
throw new MyException();
} else {
System.out.print(a / b);
}
} catch (MyException ex) {
System.out.print(ex);
}
}
}

Attendance percentage >=90 – 5 Marks


Attendance percentage >=80 and < 90 – 4 Marks
Attendance percentage >=75 and < 80 – 3 Marks
Attendance percentage < 75 - 0 Marks

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String studentName;
int regNum, totalWorking, presentDays;
System.out.print("Enter name of the student:");
studentName = sc.nextLine();
System.out.print("Enter register number:");
regNum = sc.nextInt();
System.out.print("Enter Total number of working days:");
totalWorking = sc.nextInt();
System.out.print("Enter number of days present:");
presentDays = sc.nextInt();
double percentage = ((double) presentDays / totalWorking) * 100;
if (percentage >= 90) {
System.out.print("5 Marks");
} else if (percentage >= 80 && percentage < 90) {
System.out.print("4 Marks");
} else if (percentage >= 75 && percentage < 80) {
System.out.print("3 Marks");
} else if (percentage < 75) {
System.out.print("0 Marks");
}
}
Write a Java program that reads a file name from the user, displays information about whether
the file exists, whether the file is readable, or writable, the type of file and the length of the file in
bytes

import java.util.*;
import java.io.File;

public class test {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter file name:");
String file = sc.nextLine();
File obj = new File(file);
System.out.println("Name: " + obj.getName());
System.out.println("Path: " + obj.getPath());
System.out.println("Absolute Path: " + obj.getAbsolutePath());
System.out.println(obj.exists() ? "File Exist" : "File not exist");
System.out.println(obj.canRead() ? "File can be read" : "File can't be read");
System.out.println(obj.canWrite() ? "File can be write" : "File can't be write");
System.out.println("File Length: " + obj.length() + " bytes");
}
}

You might also like