Write a program in Java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc

Write a program in Java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc


import java.util.ArrayList;
class hunteye {
    public static void main(String[] args) {
      ArrayList obj = new ArrayList();
      // isEmpty() will return true here as obj is empty 
      System.out.println(obj.isEmpty()); // true
      //size() will return 0
       System.out.println(obj.size()); //0
       // adding int
       obj.add(12);
       // adding float
       obj.add(567.123);
       //adding char
       obj.add('$');
       //adding string 
       obj.add("Java is OOPS");
       //printing all element
       System.out.println(obj);
        // isEmpty() will return false here as obj is not empty 
      System.out.println(obj.isEmpty()); // false
     //size() will return 4
       System.out.println(obj.size()); //4
      //contains(12) will return true
      System.out.println(obj.contains(12)); 
      //remove() to remove  element from ArrayList 
      obj.remove(0);// deleting element from 0 Position
      //printing all element
      System.out.println(obj);
    }
}

Output

true
0
[12, 567.123, $, Java is OOPS]
false
4
true
[12, $, Java is OOPS]

Share:

Explain the ArrayList class in java with example

ArrayList class in Java

Definition

ArrayList is a predefined java class under java util package

Advantage

ArrayList is a generic dynamic array. Which means it can be of anytype or of any size

Some important methods of ArrayList class

Methods Purpose
add() It is used to add elements in ArrayList
size() It returns the size of ArrayList
isEmpty() It returns true if ArrayList is Empty, otherwise false
contains() It is used to search an element inside ArrayList and returns true if found, otherwise false
remove() It is used to delete an element from ArrayList

WAP in java to implement ArrayList and use the important mrthods like add(), size(),isEmpty() etc


import java.util.ArrayList;
class hunteye {
    public static void main(String[] args) {
      ArrayList obj = new ArrayList();
      // isEmpty() will return true here as obj is empty 
      System.out.println(obj.isEmpty()); // true
      //size() will return 0
       System.out.println(obj.size()); //0
       // adding int
       obj.add(12);
       // adding float
       obj.add(567.123);
       //adding char
       obj.add('$');
       //adding string 
       obj.add("Java is OOPS");
       //printing all element
       System.out.println(obj);
        // isEmpty() will return false here as obj is not empty 
      System.out.println(obj.isEmpty()); // false
     //size() will return 4
       System.out.println(obj.size()); //4
      //contains(12) will return true
      System.out.println(obj.contains(12)); 
      //remove() to remove  element from ArrayList 
      obj.remove(0);// deleting element from 0 Position
      //printing all element
      System.out.println(obj);
    }
}

Output

true
0
[12, 567.123, $, Java is OOPS]
false
4
true
[12, $, Java is OOPS]

Share:

Object in JAVA and it's uses

What is Object in Java and what are it's Uses?

Definition

A class instance is known as Objects. In java object is the runtime entity of a class

Uses

Object is used to access the class members (both data member and member function)

Example



class hunteye {

    //data member
    int x ; 
    
    //member function
    void set(){
        x=13;
    }
    
    void get(){
        System.out.println("x = "+x);
        
    }
    
    public static void main(String[] args) {
    
      hunteye obj = new hunteye(); //object is created here 
      obj.set(); //object is access member function here
      obj.get();
    }
}

Share:

Write a program in Java to print Fibonacci Series using Recursion

Write a program in Java to print Fibonacci Series using Recursion


public class Fibonacci {
    public static void printFibonacci(int count, int a, int b) {
        int c = a + b;
        if (count == 0) {
            return;
        }
        System.out.print(c + "\t");
        a = b;
        b = c;
        printFibonacci(count - 1, a, b);
    }

    public static void main(String[] args) {
        int count = 8;
        System.out.print(0 + "\t" + 1 + "\t");
        printFibonacci(count - 2, 0, 1);
    }
}


Output

0 1 1 2 3 5 8 13

Share:

Write a program in Java to Calculate Factorial of a number using Recursion

Write a program in Java to Calculate Factorial of a number using Recursion


public class Factorial {

    public static int calcFact(int n) {
        if (n == 1 || n == 0) {
            return 1;
        }
        int fact = calcFact(n - 1);
        return n * fact;
    }

    public static void main(String[] args) {

        System.out.print(calcFact(5));
    }
}


Output

120

Share:

Write a Program in Java to demonstrate Selection Sort

Write a Program in Java to demonstrate Selection Sort


public class SelectionSort {
    public static void sort(int arr[]) {
        for (int i = 0; i < arr.length - 1; i++) {
            int pos = i; //Position of minimum element
            System.out.println("Pass " + (i + 1));
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[pos]) {
                    pos = j;
                }
            }
            int tmp = arr[i];
            arr[i] = arr[pos];
            arr[pos] = tmp;
            display(arr);
        }
    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.println("***********************************\n");
    }

    public static void main(String args[]) {
        int arr[]={56,23,67,11,45};
        sort(arr);
    }
}


Output

Pass 1
11 23 67 56 45
***********************************

Pass 2
11 23 67 56 45
***********************************

Pass 3
11 23 45 56 67
***********************************

Pass 4
11 23 45 56 67
***********************************

Share:

Write a Program in Java to demonstrate Insertion Sort

Write a Program in Java to demonstrate Insertion Sort


public class InsertionSort {
    public static void sort(int arr[]) {

        for (int i = 1; i < arr.length; i++) {
            System.out.println("Pass " + (i));
            int j = i - 1;
            int key = arr[i];
            while (j >= 0 && key < arr[j]) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = key;
            display(arr);
        }

    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.println("***********************************\n");
    }

    public static void main(String args[]) {
        int arr[] = { 5, 1, 4, 2, 8 };
        sort(arr);
        // display(arr);
    }
}


Output

Pass 1
1 5 4 2 8
***********************************

Pass 2
1 4 5 2 8
***********************************

Pass 3
1 2 4 5 8
***********************************

Pass 4
1 2 4 5 8
***********************************

Share:

Write a Program in Java to demonstrate Bubble Sort

Write a Program in Java to demonstrate Bubble Sort


public class BubbleSort {
    public static void sort(int arr[]) {
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.println("Pass " + (i + 1));
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }

                display(arr);
            }
            System.out.println("***********************************\n");
        }
    }

    public static void display(int arr[]) {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public static void main(String args[]) {
        int arr[] = { 5, 1, 4, 2, 8 };
        sort(arr);
        // display(arr);
    }
}

Output

Pass 1
1 5 4 2 8
1 4 5 2 8
1 4 2 5 8
1 4 2 5 8
***********************************

Pass 2
1 4 2 5 8
1 2 4 5 8
1 2 4 5 8
***********************************

Pass 3
1 2 4 5 8
1 2 4 5 8
***********************************
Pass 4

1 2 4 5 8
***********************************

Share:

Write a program in Java to take inputs from users in an array and display the elements of Array

Write a program in Java to take inputs from users in an array and display the elements of Array


import java.util.Scanner;

public class ArrayProgram {
    int arr[];
    int arrLen, i;
    Scanner sc = new Scanner(System.in);

    // to input elements in array
    void input() {
        System.out.println("How many elements you want to enter : ");
        arrLen = sc.nextInt();
        arr = new int[arrLen];
        for (i = 0; i < arrLen; i++) {
            arr[i] = sc.nextInt();
        }
    }

    // to display elements of array
    void display() {
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayProgram obj = new ArrayProgram();
        obj.input();
        obj.display();
    }
}


Share:

Write a program in Java to print the diagonal elements of given 3X3 Matrix

Write a program in Java to print the diagonal elements of given 3X3 Matrix

    
import java.io.*;
public class DiagElem {
    public static void main(String[] args) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Elements of 3X3 Array :");
        int arr[][] = new int[3][3];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                arr[i][j]= Integer.parseInt(obj.readLine());
            }
            
        }
        System.out.println("Entered Array is :");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        int ldiag[]= new int[3];
        int rdiag[]= new int[3];
        System.out.println(" Left Diagonal Elements Of Array :");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (i==j) {
                    ldiag[i]=arr[i][j];
                }
                
            }
            System.out.print(ldiag[i]+" ");
        }
        System.out.println();
        System.out.println("Right Diagonal Elements Of Array :");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (i+j==2) {
                    rdiag[i]=arr[i][j];
                }
                
            }
            System.out.print(rdiag[i]+" ");
        }
    }
    
}
    
Share:

Write a program in Java to print all the prime number between 1 to 100

Write a program in Java to print all the prime number between 1 to 100

    
public class Prime {
    public static void main(String[] args) {
        System.out.println("Prime numbers between 1 to 100 :");
        int check = 0;
        for (int i = 2; i <= 100; i++) {
            for (int j = 2; j <= i - 1; j++) {
                if (i % j == 0) {
                    check = 0;
                    break;
                } else {
                    check = 1;
                }
            }
            if (i == 2) {
                System.out.println(i);
            }

            if (check == 1) {
                System.out.println(i);
            }
        }

    }
}
    
Share:

Write a program in c++ to input and print the multiplication of two matrix

Write a program in c++ to input and print the multiplication of two matrix




#include <iostream>
using namespace std;
int main(){
    int r1,r2,c1,c2;
    cout<<"Enter Rows of First Matrix : ";
    cin>>r1;
    cout<<"Enter Columns of First Matrix : ";
    cin>>c1;
    cout<<"Enter Rows of Second Matrix : ";
    cin>>r2;
    cout<<"Enter Columns of Second Matrix : ";
    cin>>c2;
    int a[r1][c1],b[r2][c2],c[r1][c2];
    if (c1==r2)
    { //taking elements of first matrix
        cout<<"Enter Elements Of first Matrix "<<endl;
       for (int i = 0; i < r1; i++)
        {
                for (int j = 0; j < c1; j++)
                {
                    cin>>a[i][j];               
                }
        }
        //printing first matrix 
        cout<<"Elements Of first Matrix are "<<endl;
        for (int i = 0; i < r1; i++)
         {
                for (int j = 0; j < c1; j++)
                 {
                    cout<<a[i][j]<<" ";               
                 }
               cout<<endl;
        }
        //taking elements of second matrix
        cout<<"Enter Elements Of second Matrix "<<endl;
       for (int i = 0; i < r2; i++)
        {
                for (int j = 0; j < c2; j++)
                {
                    cin>>b[i][j];               
                }
        }
        //printing second matrix
        cout<<"Elements Of second Matrix are "<<endl;
        for (int i = 0; i < r2; i++)
         {
                for (int j = 0; j < c2; j++)
                 {
                    cout<<b[i][j]<<" ";               
                 }
               cout<<endl;
        }
        //multiplying first and second matrix
        for (int i = 0; i < r1; i++) 
        {
                for (int j = 0; j < c2; j++) 
                {
                    c[i][j] = 0;
                    for (int k = 0; k < r2; k++) 
                    {
                        c[i][j] = c[i][j] + a[i][k] * b[k][j];
                    }
                }
        }
        //printing multiplied matrix
            cout<<"Matrix after multiplication "<<endl;
             for (int i = 0; i < r1; i++)
              {
                for (int j = 0; j < c2; j++)
                 {
                    cout<<" " << c[i][j] << " ";
                }
                cout<<endl;
             }

        
    } else 
        {
            cout<<"Sorry!! can't Multiply";
        }
    
}





Share:

Write a program in java to input 10 numbers and find smallest among them

Write a program in java to input 10 numbers and find smallest among them



import java.util.*;
public class ArrSmaller {
    public static void main(String[] args) {
        Scanner obj = new Scanner(System.in);
        System.out.println("Enter 10 Number : ");
        int arr[]= new int[10];
        int check;
        for (int i = 0; i < 10; i++) {           
        arr[i]=obj.nextInt();
      }
         check=arr[0];
         obj.close();
         for (int i = 0; i < 10; i++) {
             if (arr[i]<check) {
                 check = arr[i];                
             }

         }
      System.out.println("Smallest Number : "+ check);
    }
    
}


Share:

Write a program in java to input 10 numbers and find largest among them

Write a program in java to input 10 numbers and find largest among them



import java.io.*;
public class ArrLarger {
    public static void main(String[] args)throws IOException {
        BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter 10 Numbers : ");
        int arr[] = new int[10];
        int check = 0;
        int i;
        for (i = 0; i < 10; i++) {
            arr[i]=Integer.parseInt(obj.readLine());          
        }
     for (i = 0; i < 10; i++) {
         if (arr[i]>check) {
             check =arr[i];
         }         
     }
     System.out.println("Largest Number Is : "+ check);
    } 
}


Share:

Write a program in java to input and print the multiplication of two matrix

Write a program in java to input and print the multiplication of two matrix

Share:

Pageviews