Search

Q16.Duplicate characters in a String model 4

 package practice1;


import java.util.Scanner;


public class DupCharsinString3 {


public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner s = new Scanner(System.in);

        System.out.println("Enter String: ");

        String str = s.nextLine();


        for(int i=0;i<str.length()-1;i++)

        {

            for(int j=i+1;j<str.length();j++)


            {

            if(str.charAt(i)==str.charAt(j))

            {


                System.out.print(str.charAt(j));

            }

            }

        }

    


}


}


Q15.Duplicate characters in string model 3

 package practice1;


import java.util.HashSet;

import java.util.Set;

public class DuplicateCharsInString {

public static void duplicate(String input){

        

        char chip[]=input.toCharArray();

                

        Set duplicateset=new HashSet<>();

         Set distinctset=new HashSet<>();

        

        for(char ch:chip){

            

        if (distinctset.add(ch)==false){

            

            duplicateset.add(ch);

            

        }

        

    }

    

       duplicateset.forEach(ch->System.out.println(ch));

        

         

    }

    

    public static void main(String[] args) {

        

        String input="sanjeev sompalli";

        DuplicateCharsInString.duplicate(input);

    }


}


Q14.Duplicate Characters in a String model 2

 package practice1;


public class DuplStrchars {


public static void main(String[] args) {

// TODO Auto-generated method stub


String str = "sanjeevaiah sompalli";

  int cnt = 0;

  char[] inp = str.toCharArray();

  System.out.println("Duplicate Characters are:");

  for (int i = 0; i < str.length(); i++) {

   for (int j = i + 1; j < str.length(); j++) {

    if (inp[i] == inp[j]) {

     System.out.println(inp[j]);

     cnt++;

     break;

    }

   }

  }

}


}


Q13.Compare two similar Strings in Java

 package practice1;


import java.util.ArrayList;

import java.util.List;


public class CompareTwoSimilarStrings {

static int max1=0;

public static void main(String[] args) {    

        String string1 = "inc Demart";   

        String string2= "Demart inc";

        int count;    

            

        //Converts given string into character array    

        char strng1[] = string1.toCharArray();    

        char strng2[] = string2.toCharArray(); 

        

        System.out.println(string2.length());

            

        System.out.println("Duplicate characters in a given string: ");    

        //Counts each character present in the string    

        for(int i = 0; i <strng1.length; i++) {    

            count = 1;    

            

            

         //   System.out.println("I value is : "+i);

            for(int j = 0; j <strng2.length; j++) {    

           

            //System.out.println("strng1[i]::"+strng1[i]);

            //System.out.println("strng2[j]::"+strng2[j]);

         //    System.out.println("J value is : "+j);

                if(strng1[i] == strng2[j] && strng1[i] != ' ' && strng2[j] != ' ') {    

                    count++;    

                    

                                        

                   

                    //Set string[j] to 0 to avoid printing visited character    

                   strng2[j] = '0';    

                }    

            }    

            //A character is considered as duplicate if count is greater than 1    

            if(count > 1 && strng1[i] != '0' && strng1[i] != ' ' )  { 

           

            int max=0;

           

            max++;

           

            max1=max1+max;

           

            System.out.println("max1 value is::: "+max1);

           

            max++;

                

           

                System.out.println(strng1[i]+" duplicate count is::: "+count); 

              

                

           

            }

                

           

           

            

        }

        

        int m1=max1;

        int m2=strng2.length;

        int m3=(m1*100)/m2;

        System.out.println(m1+"m info"+m2);

        System.out.println(m1+"max percwntage"+m3);

       

        if(m3>50) {

       

        System.out.println("Pass");

        } else {

        System.out.println("fail");

        }

    } 


}



Q12.Fabinoic Series in Java

 package practice1;


public class FabinoicSeries {


  public static void main(String[] args) {


    int i = 1, n = 10, firstTerm = 0, secondTerm = 1;

    System.out.println("Fibonacci Series till " + n + " terms:");


    while (i <= n) {

      System.out.print(firstTerm + ", ");


      int nextTerm = firstTerm + secondTerm;

      firstTerm = secondTerm;

      secondTerm = nextTerm;


      i++;

    }

  }

}


Q12.Factorial of a number in Java

 package practice1;


public class FactorialofaNumber {

public static void main(String[] args) {


        int num = 5, i = 1;

        long factorial = 1;

        while(i <= num)

        {

            factorial *= i;

            i++;

        }

        System.out.printf("Factorial of %d = %d", num, factorial);

    }


}


Q11.Find Sum of numerics present in a String in Java

 package practice1;


public class FindSumNumericsPresentString {

static int findSum(String str)

    {

        // A temporary string

        String temp = "0";

 

        // holds sum of all numbers present in the string

        int sum = 0;

 

        // read each character in input string

        for (int i = 0; i < str.length(); i++) {

            char ch = str.charAt(i);

 

            // if current character is a digit

            if (Character.isDigit(ch))

                temp += ch;

 

            // if current character is an alphabet

            else {

                // increment sum by number found earlier

                // (if any)

                sum += Integer.parseInt(temp);

 

                // reset temporary string to empty

                temp = "0";

            }

        }

 

        // atoi(temp.c_str()) takes care of trailing

        // numbers

        return sum + Integer.parseInt(temp);

    }

 

    // Driver code

    public static void main(String[] args)

    {

 

        // input alphanumeric string

        String str = "12abc20yz68";

 

        // Function call

        System.out.println(findSum(str));

    }

}



Q10.Print Duplicate Characters in String in Java

 package practice1;


public class FindDuplicateCharsinString {


public static void main(String[] args) {    

        String string1 = "sanjeevaiah Sompalli8768768768";    

        int count;    

            

        //Converts given string into character array    

        char string[] = string1.toCharArray();    

            

        System.out.println("Duplicate characters in a given string: ");    

        //Counts each character present in the string    

        for(int i = 0; i <string.length; i++) {    

            count = 1;    

            

         //   System.out.println("I value is : "+i);

            for(int j = i+1; j <string.length; j++) {    

           

         //    System.out.println("J value is : "+j);

                if(string[i] == string[j] && string[i]!= ' ') {    

                    count++;    

                    

                    

                    //Set string[j] to 0 to avoid printing visited character    

                   string[j] = '0';    

                }    

            }    

            //A character is considered as duplicate if count is greater than 1    

            if(count > 1 && string[i] != '0' )  { 

            //System.out.println(count);

                System.out.println(string[i]+" duplicate count is::: "+count);    

            } /*if(string[i]=='a') {

           

            System.out.println(string[i]+" duplicate count is::: "+count); 

           

            } */

        }    

    }    

}     


Q9.Print Leap Year

 package practice1;


public class LeapYear {


  public static void main(String[] args) {


    // year to be checked

    int year = 1996;

    boolean leap = false;


    // if the year is divided by 4

    if (year % 4 == 0) {


      // if the year is century

      if (year % 100 == 0) {


        // if year is divided by 400

        // then it is a leap year

        if (year % 400 == 0)

          leap = true;

        else

          leap = false;

      }

      

      // if the year is not century

      else

        leap = true;

    }

    

    else

      leap = false;


    if (leap)

      System.out.println(year + " is a leap year.");

    else

      System.out.println(year + " is not a leap year.");

  }

}



Q8.Print Prime numbers model 2

 package practice1;


public class PrimeNumbers {


public static void main(String[] args) {


int i,m=0,flag=0; 

 

  int n=17;//it is the number to be checked    

  

  m=n/2;  

  System.out.println("M value is ::: "+m);

  

  if(n==0||n==1){  

   System.out.println(n+" is not prime number");      

  }else{  

   for(i=2;i<=m;i++){   

   System.out.println("i value is ::: "+i);

    if(n%i==0){      

     System.out.println(n+" is not prime number");      

     flag=1;      

     break;      

    }      

   }      

   if(flag==0)  { System.out.println(n+" is prime number"); }  

  }//end of else  

    }  

}


Q7.Print Prime numbers Model 1

package practice1;

public class PrimeNumber {

static void prime(int no)

     {

         int count = 0;

        for (int i = 1; i <= no; i++)

         {

             if (no % i == 0)

                 count++;

        }

        if (count == 2)

             System.out.println(no + " is prime");

         else

             System.out.println(no + " is not prime");

    }

     public static void main(String[] args)

    {

         prime(13); // calling

     }

}


Q5.Print Sentence reverse word by word

package practice1;


public class PrintStentenceWordbyWord {


public static void main(String[] args) {


String input="Sanjeevaiah Good Morning";

String[] words = input.split("\\s");

String invertedSentece = "";

for (String word : words){

String invertedWord = "";

for (int i = word.length() - 1; i >= 0; i--)

invertedWord += word.charAt(i);

invertedSentece += invertedWord;

invertedSentece += " ";

}

// invertedSentece.trim();


System.out.println("Inverted Sentence :::  "+invertedSentece);


}

}


Java Oops Concepts

 

Java OOPs Concepts

We will learn about the basics of OOPs. Object-Oriented Programming is a paradigm that provides many concepts, such as inheritancedata bindingpolymorphism, etc.

The programming paradigm where everything is represented as an object is known as a truly object-oriented programming language.

The popular object-oriented languages are JavaC#PHPPythonC++, etc.

The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc.

OOPs (Object-Oriented Programming System)

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Object

Any entity that has state and behavior is known as an object.

An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.

Class

Collection of objects is called class. It is a logical entity.

A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.

Inheritance

When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism

If one task is performed in different ways, it is known as polymorphism.
In Java, we use method overloading and method overriding to achieve polymorphism.

Abstraction

Hiding internal details and showing functionality is known as abstraction
In Java, we use abstract class and interface to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit are known as encapsulation.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

What is the difference between an object-oriented programming language and object-based programming language?

Object-based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object-based programming languages.

Java Naming Convention

All the classes, interfaces, packages, methods and fields of Java programming language are given according to the Java naming convention. If you fail to follow these conventions, it may generate confusion or erroneous code.

Class

It should start with the uppercase letter.

Ex:-  Employee

Interface

It should start with the uppercase letter.

Ex:- Printable

Method

It should start with lowercase letter.

Ex:- method

Variable

It should start with a lowercase letter

Ex:- id

Package

It should be a lowercase letter such as java, lang.

Ex: java

Constant

It should be in uppercase letters such as RED, YELLOW.

Ex:- YELLOW

CamelCase in Java naming conventions

Java follows camel-case syntax for naming the class, interface, method, and variable.

If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName etc.

Objects and Classes in Java

An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical entity only.

What is an object in Java

An entity that has state and behavior is known as an object

An object has three characteristics:
  • State: represents the data (value) of an object.
  • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
  • Identity: An object identity is typically implemented via a unique ID.

An object is an instance of a class. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class.

Object Definitions:

  • An object is a real-world entity.
  • An object is a runtime entity.
  • The object is an entity which has state and behavior.
  • The object is an instance of a class.

What is a class in Java

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface
Ex:

class <Class_Name>{
  
    field;  
    method;  
}