Day: April 25, 2021

Learn Java by Example: Java Program to Remove All Whitespaces from a String

Java Program to Remove All Whitespaces from a String In this program, you’ll learn to remove all whitespaces in a given string using regular expressions in Java.   Example 1: Program to Remove All Whitespaces public class Whitespaces{ public static void main(String[] args){ String sentence = “T his is b ett er.”; System.out.println(“Original sentence: ” …

Learn Java by Example: Java Program to Calculate Difference Between Two Time Periods

Java Program to Calculate Difference Between Two Time Periods In this program, you’ll learn to calculate the difference between two time periods in Java.   Example: Calculate Difference Between Two Time Periods public class Time{ int seconds; int minutes; int hours; public Time(int hours, int minutes, int seconds){ this.hours = hours; this.minutes = minutes; this.seconds …

Learn Java by Example: Java Program to Add Two Complex Numbers by Passing Class to a Function

Java Program to Add Two Complex Numbers by Passing Class to a Function In this program, you’ll learn to add two complex numbers in Java by creating a class named Complex and passing it into a function add().   Example: Add Two Complex Numbers public class Complex{ double real; double imag; public Complex(double real, double …

Learn Java by Example: Java Program to Sort Elements in Lexicographical Order

Java Program to Sort Elements in Lexicographical Order (Dictionary Order) In this program, you’ll learn to sort the element words in lexicographical order using a for loop and if else in Java.   Example: Program to Sort Strings in Dictionary Order public class Sort{ public static void main(String[] args){ String[] words = { “Ruby”, “C”, …

Learn Java by Example: Java Program to Count the Number of Vowels and Consonants in a Sentence

Java Program to Count the Number of Vowels and Consonants in a Sentence In this program, you’ll learn to count the number of vowels, consonants, digits and spaces in a given sentence using if else in Java.   Example: Program to count vowels, consonants, digits, and spaces public class Count{ public static void main(String[] args){ …

Learn Java by Example: Java Program to Find the Frequency of Character in a String

Java Program to Find the Frequency of Character in a String In this program, you’ll learn to find the occurence (frequency) of a character in a given string.   Example: Find Frequency of Character public class Frequency{ public static void main(String[] args){ String str = “This website is awesome.”; char ch = ‘e’; int frequency …

Learn Java by Example: Java Program to Multiply two Matrices by Passing Matrix to a Function

Java Program to Multiply two Matrices by Passing Matrix to a Function In this program, you’ll learn to multiply two matrices using a function in Java. For matrix multiplication to take place, the number of columns of the first matrix must be equal to the number of rows of the second matrix. In our example, …