Found 9321 Articles for Object Oriented Programming

Java program to find common elements in three sorted arrays

Samual Sam
Updated on 25-Jun-2020 07:16:12

955 Views

The common elements in three sorted arrays are the elements that occur in all three of them. An example of this is given as follows −Array1 = 1 3 5 7 9 Array2 = 2 3 6 7 9 Array3 = 1 2 3 4 5 6 7 8 9 Common elements = 3 7 9A program that demonstrates this is given as follows −Examplepublic class Example { public static void main(String args[]) { int arr1[] = {1, 4, 25, 55, 78, 99}; int arr2[] = {2, 3, 4, 34, 55, 68, 75, 78, 100}; int arr3[] = {4, 55, ... Read More

Java program to count occurrences of a word in string

Samual Sam
Updated on 31-May-2024 16:34:38

11K+ Views

The number of times a word occurs in a string denotes its occurrence count. An example of this is given as follows −String = An apple is red in colour. Word = red The word red occurs 1 time in the above string.A program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String args[]) { String string = "Spring is beautiful but so is winter"; String word = "is"; String temp[] = string.split(" "); int count = 0; for (int i = 0; i < temp.length; i++) { if (word.equals(temp[i])) count++; } System.out.println("The string ... Read More

Java program to extract ‘k’ bits from a given position

karthikeya Boyini
Updated on 25-Jun-2020 10:04:23

399 Views

Extraction of k bits from the given position in a number involves converting the number into its binary representation. An example of this is given as follows −Number = 20 Binary representation = 10100 k = 3 Position = 2 The bits extracted are 010 which represent 2.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int number = 20, k = 3, pos = 2;       int exNum = ((1 > (pos - 1));       System.out.println("Extract " + k + ... Read More

Java program to check if two given matrices are identical

Samual Sam
Updated on 26-Jun-2020 05:52:55

1K+ Views

Two matrices are identical if their number of rows and columns are equal and the corresponding elements are also equal. An example of this is given as follows.Matrix A = 1 2 3 4 5 6 7 8 9 Matrix B = 1 2 3 4 5 6 7 8 9 The matrices A and B are identicalA program that checks if two matrices are identical is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} };   ... Read More

Java program to count words in a given string

karthikeya Boyini
Updated on 31-May-2024 12:58:51

13K+ Views

A string is a class in Java that stores a series of characters enclosed within double quotes. Those characters act as String-type objects. The aim of this article is to write Java programs that count words in a given string. Counting words of the given strings can be solved using the string manipulation techniques. In Java interviews, questions from string manipulation are very frequent, hence, it is important to understand this problem properly. Java Program to Count Words in a given String Before jumping to the example programs, let's understand the problem statement with the help of an example. Input ... Read More

Swap two variables in one line in using Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

5K+ Views

Two variables can be swapped in one line in Java. This is done by using the given statement. x = x ^ y ^ (y = x); where x and y are the 2 variables. A program that demonstrates this is given as follows − Example Live Demo public class Example { public static void main (String[] args) { int x = 12, y = 25; System.out.println("Original values of x and y"); System.out.println("x = " ... Read More

Java program to count total bits in a number

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

672 Views

The total bits in a number can be counted by using its binary representation. An example of this is given as follows − Number = 9 Binary representation = 1001 Total bits = 4 A program that demonstrates this is given as follows. Example Live Demo public class Example { public static void main(String[] arg) { int num = 10; int n = num; int count = 0; while ... Read More

Java program to display Astrological sign or Zodiac sign for given date of birth

karthikeya Boyini
Updated on 26-Jun-2020 05:42:05

6K+ Views

Each date of birth corresponds to a given Zodiac sign. A table that demonstrates these signs and their corresponding dates is given below −Zodiac SignDateAriesMarch 21 - April 19TaurusApril 20 - May 20GeminiMay 21 - June 20CancerJune 21 - July 22LeoJuly 23 - August 22VirgoAugust 23 - September 22LibraSeptember 23 - October 22ScorpioOctober 23 - November 21SagittariusNovember 22 - December 21CapricornDecember 22 - January 19AquariusJanuary 20 - February 18PiscesFebruary 19 - March 20A program that displays the Astrological sign or Zodiac sign for a given date of birth is given as follows.Example Live Demopublic class Example {    public static void ... Read More

Define integer literals as octal values in Java

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless. Here is the declaration and initialization. int myOct = 023; Example Live Demo public class Demo { public static void main(String []args) { int myOct = 023; System.out.println(myOct); } } Output 19 Let us see another example. Example Live Demo public class Demo { public static void main(String []args) { int myOct = 010; System.out.println(myOct); } } Output 8

Hexadecimal literal of type long in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

890 Views

Hexadecimal literal of type long is represented as − long hexLong = 0XABL; For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal Example Live Demo public class Demo { public static void main(String []args) { long hexLong = 0XABL; System.out.println("Hexadecimal literal of type long: "+hexLong); } } Output Hexadecimal literal of type long:171

Advertisements