Found 9326 Articles for Object Oriented Programming

Merge two sorted arrays in Java

Samual Sam
Updated on 25-Jun-2020 14:09:13

8K+ Views

Two sorted arrays can be merged so that a single resultant sorted array is obtained. An example of this is given as follows.Array 1 = 1 3 7 9 10 Array 2 = 2 5 8 Merged array = 1 2 3 5 7 8 9 10A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int[] arr1 = {11, 34, 66, 75};       int n1 = arr1.length;       int[] arr2 = {1, 5, 19, 50, 89, 100};       int ... Read More

Reverse words in a given String in Java

karthikeya Boyini
Updated on 25-Jun-2020 14:16:25

4K+ Views

The order of the words in a string can be reversed and the string displayed with the words in reverse order. An example of this is given as follows.String = I love mangoes Reversed string = mangoes love IA program that demonstrates this is given as follows.Example Live Demoimport java.util.regex.Pattern; public class Example {    public static void main(String[] args) {       String str = "the sky is blue";       Pattern p = Pattern.compile("\s");       System.out.println("The original string is: " + str);       String[] temp = p.split(str);       String rev = ... Read More

Java program to calculate Body Mass Index (BMI)

Samual Sam
Updated on 31-May-2024 13:04:55

21K+ Views

The Body Mass Index is the body mass in kilogram divided by the square of body height in meters. This is expressed as kg/m^2.A Java program that calculates the Body Mass Index (BMI) is given as follows.Exampleimport java.util.Scanner; public class Example {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.print("Input weight in kilogram: ");       double weight = sc.nextDouble();       System.out.print("Input height in meters: ");       double height = sc.nextDouble();       double BMI = weight / (height * height);     ... Read More

Java program to check if a Substring is present in a given String

karthikeya Boyini
Updated on 25-Jun-2020 14:23:59

941 Views

A substring is a part of a string and it can be checked if a particular substring is present in the given string or not. An example of this is given as follows −String = The sunset is beautiful Substring = sunsetThis substring is present in the string.A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String args[]) {       String str = "Apples are red";       String substr = "are";       int n1 = str.length();       int n2 = substr.length();       System.out.println("String: " + str);       System.out.println("Substring: " + substr);       for (int i = 0; i

Java Program to check whether the character is ASCII 7 bit printable

Samual Sam
Updated on 25-Jun-2020 13:33:17

230 Views

To check whether the entered value is ASCII 7-bit printable, check whether the characters ASCII value is greater than equal to 32 and less than 127 or not. These are the control characters.Here, we have a character.char one = '^';Now, we have checked a condition with if-else for printable characters.if (c >= 32 && c < 127) {    System.out.println("Given value is printable!"); } else {    System.out.println("Given value is not printable!"); }Example Live Demopublic class Demo {    public static void main(String []args) {       char c = '^';       System.out.println("Given value = "+c);     ... Read More

Java Program to check whether the character is ASCII 7 bit numeric

karthikeya Boyini
Updated on 25-Jun-2020 13:38:30

139 Views

To check whether the entered value is ASCII 7-bit numeric, check the character from ‘0’ to ‘9’.Here, we have a numeric character.char one = '9';Now, we have checked a condition with if-else for numeric character from ‘0’ to ‘9’if (c >= '0' && c = '0' && c = '0' && c = '0' && c

Java Program to check whether the entered character is ASCII 7 bit numeric and character

Samual Sam
Updated on 25-Jun-2020 13:44:38

95 Views

To check whether the entered value is ASCII 7-bit numeric and character (alphanumeric), check the characters ASCII value for −A to Z Or a to z Or 0 to 9Here, we have the following value −char one = '5';Now, we have checked some conditions with if-else for ASCII 7-bit numeric and character.if ((one >= 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one = 'A' && one = 'a' && one = '0' && one

Java Program to subtract week from current date

Samual Sam
Updated on 25-Jun-2020 13:50:00

477 Views

Firstly, you need to import the following package for Calendar class in Java.import java.util.Calendar;Create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the weeks using the calendar.add() method and Calendar.WEEK_OF_YEAR constant. Set a negative value since we are decrementing here.calendar.add(Calendar.WEEK_OF_YEAR, -2);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 2 weeks       calendar.add(Calendar.WEEK_OF_YEAR, -2);     ... Read More

Java program to print all distinct elements of a given integer array in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:51:31

5K+ Views

All distinct elements of an array are printed i.e. all the elements in the array are printed only once and duplicate elements are not printed. An example of this is given as follows.Array = 1 5 9 1 4 9 6 5 9 7 Distinct elements of above array = 1 5 9 4 6 7A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       int arr[] = {1, 5, 9, 1, 4, 9, 6, 5, 9, 7};       int n = arr.length;   ... Read More

Java program to program to cyclically rotate an array by one

Samual Sam
Updated on 25-Jun-2020 13:18:13

866 Views

The array is cyclically rotated clockwise by one. This means that each of the array elements are displayed to the right by one and the last element ends up as the first element. An example of this is given as follows.Original array = 1 2 3 4 5 6 7 8 9 10 Rotated array = 10 1 2 3 4 5 6 7 8 9A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) {       int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, ... Read More

Advertisements