Found 9321 Articles for Object Oriented Programming

Java Program to Concatenate Two List

Bharti Kumari
Updated on 31-May-2024 16:15:26

6K+ Views

Introduction In Java, a list is an ordered collection of elements that allows duplicates. Sometimes, we may need to combine two lists into one list. Concatenating two lists means joining the elements of the two lists to form a new list containing all the elements of both lists. There are various ways to concatenate two lists in Java, including using loops, streams, and built-in methods. In this context, we will explore different approaches to concatenate two lists in Java. One approach is to use the addAll() method provided by the List interface. This method adds all the elements of one ... Read More

Java Program to Compute the Sum of Numbers in a List Using While-Loop

Bharti Kumari
Updated on 10-Apr-2023 15:59:29

462 Views

Introduction The Java program to compute the sum of numbers in a list using a while-loop is a simple program that takes a list of integers and computes their sum using a while-loop construct. In this program, an ArrayList of integers is created, and a few numbers are added to the list. The program then uses a while-loop to iterate over each element in the list, adding each element to a variable "sum", which keeps track of the running sum of the numbers. Once the loop has finished, the final value of "sum" is printed to the console, which is ... Read More

Java Program to Compute the Sum of Numbers in a List Using For-Loop

Bharti Kumari
Updated on 10-Apr-2023 15:58:44

2K+ Views

Introduction Java is a popular programming language that is used for developing a wide range of applications, including those that involve working with lists of numbers. One common task is to compute the sum of numbers in a list, which can be accomplished using a for-loop. In this approach, we first create a list of numbers, then initialize a variable to hold the sum. We then use a for-loop to iterate over each element in the list, adding it to the sum variable. After the loop has completed, the sum variable contains the total sum of all the numbers in ... Read More

Java Program to Compute the Running Total of a List

Bharti Kumari
Updated on 10-Apr-2023 15:58:05

604 Views

Introduction The Java program to compute the running total of a list is a simple program that demonstrates the use of loops and lists in Java. The program takes a list of integers as input and computes the running total of the numbers in the list, which is the cumulative sum of all the numbers up to a particular point. The program initializes a variable called runningTotal to 0 and then loops through the list of numbers using a for loop. For each number in the list, the program adds it to the running total variable and prints out the ... Read More

Java Program to Compute the Area of a Triangle Using Determinants

Bharti Kumari
Updated on 10-Apr-2023 15:56:57

164 Views

Introduction The Java program to compute the area of a triangle using determinants is a concise and efficient program that calculates the area of a triangle given the coordinates of its three vertices. This program is useful for anyone studying or working with geometry, as it demonstrates how to use basic arithmetic and algebraic calculations in Java, and how to read in user input using the Scanner class. The program prompts the user to enter the coordinates of three points of the triangle, which are then read in and used to calculate the determinant of the matrix of coordinates. The ... Read More

Java Program to Compare Two Objects

Bharti Kumari
Updated on 14-Jul-2023 17:26:05

367 Views

Introduction In Java, objects can be compared using the equals() method, which checks whether two objects are equal based on their properties. When comparing objects in Java, it is important to override the equals() method in the class to ensure that the comparison is done based on the desired properties. This Java program compares two objects of type Person by overriding the equals() method to compare the objects based on their name and age properties. The equals() method is first used to check if the objects are of the same class and then compares the name and age properties. If ... Read More

Java Program to Combine Two List by Alternatively Taking Elements

Bharti Kumari
Updated on 10-Apr-2023 15:54:37

907 Views

Introduction The Java program to combine two lists by alternatively taking elements is a simple code snippet that takes in two lists of any type of object that can be stored in a list and returns a new list that contains elements from both input lists alternately. The program defines a method called alternateMerge that takes two lists as input and returns a new list that contains elements from both lists alternately. It determines the size of both lists and loops through the larger of the two lists. For each index in the loop, it adds the element from the ... Read More

Java Program to Check the Accessibility of an Static Variable By a Static Method

Bharti Kumari
Updated on 10-Apr-2023 15:50:34

222 Views

Introduction In Java, we can define variables and methods as static. A static variable or method belongs to the class itself rather than to the individual objects of the class. Therefore, we can access a static variable or method using the class name, without creating an object of the class. In this program, we will explore how to check the accessibility of a static variable by a static method. We will define a class with a static variable and a static method that accesses the variable. We will then call the static method to check if it can access the ... Read More

Java Program to Check if two Arrays are Equal or not

Bharti Kumari
Updated on 10-Apr-2023 15:48:30

2K+ Views

Introduction In Java, we can check if two arrays are equal or not by comparing their elements. If the elements in both arrays are the same and appear in the same order, then the two arrays are considered equal. One way to check the equality of two arrays is to use the Arrays.equals method provided by the java.util package. This method takes two arrays as arguments and returns a boolean value indicating whether they are equal or not. The method compares the elements of the arrays in the same order, so if the order of the elements is not important, ... Read More

Java vs C#

Rudradev Das
Updated on 10-Apr-2023 15:59:23

213 Views

Java is a dynamic, secured and class based high level object oriented programming language developed by Oracle Corporation. On the other hand; C# is a .Net Framework object oriented programming language developed by Microsoft. Java and C# both are the general purpose programming paradigm or basically known as the imperative environment for coding. And these two languages are capable to provide some high level results as output. In a broad view there are many differences between these two OOPs − Java Runtime Environment designed to run a Java code where C# runs on CLR Environment (Common Language Runtime). Java ... Read More

Advertisements