Q1. Write a program to find maximum and minimum of 3 integers using nested if-else. Read numbers as a command line argument.
Q2. Write a program that prints the prime no between 0 and 100.
Q3. Write a program to determine grade for given marks Marks Grade 80-100 Distinction 60-79 1st Class 40-59 2nd Class Less than 40 FAIL
Q4. Write a program that takes 'n' numbers from user as a command line arguments and prints the maximum and minimum of these numbers using 'for' loops.
Q5. Write a program to check entered no. is Prime or not
Q6. Write a program to split 4 digit no.
Q7. Write a program to for Fibonacci series. (Recursive)
Q8. Write a program to compute sum of the digits of a given integer number
Q9. Write a program to create a menu driven arithmetic calculator. (Use switch-case)
Q10. Write a program to display following patterns:
a) * * * * * * * * * * * * * * *
b) $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
c) * * * *
* * *
*
d) A A B A B C A B C D A B C D E
e) A B C D C B A A B C B A A B A A
f) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Q11. Declare a class called Circle with following members. Variables: radius - integer Constructors: 1)Circle() : assign 1 to radius. 2)Circle(int r) : assign the value of r to radius of calling object. Methods: 1) setData(int r) : assign the value of r to radius of calling object. 2) getArea() : calculate and return the area of circle object. 3)print() : print the radius and area of calling object. Write a driver class to test Circle class.
Q12. Construct a class Bank Account with Rs. 500 as the initial balance amount. The class has two methods: deposit(double amount), withdrawal(double amount). Write a program that asks the user for the amount to be deposited and also the amount to be withdrawn and performs the both using the methods of the class BankAccount. There should be a check that the initial amount is not less than Rs. 500.
Q13. Declare a class called Rectangle with following members: Variables: length and breadth - integer Methods: 1) setData(int length, int breadth) : assign a length to length and breadth to breadth of the object 2) setData(int a) : assign a to base and height 3) getArea() : Calculate and return the area of Rectangle object 4) print() : print the length, breadth and area of calling object Write a driver class to test Rectangle class.
Q14. Declare a class called Triangle with following members: Variables: base and height - integer Methods: 1) setData(int base, int height) : assign base to base and height to height of the object 2) setData(int a) : assign a to base and height 3) getArea() : Calculate and return the area of Triangle object 4) print() : print the base, height and area of calling object Write a driver class to test Triangle class.
Q15. Write a program using class Complex to calculate addition of two complex numbers.
Q16. Write a program that finds the average of the numbers stored in a 1-Dimensional array
Q17. Write a program that sorts the elements stored in a one dimensional array
Q18. Write a program to perform matrix addition.
Q19. Write a program to perform matrix multiplication
Q20. Write a program to find maximum and minimum element of the array
Q21. Given a list of marks ranging from 0 to 100. Write a program to compute and print the number of students who have obtained mark a) In the range 81 to 100 b) In the range 61 to 80 c) In the range 41 to 60 d) In the range 0 to 40
Q22. Write a program that displays the marks of 5 subjects of 3 students and the average of marks for each subject
Q23. Write a program to find number of Uppercase, lowercase, blank spaces and digit and special symbols in the entered String
Q24. Write a program to check whether the entered String is palindrome or not
Q25. Write a program to check whether entered Email Id is valid or not
Q26. Write a program that will sort a list of names of 5 cricketers in alphabetical order
Q27. Write a program to capitalize the first character of each word from the given sentence.(For example: watumull institute of engineering ==> Watumull Institute Of Engineering)
Q28. Write a program to perform String reverse operation.
Q29. Write a program to accepts 3 digit number and print it out digit by digit as a series of word. Example: 435 would be printed as "Four Three Five"
Q30. Write a program to count the frequency of a given letter in a String
Q31. Write a program to accept any String up to 15 characters. Display the elements of String with their element nos. use toCharArray() function
Q32. Write a program to declare 2 integers (a & b). Take the input from the user. Declare a float variable c where c=a+b. Handle Exception properly.
Q33. Write a program to declare an array of size specified by the user. Prompt the user to enter array elements and print the same. Handle likely Exceptions.
Q34. Write a program to read a character from the user. If the entered character is a,e,i,o,u then raise the user defined Exception called "VowelException" If the entered character is x or X then raise a user defined Exception called "ExitException" Else print the character 5 times.
Q35. Define an Exception called "NotMatchingException" that is thrown when the string entered by the user as command line arguments is not equal to "India".
Q36. Write an abstract class called Shape. Shape has a constant field called P1. It has 2 abstract methods: area and perimeter. This class has 2 non-abstract sub-classes called Circle and Rectangle. Circle has one additional attribute called radius Rectangle class has 2 additional attributes called width and height Include a client class to test the above classes
Q37. Write a program to declare a vector with initial capacity 5 and increment of 3 a. Add 3 string objects b. Print the content of vector c. Print size and capacity d. Add 4 integer objects in the vector e. Remove the element object present at position2 f. Check whether the vector is empty or not i. If vector is empty, the print a message "empty vector" ii. Else print the capacity, size and vector elements.
Q38. Write a program to find factorial of a number using Recursion
Q39. Write a program that accepts a shopping list of five items from the command line and stores in a vector ad perform the following operations i. Add an item in the list ii. Delete an item in the list iii. Prints the shopping list
Q40. Write a program to create 2 threads. 1st calculating sum of first 5 numbers. 2nd calculating factorial of first five numbers. (By extending Thread class)
Q41. Write a program to create 2 threads. 1st calculating sum of first 5 numbers. 2nd calculating factorial of first five numbers. (Using Runnable interface)
Q42. Write a program to demonstrate following methods of multithreading programs i. sleep() ii. setName() iii. getName() iv. setPriority() v. getPriority()
Q43. Implement the following hierarchy
Saff Id : int Name : String Display() / \ / \ / \ Teacher Typist Subject : String Speed : int Display() Display() | | | Contractual Pay : float Display()
Q44. Write an applet to print "initialization" when applet is initializing. Print "Start" when applet is starting. Print "Stop" when applet is stopped. Print "Destroy" when applet is destroyed. (Applet Life Cycle)
Q45. Write an applet to draw Joker face.
please early as possible
1)
import java.util.*; class LargerNumber{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("Enter Number1: "); int x=input.nextInt(); System.out.print("Enter Number2: "); int y=input.nextInt(); System.out.print("Enter Number3: "); int z=input.nextInt(); if ((x>y)&&(x>z)){ System.out.println(x+" is larger"); } else if ((y>x)&&(y>z)){ System.out.println(y+" is larger"); } else if ((z>y)&&(z>x)){ System.out.println(z+" is larger"); } } }
2)
class PrimeNumbers{ public static void main (String args[]){ String primeNo = ""; int j=0; System.out.println("Prime nums are:" ); for (int i = 0; i < 100; i++){ for ( j = 2; j < i; j++){ if (i % j == 0) { break; } } if (i == j) { primeNo += i + " "; } } System.out.println(primeNo); } }
3)
import java.util.*; class IfElse{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("Enter Marks: "); int marks=input.nextInt(); if(marks>=80&&marks<=100){ System.out.println("Distinction"); } else if(marks>=60&&marks<=79){ System.out.println("1st Class"); } else if(marks>=40&&marks<=59){ System.out.println("2nd Class"); } else{ System.out.println("Fail"); } } }
4)
import java.util.*; class FindLargestAndSmallest{ public static void main(String[] args){ int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; Scanner input=new Scanner(System.in); System.out.println("How many numbers you want to input? :"); int n=input.nextInt(); System.out.println("Enter "+n+" numbers: "); for(int i=1;i<=n;i++){ int num=input.nextInt(); if (num < min) { min = num; } if (num > max) { max = num; } } System.out.println("Smallest Number is: "+min); System.out.println("Largest Number is: "+max); } }
5)
import java.util.*; class CheckPrimeNumber { static boolean isPrime(int number){ boolean isPrime=false; int i=(int) Math.ceil(Math.sqrt(number)); while (i>1){ if((number!=i) && (number%i==0)){ isPrime=false; break; } else if (!isPrime) isPrime=true; --i; } return isPrime; } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter Number:"); int num=input.nextInt(); if(isPrime(num)){ System.out.println("Number is Prime!"); } else{ System.out.println("Number is not prime!"); } } }
6)
import java.util.*; class Split4DigitNumber { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.println("Enter 4 digit number: "); int x = input.nextInt(); int y, z; y = x /1000 ; System.out.println("The digit in the Thousand's place = "+y); z = x % 1000; y = z /100; System.out.println("The digit in the Hundred's place = "+y); z = z % 100; y = z / 10; System.out.println("The digit in the Ten's place = "+y); y = z % 10; System.out.println("The digit in the Unit's place = "+y); } }
7)
import java.util.*; public class Fibonacci{ public static int fibonacciSeries(int n) { if (n == 1) return 1; else if (n == 2) return 1; else return fibonacciSeries(n-1) + fibonacciSeries(n-2); } public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); System.out.print("Enter value of n: "); int num = input.nextInt(); for(int i=1;i<=num;i++){ System.out.println(fibonacciSeries(i)); } } }
8)
import java.util.*; class SumOfDigits { public static void main(String args[]) { int sum = 0; System.out.println("Enter multi digit number:"); Scanner input = new Scanner(System.in); int n = input.nextInt(); int t = n; while (n > 0) { int p = n % 10; sum = sum + p; n = n / 10; } System.out.println("sum of the digits in " + t + " is " + sum); } }
9)
import java.util.*; class Calculator { public static void main(String[] args) { boolean exit=false; Scanner input=new Scanner(System.in); System.out.print("Enter number1: "); double num1=input.nextDouble(); System.out.print("Enter number2: "); double num2=input.nextDouble(); System.out.println("1 Addition"); System.out.println("2 Subtraction"); System.out.println("3 Multiplication"); System.out.println("4 Division"); System.out.println("5 Exit"); do{ System.out.print("Enter your choice: "); int choice=input.nextInt(); switch(choice){ case 1: double add=num1+num2; System.out.println("Addition="+add); break; case 2: double sub=num1-num2; System.out.println("Subtraction="+sub); break; case 3: double mul=num1*num2; System.out.println("Multiplication="+mul); break; case 4: double div=num1/num2; System.out.println("Division="+div); break; case 5: exit=true; break; } } while(!exit); } }
10 a)
public class PatternExample { public static void main(String[] args) { int y = 1; for(int i=5; i>= 1 ;i--){ for(int j=1; j <= i ; j++){ System.out.print(" *"); y = y + 1; } System.out.println(""); } } }
10 b)
public class PatternExample { public static void main(String[] args) { int y = 1; for(int i=1; i<= 5 ;i++){ for(int j=1; j <= i ; j++){ System.out.print(" $"); y = y + 1; } System.out.println(""); } } }
10 c)
public class PatternExample { public static void main(String[] args) { int n = 2; for (int i = -n; i <= n; i++) { for (int j = -n; j <= n; j++) { if (Math.abs(i) + Math.abs(j) <= n){ System.out.print("* "); } else{ System.out.print(" "); } } System.out.println(); } } }
10 d)
public class PatternExample{ public static void main(String[] args){ for(char i='A';i<='E';i++){ for(char j='A';j<=i;j++){ System.out.print(j); } System.out.println(); } } }
11)
class Circle{ int radius; static double pi=3.14; Circle(){ this.radius=1; } Circle(int radius){ this.radius=radius; } public void setData(int r) { radius = r; } public double getArea(){ double area=pi*radius*radius; return area; } public void print(){ double area=getArea(); System.out.println("Radius is: "+radius+" , Area is: "+area); } } public class TestCircle{ public static void main(String[] args){ Circle c1=new Circle(); c1.print(); c1.setData(5); c1.print(); Circle c2=new Circle(4); c2.print(); c2.setData(2); c2.print(); } }
12)
import java.util.*; class BankAccount{ double bal; BankAccount(int bal) { this.bal = bal; } double deposit(double amt) { if (amt < 0) { System.out.println("Invalid Amount"); return 1; } bal = bal + amt; return 0; } double withdraw(double amt) { if (bal < amt) { System.out.println("Not sufficient balance."); return 1; } if (amt < 0) { System.out.println("Invalid Amount"); return 1; } bal = bal - amt; return 0; } void check() { System.out.println("Your Balance is: "+bal); } public static void main(String[]args){ Scanner input=new Scanner(System.in); BankAccount Cust=new BankAccount(500); System.out.print("Enter the amount to deposit:"); double dep=input.nextDouble(); double bal1=Cust.deposit(dep); Cust.check(); System.out.print("Enter the amount to withdraw:"); double with=input.nextDouble(); double bal2=Cust.withdraw(with); Cust.check(); } }
13)
class Rectangle{ int length; int breadth; public void setData(int length,int breadth) { this.length = length; this.breadth=breadth; } public void setData(int a) { length = a; breadth=a; } public int getArea(){ int area=length*breadth; return area; } public void print(){ System.out.println("Length of triangle is: "+length+" ,Breadth is: "+breadth+", Area of rectangle is: "+getArea()); } } public class TestRectangle{ public static void main(String[]args){ Rectangle rect=new Rectangle(); rect.setData(2,3); rect.print(); rect.setData(5); rect.print(); } }
14)
class Triangle { int base; int height; public void setData(int base,int height) { this.base = base; this.height=height; } public void setData(int a) { base = a; height=a; } public int getArea(){ int area=base*height; return area/2; } public void print(){ System.out.println("Base of triangle is: "+base+" ,Height is: "+height+", Area of triangle is: "+getArea()); } } public class TestTriangle { public static void main(String[] args) { Triangle tri=new Triangle(); tri.setData(2,3); tri.print(); tri.setData(4); tri.print(); } }
15)
public class Complex { private int a; private int b; public Complex () { } public Complex (int a, int b) { this.a = a; this.b = b; } public String getComplexValue() { if (this.b < 0) { return a + "" + b + "i"; } else { return a + "+" + b + "i"; } } public static String addition(Complex num1, Complex num2) { int a1 = num1.a + num2.a; int b1 = num1.b + num2.b; if (b1 < 0) { return a1 + "" + b1 + "i"; } else { return a1 + "+" + b1 + "i"; } } public static void main(String args[]) { Complex com1 = new Complex (-2, -3); Complex com2 = new Complex (-4, -5); System.out.println(com1.getComplexValue()); System.out.println(com2.getComplexValue()); System.out.println("Addition of Complex Numbers are :" + Complex. addition(com1, com2)); } }
16)
import java.util.*; class ArraySumAndAverage{ public static void main(String[] args){ Scanner input=new Scanner(System.in); int num[]=new int[10]; int sum=0; System.out.println("Enter Array Elements: "); for(int i=0;i<num.length;i++){ num[i]=input.nextInt(); sum+=num[i]; } int average=sum/num.length; System.out.println("Average of elements: "+average); } }
17)
import java.util.*; class ArraySort{ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Enter 5 numbers: "); Integer array[]=new Integer[5]; for(int i=0;i<array.length;i++){ array[i]=input.nextInt(); } System.out.println("Sorted Array:"); Arrays.sort(array); for(int i=0;i<array.length;i++){ System.out.println(array[i]); } } }
18)
import java.util.*; class MatrixAddition{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Addition Of Matrices: "); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ C[i][j]=A[i][j]+B[i][j]; System.out.print(C[i][j]+" "); } System.out.println(); } } }
19) import java.util.*; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Result is: "); System.out.println(); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
19)
import java.util.*; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println(); for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Result is: "); System.out.println(); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }
20)
import java.util.*; class ArrayExample{ public static int getMaxValue(int[] arr){ int maxValue = arr[0]; for(int i=1;i < arr.length;i++){ if(arr[i] > maxValue){ maxValue = arr[i]; } } return maxValue; } public static int getMinValue(int[] arr){ int minValue = arr[0]; for(int i=1;i<arr.length;i++){ if(arr[i] < minValue){ minValue = arr[i]; } } return minValue; } public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("Enter Array Elements: "); int array[]=new int[5]; for(int i=0;i<array.length;i++){ int num=input.nextInt(); array[i]=num; } System.out.println("Smallest No: "+getMinValue(array)); System.out.println("Largest No: "+getMaxValue(array)); } }
21)
import java.util.*; class Marks { public static void main(String[] args) { int a=0,b=0,c=0,d=0; Scanner input=new Scanner(System.in); for(int i=1;i<=10;i++){ int marks=input.nextInt(); if(marks>80) a++; else if(marks>60) b++; else if(marks>40) c++; else if(marks<=40) d++; } System.out.println("Number of students who have obtained more than 80 marks: "+a); System.out.println("Number of students who have obtained more than 60 marks and less than equal to 80 marks: "+b); System.out.println("Number of students who have obtained more than 40 marks and less than equal to 60 marks: "+c); System.out.println("Number of students who have obtained less than 40 marks: "+d); } }
23)
import java.util.*; class FindFromString { public static void main(String[] args) { int upp=0,low=0,space=0,digit=0,special=0; Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.nextLine(); char ch[]=st.toCharArray(); for(int i=0;i<ch.length;i++){ if(Character.isUpperCase(ch[i])){ upp++; } else if(Character.isLowerCase(ch[i])){ low++; } else if(Character.isWhitespace(ch[i])){ space++; } else if(Character.isDigit(ch[i])){ digit++; } else{ special++; } } System.out.println("No of uppercase characters: "+upp); System.out.println("No of lowercase characters: "+low); System.out.println("No of space characters: "+space); System.out.println("No of digit characters: "+digit); System.out.println("No of special characters: "+special); } }
24)
import java.util.*; public class CheckPalindrome{ public static void main(String[]args){ Scanner input=new Scanner(System.in); System.out.print("Enter String: "); String st=input.next(); String str[]=st.split(""); String reversedSt=""; for(int i=str.length-1;i>=0;i--){ reversedSt+=str[i]; } if(st.equalsIgnoreCase(reversedSt)){ System.out.println("String is palindrome"); } else{ System.out.println("String is not palindrome"); } } }
25)
import java.util.*; import java.util.regex.*; class CheckEmailId{ public static boolean isEmailValid(String email){ boolean isValid = false; String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"; CharSequence inputStr = email; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if(matcher.matches()){ isValid = true; } return isValid; } public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("Enter Email ID: "); String email=input.nextLine(); if(isEmailValid(email)){ System.out.println("Valid!"); } else{ System.out.println("Invalid Email-id!"); } } }
26)
import java.util.*; class SortList { public static void main(String[] args) { Scanner input=new Scanner(System.in); ArrayList<String> list=new ArrayList<String>(); System.out.println("Enter names of 5 Cricketers: "); for(int i=0;i<5;i++){ String name=input.nextLine(); list.add(name); } System.out.println("Names of 5 Cricketers in sorted order: "); Collections.sort(list); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).toString()); } } }
27)
import java.util.*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str, " ", true ); final StringBuilder sb = new StringBuilder(); while ( st.hasMoreTokens() ) { String token = st.nextToken(); Character ch=Character.valueOf(token.charAt(0)); token = String.format( "%s%s",Character.toUpperCase(token.charAt(0)),token.substring(1) ); sb.append( token ); } return sb.toString(); } public static void main(String[]args) throws Exception{ System.out.print("Enter the sentence : "); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); String inputString = br.readLine(); FirstLetter letter=new FirstLetter(); String words=letter.capitalizeFirstLetter(inputString); System.out.println(words); } }
28)
import java.util.*; public class ReverseString{ public static String reverse(String st){ String rev = new StringBuffer(st).reverse().toString(); return rev; } public static void main(String[]args){ Scanner input=new Scanner(System.in); System.out.println("Enter string: "); String str=input.nextLine(); System.out.println("Reverse: " + reverse(str)); } }
30)
import java.util.*; class CountFrequency { public static void main(String[] args) { int count=0; Scanner input=new Scanner(System.in); System.out.println("Enter string: "); String str=input.nextLine(); System.out.println("Enter letter to count its frequency: "); char chr=input.next().charAt(0); char ch[]=str.toCharArray(); for(int i=0;i<ch.length;i++){ if(ch[i]==chr){ count++; } } System.out.println("Frequency of "+chr+": "+count); } }
31)
import java.util.*; class StringElementNo { public static void main(String[] args) { int count=0; Scanner input=new Scanner(System.in); System.out.println("Enter string: "); String str=input.nextLine(); char ch[]=str.toCharArray(); for(int i=0;i<ch.length;i++){ System.out.println("character "+ch[i]+" is at position: "+i); } } }
32)
import java.util.*; class InputNumbersToFindAdd { public static void main(String[] args) { try{ float c=0; Scanner input=new Scanner(System.in); System.out.print("Enter number 1: "); int a=input.nextInt(); System.out.print("Enter number 2: "); int b=input.nextInt(); c=a+b; System.out.println(c); } catch(Exception e){ System.out.println(e); } } }
33)
import java.util.*; class ArrayExample { public static void main(String[] args) { try{ Scanner input=new Scanner(System.in); System.out.print("How many numbers you want to ass in your array: "); int size=input.nextInt(); int array[]=new int[size]; System.out.println(); System.out.println("Enter Array Elements: "); for(int i=0;i<array.length;i++){ array[i]=input.nextInt(); } System.out.println("Your Array Elements are: "); for(int i=0;i<array.length;i++){ System.out.println(array[i]); } } catch(Exception e){ System.out.println(e); } } }
34)
class ReadChar { public static void main(String[] args) throws Exception { System.out.print("Enter character: "); int i=System.in.read(); char ch=(char)i; System.out.println(); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){ throw new Exception("Vowel Exception"); } else if(ch=='X'||ch=='x'){ throw new Exception("Exit Exception"); } else{ for(int j=1;j<=5;j++){ System.out.println(ch); } } } }
35)
import java.util.*; class NoMatchingException { public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); System.out.print("Enter string: "); String st=input.nextLine(); if(!st.equals("India")){ throw new Exception("Not Matching Exception"); } } }
36)
abstract class Shape{ double PI=3.14; abstract public double area(); abstract public double perimeter(); } class Rectangle extends Shape{ int l, b; Rectangle(int l,int b){ this.l=l; this.b=b; } public double area(){ return l*b; } public double perimeter(){ return (2*l)+(2*b); } } class Circle extends Shape{ int r; Circle(int r){ this.r=r; } public double area(){ return PI*r*r; } public double perimeter(){ return 2*PI*r; } } public class AbstractClassExample{ public static void main(String args[]) { Rectangle rect=new Rectangle(2,5); System.out.println("Area of Rectangle: "+(rect.area())); System.out.println("Perimeter of Rectangle: "+(rect.perimeter())); Circle cir=new Circle(5); System.out.println("Area of Circle: "+(cir.area())); System.out.println("Perimeter of Circle: "+(cir.perimeter())); } }
37)
38)
import java.util.*; class Factorial{ public static int findFactorial(int n) { if (n == 1) { return n; } else { return n * findFactorial(n - 1); } } public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter number: "); int num=input.nextInt(); System.out.println("The factorial of " +num+" is : " + findFactorial(num)); } }
Ads