- /*
- Reverse Number using Java
- This Java Reverse Number Example shows how to reverse a given number.
- */
- public class ReverseNumber {
- public static void main(String[] args) {
- //original number
- int number = 1234;
- int reversedNumber = 0;
- int temp = 0;
- while(number > 0){
- //use modulus operator to strip off the last digit
- temp = number%10;
- //create the reversed number
- reversedNumber = reversedNumber * 10 + temp;
- number = number/10;
- }
- //output the reversed number
- System.out.println("Reversed Number is: " + reversedNumber);
- }
- }
- /*
- Output of this Number Reverse program would be
- Reversed Number is: 4321
- */
- /*
- Find Largest and Smallest Number in an Array Example
- This Java Example shows how to find largest and smallest number in an
- array.
- */
- public class FindLargestSmallestNumber {
- public static void main(String[] args) {
- //array of 10 numbers
- int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
- //assign first element of an array to largest and smallest
- int smallest = numbers[0];
- int largetst = numbers[0];
- for(int i=1; i< numbers.length; i++)
- {
- if(numbers[i] > largetst)
- largetst = numbers[i];
- else if (numbers[i] < smallest)
- smallest = numbers[i];
- }
- System.out.println("Largest Number is : " + largetst);
- System.out.println("Smallest Number is : " + smallest);
- }
- }
- /*
- Output of this program would be
- Largest Number is : 98
- Smallest Number is : 23
- */
- /*
- Even Odd Number Example
- This Java Even Odd Number Example shows how to check if the given
- number is even or odd.
- */
- public class FindEvenOrOddNumber {
- public static void main(String[] args) {
- //create an array of 10 numbers
- int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
- for(int i=0; i < numbers.length; i++){
- /*
- * use modulus operator to check if the number is even or odd.
- * If we divide any number by 2 and reminder is 0 then the number is
- * even, otherwise it is odd.
- */
- if(numbers[i]%2 == 0)
- System.out.println(numbers[i] + " is even number.");
- else
- System.out.println(numbers[i] + " is odd number.");
- }
- }
- }
- /*
- Output of the program would be
- 1 is odd number.
- 2 is even number.
- 3 is odd number.
- 4 is even number.
- 5 is odd number.
- 6 is even number.
- 7 is odd number.
- 8 is even number.
- 9 is odd number.
- 10 is even number.
- */
- /*
- Java Factorial Using Recursion Example
- This Java example shows how to generate factorial of a given number
- using recursive function.
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class JavaFactorialUsingRecursion {
- public static void main(String args[]) throws NumberFormatException, IOException{
- System.out.println("Enter the number: ");
- //get input from the user
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- int a = Integer.parseInt(br.readLine());
- //call the recursive function to generate factorial
- int result= fact(a);
- System.out.println("Factorial of the number is: " + result);
- }
- static int fact(int b)
- {
- if(b <= 1)
- //if the number is 1 then return 1
- return 1;
- else
- //else call the same function with the value - 1
- return b * fact(b-1);
- }
- }
- /*
- Output of this Java example would be
- Enter the number:
- 5
- Factorial of the number is: 120
- */
- /*
- Swap Numbers Java Example
- This Swap Numbers Java Example shows how to
- swap value of two numbers using java.
- */
- public class SwapElementsExample {
- public static void main(String[] args) {
- int num1 = 10;
- int num2 = 20;
- System.out.println("Before Swapping");
- System.out.println("Value of num1 is :" + num1);
- System.out.println("Value of num2 is :" +num2);
- //swap the value
- swap(num1, num2);
- }
- private static void swap(int num1, int num2) {
- int temp = num1;
- num1 = num2;
- num2 = temp;
- System.out.println("After Swapping");
- System.out.println("Value of num1 is :" + num1);
- System.out.println("Value of num2 is :" +num2);
- }
- }
- /*
- Output of Swap Numbers example would be
- Before Swapping
- Value of num1 is :10
- Value of num2 is :20
- After Swapping
- Value of num1 is :20
- Value of num2 is :10
- */
- /*
- Java Bubble Sort Descending Order Example
- This Java bubble sort example shows how to sort an array of int in descending
- order using bubble sort algorithm.
- */
- public class BubbleSortDescendingOrder {
- public static void main(String[] args) {
- //create an int array we want to sort using bubble sort algorithm
- int intArray[] = new int[]{5,90,35,45,150,3};
- //print array before sorting using bubble sort algorithm
- System.out.println("Array Before Bubble Sort");
- for(int i=0; i < intArray.length; i++){
- System.out.print(intArray[i] + " ");
- }
- //sort an array in descending order using bubble sort algorithm
- bubbleSort(intArray);
- System.out.println("");
- //print array after sorting using bubble sort algorithm
- System.out.println("Array After Bubble Sort");
- for(int i=0; i < intArray.length; i++){
- System.out.print(intArray[i] + " ");
- }
- }
- private static void bubbleSort(int[] intArray) {
- /*
- * In bubble sort, we basically traverse the array from first
- * to array_length - 1 position and compare the element with the next one.
- * Element is swapped with the next element if the next element is smaller.
- *
- * Bubble sort steps are as follows.
- *
- * 1. Compare array[0] & array[1]
- * 2. If array[0] < array [1] swap it.
- * 3. Compare array[1] & array[2]
- * 4. If array[1] < array[2] swap it.
- * ...
- * 5. Compare array[n-1] & array[n]
- * 6. if [n-1] < array[n] then swap it.
- *
- * After this step we will have smallest element at the last index.
- *
- * Repeat the same steps for array[1] to array[n-1]
- *
- */
- int n = intArray.length;
- int temp = 0;
- for(int i=0; i < n; i++){
- for(int j=1; j < (n-i); j++){
- if(intArray[j-1] < intArray[j]){
- //swap the elements!
- temp = intArray[j-1];
- intArray[j-1] = intArray[j];
- intArray[j] = temp;
- }
- }
- }
- }
- }
- /*
- Output of the Bubble Sort Descending Order Example would be
- Array Before Bubble Sort
- 5 90 35 45 150 3
- Array After Bubble Sort
- 150 90 45 35 5 3
- */
- /*
- Java Bubble Sort Example
- This Java bubble sort example shows how to sort an array of int using bubble
- sort algorithm. Bubble sort is the simplest sorting algorithm.
- */
- public class BubbleSort {
- public static void main(String[] args) {
- //create an int array we want to sort using bubble sort algorithm
- int intArray[] = new int[]{5,90,35,45,150,3};
- //print array before sorting using bubble sort algorithm
- System.out.println("Array Before Bubble Sort");
- for(int i=0; i < intArray.length; i++){
- System.out.print(intArray[i] + " ");
- }
- //sort an array using bubble sort algorithm
- bubbleSort(intArray);
- System.out.println("");
- //print array after sorting using bubble sort algorithm
- System.out.println("Array After Bubble Sort");
- for(int i=0; i < intArray.length; i++){
- System.out.print(intArray[i] + " ");
- }
- }
- private static void bubbleSort(int[] intArray) {
- /*
- * In bubble sort, we basically traverse the array from first
- * to array_length - 1 position and compare the element with the next one.
- * Element is swapped with the next element if the next element is greater.
- *
- * Bubble sort steps are as follows.
- *
- * 1. Compare array[0] & array[1]
- * 2. If array[0] > array [1] swap it.
- * 3. Compare array[1] & array[2]
- * 4. If array[1] > array[2] swap it.
- * ...
- * 5. Compare array[n-1] & array[n]
- * 6. if [n-1] > array[n] then swap it.
- *
- * After this step we will have largest element at the last index.
- *
- * Repeat the same steps for array[1] to array[n-1]
- *
- */
- int n = intArray.length;
- int temp = 0;
- for(int i=0; i < n; i++){
- for(int j=1; j < (n-i); j++){
- if(intArray[j-1] > intArray[j]){
- //swap the elements!
- temp = intArray[j-1];
- intArray[j-1] = intArray[j];
- intArray[j] = temp;
- }
- }
- }
- }
- }
- /*
- Output of the Bubble Sort Example would be
- Array Before Bubble Sort
- 5 90 35 45 150 3
- Array After Bubble Sort
- 3 5 35 45 90 150
- */
- /*
- Add an element to specified index of Java ArrayList Example
- This Java Example shows how to add an element at specified index of java
- ArrayList object using add method.
- */
- import java.util.ArrayList;
- public class AddElementToSpecifiedIndexArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- /*
- To add an element at the specified index of ArrayList use
- void add(int index, Object obj) method.
- This method inserts the specified element at the specified index in the
- ArrayList.
- */
- arrayList.add(1,"INSERTED ELEMENT");
- /*
- Please note that add method DOES NOT overwrites the element previously
- at the specified index in the list. It shifts the elements to right side
- and increasing the list size by 1.
- */
- System.out.println("ArrayList contains...");
- //display elements of ArrayList
- for(int index=0; index < arrayList.size(); index++)
- System.out.println(arrayList.get(index));
- }
- }
- /*
- Output would be
- ArrayList contains...
- 1
- INSERTED ELEMENT
- 2
- 3
- */
- /*
- Iterate through elements Java ArrayList using Iterator Example
- This Java Example shows how to iterate through the elements of java
- ArrayList object using Iterator.
- */
- import java.util.ArrayList;
- import java.util.Iterator;
- public class IterateThroughArrayListUsingIteratorExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- arrayList.add("4");
- arrayList.add("5");
- //get an Iterator object for ArrayList using iterator() method.
- Iterator itr = arrayList.iterator();
- //use hasNext() and next() methods of Iterator to iterate through the elements
- System.out.println("Iterating through ArrayList elements...");
- while(itr.hasNext())
- System.out.println(itr.next());
- }
- }
- /*
- Output would be
- Iterating through ArrayList elements...
- 1
- 2
- 3
- 4
- 5
- */
- /*
- Search an element of Java ArrayList Example
- This Java Example shows how to search an element of java
- ArrayList object using contains, indexOf and lastIndexOf methods.
- */
- import java.util.ArrayList;
- public class SearchAnElementInArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("1");
- arrayList.add("2");
- arrayList.add("3");
- arrayList.add("4");
- arrayList.add("5");
- arrayList.add("1");
- arrayList.add("2");
- /*
- To check whether the specified element exists in Java ArrayList use
- boolean contains(Object element) method.
- It returns true if the ArrayList contains the specified objct, false
- otherwise.
- */
- boolean blnFound = arrayList.contains("2");
- System.out.println("Does arrayList contain 2 ? " + blnFound);
- /*
- To get an index of specified element in ArrayList use
- int indexOf(Object element) method.
- This method returns the index of the specified element in ArrayList.
- It returns -1 if not found.
- */
- int index = arrayList.indexOf("4");
- if(index == -1)
- System.out.println("ArrayList does not contain 4");
- else
- System.out.println("ArrayList contains 4 at index :" + index);
- /*
- To get last index of specified element in ArrayList use
- int lastIndexOf(Object element) method.
- This method returns index of the last occurrence of the
- specified element in ArrayList. It returns -1 if not found.
- */
- int lastIndex = arrayList.lastIndexOf("1");
- if(lastIndex == -1)
- System.out.println("ArrayList does not contain 1");
- else
- System.out.println("Last occurrence of 1 in ArrayList is at index :"
- + lastIndex);
- }
- }
- /*
- Output would be
- Does arrayList contain 2 ? true
- ArrayList contains 4 at index :3
- Last occurrence of 1 in ArrayList is at index :5
- */
- /*
- Find Minimum element of Java ArrayList Example
- This java example shows how to find a minimum element of Java ArrayList using
- min method of Collections class.
- */
- import java.util.ArrayList;
- import java.util.Collections;
- public class FindMinimumOfArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add(new Integer("327482"));
- arrayList.add(new Integer("13408"));
- arrayList.add(new Integer("802348"));
- arrayList.add(new Integer("345308"));
- arrayList.add(new Integer("509324"));
- /*
- To find minimum element of Java ArrayList use,
- static Object min(Collection c) method of Collections class.
- This method returns the minimum element of Java ArrayList according to
- its natural ordering.
- */
- Object obj = Collections.min(arrayList);
- System.out.println("Minimum Element of Java ArrayList is : " + obj);
- }
- }
- /*
- Output would be
- Minimum Element of Java ArrayList is : 13408
- */
- /*
- Find maxmimum element of Java ArrayList Example
- This java example shows how to find a maximum element of Java ArrayList using
- max method of Collections class.
- */
- import java.util.ArrayList;
- import java.util.Collections;
- public class FindMaximumOfArrayListExample {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add(new Integer("327482"));
- arrayList.add(new Integer("13408"));
- arrayList.add(new Integer("802348"));
- arrayList.add(new Integer("345308"));
- arrayList.add(new Integer("509324"));
- /*
- To find maximum element of Java ArrayList use,
- static Object max(Collection c) method of Collections class.
- This method returns the maximum element of Java ArrayList according to
- its natural ordering.
- */
- Object obj = Collections.max(arrayList);
- System.out.println("Maximum Element of Java ArrayList is : " + obj);
- }
- }
- /*
- Output would be
- Maximum Element of Java ArrayList is : 802348
- */
- /*
- Add elements at beginning and end of LinkedList Java example
- This java example shows how to add an element at beginning or at end of
- java LinkedList object using addFirst and addLast methods.
- */
- import java.util.LinkedList;
- public class AddElementsAtStartEndLinkedListExample {
- public static void main(String[] args) {
- //create LinkedList object
- LinkedList lList = new LinkedList();
- //add elements to LinkedList
- lList.add("1");
- lList.add("2");
- lList.add("3");
- lList.add("4");
- lList.add("5");
- System.out.println("LinkedList contains : " + lList);
- /*
- * To add an element at the beginning of the LinkedList, use
- * void addFirst(Object obj) method.
- *
- * This method inserts object at the beginning of the LinkedList.
- */
- lList.addFirst("0");
- System.out.println("After inserting 0 at beginning, LinkedList contains :"
- + lList);
- /*
- * To append an element at end of the LinkedList, use
- * void addLast(Object obj) method.
- *
- * This method append specified element at the end of the LinkedList.
- */
- lList.addLast("6");
- System.out.println("After appending 0 at end, LinkedList contains :" + lList);
- }
- }
- /*
- Output would be
- LinkedList contains : [1, 2, 3, 4, 5]
- After inserting 0 at beginning, LinkedList contains :[0, 1, 2, 3, 4, 5]
- After appending 0 at end, LinkedList contains :[0, 1, 2, 3, 4, 5, 6]
- */
- /*
- Performing Binary Search on Java int Array Example
- This java example shows how to perform binary search for an element of
- java int array using Arrays class.
- */
- import java.util.Arrays;
- public class BinarySearchIntArrayExample {
- public static void main(String[] args) {
- //create int array
- int intArray[] = {1,2,4,5};
- /*
- To perform binary search on int array use
- int binarySearch(int[] b, int value) of Arrays class. This method searches
- the int array for specified int value using binary search algorithm.
- Please note that the int array MUST BE SORTED before it can be searched
- using binarySearch method.
- This method returns the index of the value to be searched, if found in the
- array.
- Otherwise it returns (- (X) - 1)
- where X is the index where the the search value would be inserted.
- i.e. index of first element that is grater than the search
- value or array.length, if all elements of an array are less than
- the search value.
- */
- //sort int array using Arrays.sort method
- Arrays.sort(intArray);
- //value to search
- int searchValue = 2;
- //since 2 is present in int array, index of it would be returned
- int intResult = Arrays.binarySearch(intArray,searchValue);
- System.out.println("Result of binary search of 2 is : " + intResult);
- //lets search something which is not in int array !
- searchValue = 3;
- intResult = Arrays.binarySearch(intArray,searchValue);
- System.out.println("Result of binary search of 3 is : " + intResult);
- }
- }
- /*
- Output would be
- Result of binary search of 2 is : 1
- Result of binary search of 3 is : -3
- */
Popular Posts
-
List of Company Slogans: Microsoft - Your potential. Our passion. IBM - I think, therefore IBM. WIPRO - Applying thoughts I...
-
This c program prints current system date. To print date we will use getdate function. C programming code #include<stdio.h> #include...
-
Google history Google timeline Our company has packed a lot in to a relatively young li...
-
1.What is an operating system? Ans: An operating system is a program that acts as an intermediary between the user and the computer har...
-
c program to add two complex numbers :- This program calculate the sum of two complex numbers which will be entered by the user and then p...
Monday, 5 March 2012
Model Java Programs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment