Java Program to find Sum of Digits


1. Java Program to find sum of digits without using recursion.

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class SumOfDigits {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. int number;
  9. Scanner in = new Scanner(System.in);
  10.  
  11. System.out.println("Please Enter a number");
  12.  
  13. number=in.nextInt(); 
  14.  
  15. int sum=0 ;
  16.  
  17. while(number!=0){
  18.  
  19. sum=sum+(number%10);
  20. number=number/10;
  21. }
  22.  
  23. System.out.println("Sum of Digits ="+sum);
  24.  
  25. }
  26. }
Output:

  1. Please Enter a number
  2. 123
  3. Sum of Digits=6


2. Java Program to find sum of digits using recursion.

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class SumOfDigits {
  5.  
  6. int sum;

  7. public int CalRecSum(int n){
  8.  
  9. if(n==0){
  10. return sum;
  11. }
  12. else{
  13.  
  14.  sum+=n%10;
  15.  CalRecSum(n/10);
  16.  
  17.  
  18. return sum;
  19. }

  20. public static void main(String[] args) {
  21.  
  22. int number;
  23. Scanner in = new Scanner(System.in);
  24.  
  25. System.out.println("Please Enter a number");
  26.  
  27. number=in.nextInt(); 
  28.  
  29. SumOfDigits   ob= new SumOfDigits();
  30. System.out.println("Sum of Digits ="+ob.CalRecSum(number));
  31.  
  32. }
  33.  
  34. }


Output:

  1. Please Enter a number
  2. 326
  3. Sum of Digits=11

Swap two numbers without using third variable

1. Java Interview Program to Swap two numbers without using third variable in java



  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=20;
  8. int number2=30;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1+number2;
  15. number2=number1-number2;
  16. number1=number1-number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }
Output:

  1. Before Swapping
  2. Value of number1 is :20
  3. Value of number2 is :30
  4. After Swapping
  5. Value of number1 is :30
  6. Value of number2 is :20

2. Java Program to Swap two numbers by using division and multiplication.


  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=20;
  8. int number2=30;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1*number2;
  15. number2=number1/number2;
  16. number1=number1/number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }


Output:

  1. Before Swapping
  2. Value of number1 is :20
  3. Value of number2 is :30
  4. After Swapping
  5. Value of number1 is :30
  6. Value of number2 is :20


3. Java Program to Swap two integers by using bit wise operators


  1. package com.instaceofjava;
  2.  
  3. public class SwapTwoNumbers {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. int number1=2;
  8. int number2=4;
  9.  
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of number1 is :" + number1);
  12. System.out.println("Value of number2 is :" +number2); 
  13.  
  14. number1=number1^number2;
  15. number2=number1^number2;
  16. number1=number1^number2;
  17.  
  18. System.out.println("After Swapping");
  19. System.out.println("Value of number1 is :" + number1);
  20. System.out.println("Value of number2 is :" +number2);
  21.  
  22. }
  23. }
Output:

  1. Before Swapping
  2. Value of number1 is :2
  3. Value of number2 is :4
  4. After Swapping
  5. Value of number1 is :4
  6. Value of number2 is :2


HashSet class


Hierarchy of HashSet class:


 

 public class HashSet<E>
        extends AbstractSet<E>
           implements Set<E>, Cloneable, java.io.Serializable

Key points:

  • HashSet is sub class of AbstractSet class.
  • Underlying data structure of HashSet is HashTable.
  • HashSet implements Serializable and Cloneable interfaces
  • HashSet does not allow duplicate elements.
  • Insertion order is not preserved. No order(Based on hashcode of objects)



Constructors of HashSet:

1.HashSet( )
  • Creates an empty HashSet object with default initial capacity 16.  Fill ratio or load factor 0.75
2.HashSet(Collection obj)
  • This constructor initializes the hash set by using the elements of the collection obj.
3.HashSet(int capacity)
    • Creates an empty HashSet object with given capacity.
      4.HashSet(int capacity, float fillRatio)
      • This constructor initializes both the capacity and the fill ratio (also called load capacity) of the hash set from its arguments (fill ratio 0.1 to 1.0)

      Reverse words in a String

      1. Java Interview Program to Reverse words in a string


      1. package com.instaceofjava;
      2.  
      3. public class ReverseString {
      4.  
      5. public static void main(String[] args) {
      6.  
      7. String strng= "Instance of Java ";
      8.  
      9. String str[] =strng.split(" ");
      10.  
      11. String result="";
      12.  
      13. for(int i=str.length()-1;i>=0;i--){
      14.  
      15. result += str[i]+" ";
      16.  
      17. }
      18.  
      19. System.out.println(result );
      20. }
      21. }
      Output:

      1. Java of Instance



      2. Java Interview Program to Reverse words in a string


      1. package com.instaceofjava;
      2.  
      3. public class ReverseString {
      4.  
      5. public static void main(String[] args) {
      6.  
      7. String strng= "Instance of Java ";
      8.  
      9. StringBuilder sb = new StringBuilder(strng.length() + 1);
      10.  
      11.   String[] words = strng.split(" ");
      12.  
      13.   for (int i = words.length - 1; i >= 0; i--) {
      14.          sb.append(words[i]).append(' ');
      15.    }
      16.     
      17.     sb.setLength(sb.length() - 1); 
      18.  
      19.    String result= sb.toString();  
      20.  
      21.     System.out.println(result);

      22. }
      23. }
      Output:

      1. Java of Instance

      Introducton to Spring



      Spring Framework history:

      • Spring was developed in June,2003
      • Initially developed by Rod Johnson.
      • Latest version of  Spring Framework version 4.1.6 released in Mar 2015.

      What is Spring:
      • Spring is  Open Source Framework.
      • Spring is Lightweight Application Framework. 
      • Spring is  Simple framework.
      • Spring is loosely coupled. 
      • Spring is a complete and a modular framework,because spring can be used in all layers of applications means front end(Spring MVC),Database(Spring JDBC,Spring ORM).

      Why Spring:
      • Why Spring come in to picture is because of EJB fails,EJB having multiple configuration files,it is affected on performance.so spring came up.
      • Struts designed for Web Layer,like Other Frameworks also addressed specific layer,But Spring Framework provides solution to support all layers of application.
      • Spring is non invasive framework,means Spring doesn't force to implement or extend any class from predefined class from Spring API.

      Advantages of Spring:
      • Spring is open source and lightweight framework.
      • Spring is supports all layers including web layer.
      • Easy for testing.
      • Spring Supports POJO Model.
      • POJO-Plain Old Java Object. 
      • Spring works in simple Java environment,because it is non,invasive.
      • Spring can be integrated with any Application Server
      • Spring simplifies J2EE development. 



      Spring Modules:
       spring 1.x have seven modules,but in 2.x on wards we have 6 modules.
      • Spring Core
      • Spring DAO(Spring JDBC)
      • Spring AOP(Ascept Orient Programming)
      • Spring ORM(Spring Hibernate)
      • Spring MVC
      • Spring WEB
      • Spring Context(J2EE)  
      Spring 2.x Modules:
      from Spring 2.x ,Spring -WEB and Spring-MVC is combined.
      • Spring Core
      • Spring DAO(Spring JDBC)
      • Spring AOP(Ascept Orient Programming)
      • Spring ORM(Spring Hibernate)
      • Spring WEB-MVC
      • Spring Context(J2EE


      spring tutorial



      • Spring is combination of various modules Seven well defined modules,Most of them are reasonably independent.
      • Spring modules built using modular approach,you can use only required modules Each module is set of one or more JAR files
      • Spring’s core module is “Inversion of Control”(IoC) also known as “Dependency Injection”.
      • All other Spring modules are built on top of IoC.IoC is a Foundation or Container of Spring Framework.
      • Spring core module is the base module for all modules.

      Collection interface in java

      The Collection Interface in Java is the root interface(a kind of abstract class) in java.util package. It represents a group of objects, known as elements, and provides a unified way to manipulate and work with collections of data.

      Key Points About the Collection Interface

      Root Interface: The Collection interface is the root interface of all Collection objects in Java, except Map.
      Generic Interface: A generic interface, meaning it can work with any type of object (_e.g.,_ Collection<String>, Collection etc.).
      Common Methods: For example, we can use the following methods for both adding and deleting elements to an array: add(), remove (), size().
      Extends Iterable: The Collectioneuml; interface extends the Iterableinterface, and this means that all collections can be iterated over via either an iterator or the enhanced for-loop construct.



      1. public interface Collection<E>
      2. extends Iterable<E>


      Methods in Collection Interface:

       1.public Boolean add(Object obj)
      •  Used to add element in to the collection

       2.public Boolean addAll(Collection c)

      • Adds all the elements of c to the invoking collection. Returns true if the operation succeeded else returns false.

       3.public boolean isEmpty()
      • Returns true if collection is empty otherwise returns false.

       4.public boolean remove(Object obj)
      • Remove element from collection. Returns true if successful deletion.

      5.public boolean  removeAll(Collection c)
      • Removes all elements of a collection.

       6.public void clear()
      • Deletes total elements from collection

       7.public boolean contains(Object obj)
      • This method used to search an element

       8.public boolean containsAll(Collection c)
      • This method used to search an element in a collection

       9.public boolean retianAll(Collection c)
      • Used to delete all elements from a collection except specified one.

      10.public int size()
      • Returns total numbers of elements in a collection

      11.public Iterator iterator()
      • Returns iterator for the collection

      12.public boolean equals(Object obj)
      • Compares two collections

      13.public int hashCode()
      • Returns hashcode

      14.public Object[] toArray()
      • This method used to convert collection into array
      15.public Object[] toArray(Object[] obj)
      • Returns an array containing only those collection elements whose type matches that of array.

      Difference Between Collection and Collections:

      • Collection is the base interface for most of the classes.
      • Collections is the utility class.


      Select Menu