Found 9326 Articles for Object Oriented Programming

Use a quantifier to find a match in Java

Arushi
Updated on 30-Jul-2019 22:30:24

62 Views

One of the quantifiers is the plus(+). This matches one or more of the subsequence specified with the sequence.A program that demonstrates using the quantifier plus(+) to find a match in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("o+");       Matcher m = p.matcher("o oo ooo");       System.out.println("The input string is: o oo ooo");       System.out.println("The Regex is: o+ ");       System.out.println();       while (m.find()) {          System.out.println("Match: ... Read More

Use find() to find multiple subsequences in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

93 Views

The find() method finds the multiple subsequences in an input sequence that matches the pattern required. This method is available in the Matcher class that is available in the java.util.regex packageA program that uses the find() method to find multiple subsequence in Java is given as follows:Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("the");       Matcher m = p.matcher("eclipses of the sun and the moon");       System.out.println("Subsequence: the");       System.out.println("Sequence: eclipses of the sun and the moon");   ... Read More

Why Final Class used in Java?

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

757 Views

The final keyword is used with a class to create a final class. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.A program that demonstrates a final class in Java is given as follows:Example Live Demofinal class A {    private int a = 9;    public void printA() {       System.out.println("Value of a = " + a);    } } public class Demo {    public static void main(String args[]) {       A obj = new A();       obj.printA();    } }OutputValue of ... Read More

Why Protected access modifier used in Java?

Rishi Raj
Updated on 30-Jul-2019 22:30:24

171 Views

The data members and methods of a class can be accessed from the same package or sub-classes in a different package if they are specified with the protected access modifier. The keyword protected is used to specify this modifier.A program that demonstrates the protected access modifier in Java is given as follows:Example Live Democlass A {    protected int a = 9;    public void printA() {       System.out.println("Value of a = " + a);    } } public class Demo {    public static void main(String args[]) {       A obj = new A();     ... Read More

Demonstrate Private access modifier in Java

Arushi
Updated on 30-Jul-2019 22:30:24

128 Views

The data members and methods of a class can be accessed only from within their declared class if they are specified with the private access modifier. The keyword private is used to specify this modifier.A program that demonstrates the private access modifier in Java is given as follows:Example Live Democlass A {    private int a = 6;    public void printA() {       System.out.println("Value of a = " + a);    } } public class Demo {    public static void main(String args[]) {       A obj = new A();       obj.printA();    } ... Read More

Recursive factorial method in Java

Rishi Raj
Updated on 30-Jul-2019 22:30:24

4K+ Views

The factorial of any non-negative integer is basically the product of all the integers that are smaller than or equal to it. The factorial can be obtained using a recursive method.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static long fact(long n) {       if (n

Raise x to the power n using Recursion in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:24

605 Views

The power of a number can be calculated using recursion. Here the number is x and it is raised to power n.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    static double pow(double x, int n) {       if (n != 0)          return (x * pow(x, n - 1));       else          return 1;    }    public static void main(String[] args) {       System.out.println("7 to the power 3 is " + pow(7, 3));       System.out.println("4 to the power 1 ... Read More

Zip Code validation using Java Regular Expressions

Arushi
Updated on 30-Jul-2019 22:30:24

1K+ Views

The zip code can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the zip code and the given input zip code and returns true if they match and false otherwise.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static void main(String args[]) {       String zipCode = "83592-1537";       String regex = "\d{5}(-\d{4})?";       System.out.println("The zip code is: " + zipCode);       System.out.println("Is the above zip code valid? " + zipCode.matches(regex));    } }OutputThe zip code is: 83592-1537 Is the above ... Read More

Phone Number validation using Java Regular Expressions

Jai Janardhan
Updated on 30-Jul-2019 22:30:24

3K+ Views

The phone number can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the phone number and the given input phone number and returns true if they match and false otherwise.Note: We are considering a demo number for our example since we cannot use our phone number publicly.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static void main(String args[]) {       String phoneNumber = "9999999998";       String regex = "(0/91)?[7-9][0-9]{9}";       System.out.println("The phone number is: " + phoneNumber);       System.out.println("Is the ... Read More

Why do we use import statement in Java? Where it should be included in the class?

Rishi Raj
Updated on 30-Jul-2019 22:30:24

3K+ Views

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.A program that demonstrates this in Java is given as follows:Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Apple");       l.add("Mango");       l.add("Cherry");       l.add("Orange");       l.add("Pear");       System.out.println("The ... Read More

Advertisements