1.Find Maximum and Minium number in the given array.
public class ArrayMinMax {
public static void main(String[] args) {
int numbers[] = { 7, 4, 9, 1, 3, 8, 6 };
int max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (max < numbers[i]) {
max = numbers[i];
}
}
System.out.println("max number is " + max);
int min = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (min > numbers[i]) {
min = numbers[i];
}
}
System.out.println("min number is " + min);
}
}
public class ArrayMinMax {
public static void main(String[] args) {
int numbers[] = { 7, 4, 9, 1, 3, 8, 6 };
int max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (max < numbers[i]) {
max = numbers[i];
}
}
System.out.println("max number is " + max);
int min = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (min > numbers[i]) {
min = numbers[i];
}
}
System.out.println("min number is " + min);
}
}
Output:
max number is 9
min number is 1
2.Sorting the numbers using bubble sort algorithm
public class BubbleSort {
 public static void main(String[] args) {
  int numbers[] = { 7, 4, 9, 1, 3, 8, 6 };
  for (int i = 0; i < numbers.length; i++) {
   for (int j = 1; j < (numbers.length - i); j++) {
    if (numbers[j - 1] > numbers[j]) {
     int temp = numbers[j];
     numbers[j] = numbers[j - 1];
     numbers[j - 1] = temp;
    }
   }
  }
  for (int i = 0; i < numbers.length; i++)
   System.out.print(numbers[i] + " ");
 }
}
Output:
1 3 4 6 7 8 9 
3.Find factorial of a given number
public class Factorial {
 public static void main(String args[]) {
  int n = 6;
  int fact = 1;
  for (int i = 2; i <= n; i++) {
   fact = fact * i;
  }
  System.out.println("The factorial value is " + fact);
 }
}
Output:
The factorial value is 720
4.Find Fibonacci series upto given count.
public class Fibonacci {
 public static void main(String args[]) {
  int f0 = 0, f1 = 1, f2 = 1;
  int count = 10;
  int i = 0;
  System.out.print(f0 + " " + f1 + " " + f2);
  while (i <= count) {
   f0 = f1;
   f1 = f2;
   f2 = f0 + f1;
   System.out.print(" " + f2);
   i++;
  }
 }
}
Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233
5.Swap two numbers without using 3rd variable
public class Swap {
 public static void main(String args[]) {
  int a = 30;
  int b = 40;
  a = a + b;
  b = a - b;
  a = a - b;
  System.out.println(a + "  " + b);
 }
}
Output:
40  30
6.TypeCasting program
public class TypeCasting {
 public static void main(String[] args) {
  int a = 10;
  // A data type of lower size is assigned to a data type of higher size
  double b = a; // Implicit type casting.
  System.out.println(b); // 10.0
  // byte –> short –> int –> long –> float –> double
  double c = 10.2;
  int d = (int) c; // Explicit type casting
  System.out.println(d); // 10
  long e = 20;
  float f = e; // Implicit type casting.
  System.out.println(f);
  boolean m = true;
  // int n = (int) m; // boolean is incompatible for conversion
  boolean o = m;
  System.out.println(o); // true
  float x = 10.2f;
  if (x == 10.2) {
   System.out.println("same");
  }
  if (x == 10.2f) {
   System.out.println("now float same");
  }
  double y = 10.2;
  if (y == 10.2) {
   System.out.println("double same");
  }
 }
}
Output:
10.0
10
20.0
true
now float same
double same
7.Read text file
package javaprograms;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadFile {
 
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("E:\\Selenium\\testFile.txt");
BufferedReader buffered = new BufferedReader(file);
String str;
while((str=buffered.readLine())!=null) {
System.out.println(str);
}
buffered.close();
}
}
7.Read text file
package javaprograms;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadFile {
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("E:\\Selenium\\testFile.txt");
BufferedReader buffered = new BufferedReader(file);
String str;
while((str=buffered.readLine())!=null) {
System.out.println(str);
}
buffered.close();
}
}
Output:
janardhan
bhaskar
8.Reverse Char Array :
public class ReverseString {
public static void main(String[] args) {
char[] var = {'h','e','l','l','o'};
char ch;
for(int i=var.length-1,j=0;j<var.length/2;i--,j++) {
ch=var[i];
var[i] = var[j];
var[j]= ch;
}
  
for(char c : var) {
System.out.print(c);
}
System.out.println();
  
for(int k=0;k<var.length;k++) {
System.out.print(var[k]);
}
}
}
a.
public class TryCatch {
public void tryCatchArithmeticWrongException() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.println("this will not execute");
}
 
public void tryCatchAnyType() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(Exception e) {
System.out.println(e);
}
System.out.println("this will execute");
}
 
public void tryCatchArithmetic() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.println("this will execute");
}
 
public static void main(String[] args) {
TryCatch obj = new TryCatch();
// obj.tryCatchArithmetic();
// obj.tryCatchAnyType(); // Then this also will not execute
obj.tryCatchAnyType();
obj.tryCatchArithmetic();
obj.tryCatchArithmeticWrongException();
  
}
}
  
  
  
   
   
  
  
  
  
  
  
10.Difference between List and Set :
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListSet {
 
public static void main(String[] args) {
  
System.out.println("List Example");
// List<String> list = new List<String> // List is an interface.
List<String> list = new ArrayList<String>();
list.add("gitam");
list.add("college");
list.add("of");
list.add("engineering");
for(String value : list) {
System.out.print(value+" ");
}
  
System.out.println("\nSet Example");
Set<String> set = new HashSet<String>();
set.add("gitam");
set.add("college");
set.add("of");
set.add("engineering");
for(String value : set) {
System.out.print(value+" ");
}
  
System.out.println("List Allows duplicates Example");
// List<String> list = new List<String> // List is an interface.
List<String> duplicates = new ArrayList<String>();
duplicates.add("10");
duplicates.add("20");
duplicates.add("30");
duplicates.add("10");
for(String value : duplicates) {
System.out.print(value+" ");
}
  
System.out.println("The size of the list now is 3 or 4 : " + duplicates.size());
  
System.out.println("\nSet Does not allow duplicates Example");
Set<String> noduplicates = new HashSet<String>();
noduplicates.add("10");
noduplicates.add("20");
noduplicates.add("30");
noduplicates.add("10");
for(String value : noduplicates) {
System.out.print(value+" ");
}
  
System.out.println("The size of the set now is 3 or 4 : " + noduplicates.size());
}
}
Output:
List Example
gitam college of engineering
Set Example
of gitam college engineering List Allows duplicates Example
10 20 30 10 The size of the list now is 3 or 4 : 4
Set Does not allow duplicates Example
20 10 30 The size of the set now is 3 or 4 : 3
8.Reverse Char Array :
public class ReverseString {
public static void main(String[] args) {
char[] var = {'h','e','l','l','o'};
char ch;
for(int i=var.length-1,j=0;j<var.length/2;i--,j++) {
ch=var[i];
var[i] = var[j];
var[j]= ch;
}
for(char c : var) {
System.out.print(c);
}
System.out.println();
for(int k=0;k<var.length;k++) {
System.out.print(var[k]);
}
}
}
Output:
olleh
olleh
9.Try,Catch/Finally :
public class TryCatch {
public void tryCatchArithmeticWrongException() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.println("this will not execute");
}
public void tryCatchAnyType() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(Exception e) {
System.out.println(e);
}
System.out.println("this will execute");
}
public void tryCatchArithmetic() {
try {
int a = 5/0;
System.out.println("This should require either catch/finally");
}
catch(ArithmeticException e) {
System.out.println(e);
}
System.out.println("this will execute");
}
public static void main(String[] args) {
TryCatch obj = new TryCatch();
// obj.tryCatchArithmetic();
// obj.tryCatchAnyType(); // Then this also will not execute
obj.tryCatchAnyType();
obj.tryCatchArithmetic();
obj.tryCatchArithmeticWrongException();
}
}
Output:
java.lang.ArithmeticException: / by zero
this will execute
java.lang.ArithmeticException: / by zero
this will execute
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at TryCatch.tryCatchArithmeticWrongException(TryCatch.java:5)
 at TryCatch.main(TryCatch.java:42)
b.Try/Catch/Finally return types :
public class TryCatchFinally {
 public static int divideone(int a) {
  try {
   int b=8/a;
   System.out.println("this is try block "+b);
   return 10;
  }
  catch(Exception e) {
   System.out.println("this is catch block");
   return 20;
  }
  finally {
   System.out.println("this is in finally block");
      return 30;
  }
 }
  public static int dividetwo(int a) {
   try {
    int b=8/a;
    System.out.println("this is try block "+b);
    return 10;
   }
   catch(Exception e) {
    System.out.println("this is catch block");
    return 20;
   }
   finally {
    System.out.println("this is in finally block");
      // return 30;
   }
  // return a;  // gives error, since we already mentioned return in try block.
 }
 public static void main(String[] args) {
  int value1 = TryCatchFinally.divideone(5);
  System.out.println(value1);
  TryCatchFinally.divideone(0);
  System.out.println(value1);
  int value2 = TryCatchFinally.dividetwo(5);
  System.out.println(value2);
  TryCatchFinally.dividetwo(0);
  System.out.println(value2);
 }
}
Output:
this is try block 1
this is in finally block
30
this is catch block
this is in finally block
30
this is try block 1
this is in finally block
10
this is catch block
this is in finally block
10
10.Difference between List and Set :
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListSet {
public static void main(String[] args) {
System.out.println("List Example");
// List<String> list = new List<String> // List is an interface.
List<String> list = new ArrayList<String>();
list.add("gitam");
list.add("college");
list.add("of");
list.add("engineering");
for(String value : list) {
System.out.print(value+" ");
}
System.out.println("\nSet Example");
Set<String> set = new HashSet<String>();
set.add("gitam");
set.add("college");
set.add("of");
set.add("engineering");
for(String value : set) {
System.out.print(value+" ");
}
System.out.println("List Allows duplicates Example");
// List<String> list = new List<String> // List is an interface.
List<String> duplicates = new ArrayList<String>();
duplicates.add("10");
duplicates.add("20");
duplicates.add("30");
duplicates.add("10");
for(String value : duplicates) {
System.out.print(value+" ");
}
System.out.println("The size of the list now is 3 or 4 : " + duplicates.size());
System.out.println("\nSet Does not allow duplicates Example");
Set<String> noduplicates = new HashSet<String>();
noduplicates.add("10");
noduplicates.add("20");
noduplicates.add("30");
noduplicates.add("10");
for(String value : noduplicates) {
System.out.print(value+" ");
}
System.out.println("The size of the set now is 3 or 4 : " + noduplicates.size());
}
}
Output:
List Example
gitam college of engineering
Set Example
of gitam college engineering List Allows duplicates Example
10 20 30 10 The size of the list now is 3 or 4 : 4
Set Does not allow duplicates Example
20 10 30 The size of the set now is 3 or 4 : 3
 
No comments:
Post a Comment