Found 2616 Articles for Java

Java Program to Add the data from the Specified Collection in the Current Collection

Shriansh Kumar
Updated on 25-Apr-2023 18:05:13

184 Views

In java, collection is an object or we can say a container for simplicity that allows us to group several numbers of objects in a single unit. The collection interface is present at the root of all collection framework interfaces. We can perform various operations on collection like adding, removing, iterating, searching and retrieving objects. In this article, we will discuss a java program to add data from the specified collection in the current collection with the help of addAll() and add() method. addAll() − It add data from a specified collection into current collection. Its return type is ... Read More

Java Program to Add the nth Square Series

Shriansh Kumar
Updated on 25-Apr-2023 18:04:40

129 Views

Sum of nth squares series is a form of arithmetic progression where sum of squares is arranged in a sequence with initial term as 1, and n being the total number of terms. 12 + 22 + 32 + ….. + n2 In this article, we will discuss java program to add the nth square series. Approach 1: Using Iterative approach Let’s understand the logic for program with a simple example. Example If we take input as num = 6 It will be calculated as: 12 + 22 + 32 + 42 + 52 + 62 = ... Read More

Java Program to Allocate and Initialize Super Class Members using Constructor

Shriansh Kumar
Updated on 25-Apr-2023 18:04:07

262 Views

In java, super refers to the parent class and to inherit one class to another class we use extends keyword. Before making a java program to allocate and initialize super class members using constructor, let’s go through some concepts we are going to use in this article. What is Constructor? Constructor is very similar to methods but the difference is that methods define the behavior of an object but constructor is used to initializing those objects. We can provide any name of our choice to methods but a constructor must have same name as class name. Also, methods ... Read More

Java Program to Add two Numbers without using Arithmetic Operator

Shriansh Kumar
Updated on 25-Apr-2023 18:03:03

2K+ Views

Arithmetic operators such as +, -, *, / are used to perform mathematical operations like addition, subtraction, multiplication, modulo and division. We all have done addition of two numbers using + operator but in this article, we are going to see a few java programs that can add two numbers without using arithmetic operators. The following operators best serve this purpose − Increment operators (++) − They are used to increment value of a variable by 1. It is of two types pre increment and post increment. Pre increment operator first increases the value and then, uses it for ... Read More

Java Program to Calculate difference between two Time Periods

Shriansh Kumar
Updated on 25-Apr-2023 18:02:07

484 Views

To work with date and time in java, we need to import java.time, java.util and java.text packages. We are going to use the following classes and methods provided by these packages to calculate difference between two time periods in java − SimpleDateFormat class Date class LocalDate class Period class parse() method between() method As we move further in this article, we will understand the uses of these classes and methods. Approach 1: Using SimpleDateFormat and Date class SimpleDateFormat − It is a class in java that allows us to convert date to string (formatting) and convert string to ... Read More

Java Program to Calculate Sum of Two Byte Values Using Type Casting

Shriansh Kumar
Updated on 25-Apr-2023 17:38:17

341 Views

When we convert a data type to another data type, we call it as type casting. There are two types of type casting in java explicit and implicit. When we type cast a higher datatype to a lower data type then, it is called explicit type casting and it is done manually. In it, there is a risk of losing data because lower datatype has range smaller than a higher datatype, this is the main reason of doing it manually. Since, byte is the lowest datatype available in java, we need to type cast it into a higher data type. ... Read More

Java Program to Categorize Taller, Dwarf and Average by Height of a Person

Shriansh Kumar
Updated on 25-Apr-2023 17:37:05

228 Views

First, we need to define taller, dwarf and average height of person. A person with height between 170cm to 195cm is considered as taller, height in the range of 150cm to 170cm is considered as average, person whose height is below 150cm is considered as dwarf and person whose height is greater than 195cm is abnormal. Now let’s jump into the java program to categorize taller, dwarf and average by height of a person using if else if condition. Approach 1: Using if else if condition Generally, we use if else if statements when we have to check ... Read More

Java Program to Add Characters to a String

Shriansh Kumar
Updated on 31-May-2024 14:39:39

6K+ Views

Java provides different ways to add characters to a string. We can add characters in the beginning or at the end or at any position according to the need. In this article we are going to see three approaches to do so − Using ‘+’ operator Using methods of StringBuffer and StringBuilder class Using substring() method Approach 1: Using ‘+’ Operator In this section, we will add a character to a string using the concatenation operator. Example public class Main { public static void main(String[] args) { String ... Read More

How to use Weka Java API in ML

Jay Singh
Updated on 25-Apr-2023 13:47:48

1K+ Views

The Weka Java API is a potent machine-learning tool that makes it easy for programmers to incorporate Weka algorithms into Java applications. Complicated machine-learning models can be easily constructed using the Weka Java API's strong built-in data preparation, classification, regression, clustering, and visualization features. Weka includes a wide range of preprocessing methods, including normalization, discretization, and feature selection, and supports a number of file formats, including CSV, ARFF, and C4.5. Only a handful of the machine-learning methods offered by Weka include neural networks, SVMs, decision trees, and random forests. Developers can quickly train and assess machine learning models, as well ... Read More

Java program to check whether the number is divisible by 5

Tapas Kumar Ghosh
Updated on 20-Apr-2023 12:02:43

3K+ Views

In mathematics, the divisibility rule of 5 states that if the number ends with 0 or 5 then it will divisible by 5. There is another way to identify the divisibility rule of 5, if the remainder comes to 0 it returns the number is divisible by 5. The mod(%) operator is normally used in programming when it comes to divisibility. Let’s take an example of this. Given number is 525, the number ends with 5 and it is divisible by 5. Given number is 7050, the number ends with 0 and it is divisible by 5. Given number ... Read More

Advertisements