java

Capricorn or Kaprekar Number Program in Java

A number is called Capricorn or Kaprekar number whose square is divided into two parts in any conditions and parts are added, the additions of parts is equal to the number, is called Capricorn or Kaprekar number.

Example: 45

45 =  (45)2 = 2025

2025
All parts for 2025:-
202 + 5 = 207 (not 45)
20 + 25 = 45
2 + 025 = 27 (not 45)

Above we can see one combination is equal to number so that 45 is Capricorn or Kaprekar number.

Selection Sort in String Program in Java

Write a program to input the names of 15 cities sort in descending order using selection sort technique. import java.util.Scanner; public class SelectionSortString { public static void main(String[] args) { String cities[] = new String[15]; Scanner sc = new Scanner(System.in); int l = cities.length; System.out.println(“Enter 15 cities name:”); for (int i = 0; i < […]

Java Constructors

Java constructor is similar to the method. It calls when an object(instance) of a class is created. Allocated in the memory when a constructor is called. It is used to initialize the object of a class. When an object is created using the new keyword, constructor is called. It calls a default constructor if there […]

Java String Function replace()

The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence. Since JDK 1.5, a new replace() method is introduced, allowing you to replace a sequence of char values. Java String replace(char old, char new) method example public class ReplaceExample1{ public static void main(String args[]){ String […]

Java String Function length()

The java string length() method length of the string. It returns count of total number of characters. The length of java string is same as the unicode code units of the string. Java String length() method example public class LengthExample{ public static void main(String args[]){ String s1=”javatpoint”; String s2=”python”; System.out.println(“string length is: “+s1.length());//10 is the […]

Java String Function lastIndexOf()

The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. Java String lastIndexOf() method example public class LastIndexOfExample{ public static void main(String args[]){ String s1=”this is index of example”;//there are 2 ‘s’ characters in this sentence […]

Java String Function join()

The java string join() method returns a string joined with given delimiter. In string join method, delimiter is copied for each elements. In case of null element, “null” is added. The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. Java String join() method […]

Java String Function isEmpty()

The java string isEmpty() method checks if this string is empty or not. It returns true, if length of string is 0 otherwise false. In other words, true is returned if string is empty otherwise it returns false. The isEmpty() method of String class is included in java string since JDK 1.6. Java String isEmpty() […]

Java String Function intern()

The java string intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory, if it is created by new keyword. It creates exact copy of heap string object in string constant pool. Java String intern() method example public class InternExample{ public static void […]

Java String Function getChars()

The java string getChars() method copies the content of this string into specified char array. There are 4 arguments passed in getChars() method. The signature of getChars() method is given below: Java String getChars() method example public class StringGetCharsExample{ public static void main(String args[]){ String str = new String(“hello efaculty how r u”); char[] ch […]

1 2 3 12