Found 9122 Articles for Object Oriented Programming

How to perform heapsort on an array in Java?

Priya Pallavi
Updated on 19-Feb-2020 12:22:49

663 Views

Following is the algorithm for heapsort (maxheap).Step 1 − Create a new node at the end of the heap.Step 2 − Assign new value to the node.Step 3 − Compare the value of this child node with its parent.Step 4 − If the value of parent is less than a child, then swap them.Step 5 − Repeat step 3 & 4 until Heap property holds.Exampleimport java.util.Arrays; import java.util.Scanner; public class Heapsort {    public static void heapSort(int[] myArray, int length) {       int temp;       int size = length-1;       for (int i ... Read More

How to check if array contains three consecutive dates in java?

Nikitha N
Updated on 19-Feb-2020 12:22:03

1K+ Views

To check to find whether a given array contains three consecutive dates:Convert the given array into a list of type LocalDate.Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.ExampleLive Demoimport java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate {    public static void main(String args[]) {       String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"};       List localDateList = new ArrayList();       for (int i = 0; i

What is the lambda expression to convert array/List of String to array/List of Integers in java?

Srinivas Gorla
Updated on 16-Jun-2020 10:12:36

810 Views

You can convert an array list of strings to an array list of integers as:ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class LamdaExpressions {    public static void main(String args[]) {       ArrayList list = new ArrayList();       list.add("123");       list.add("223");       list.add("323");       list.add("334");       List listInteger =          list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());       System.out.println(listInteger);    } }Output[123, 223, 323, 334]

How can I put a Java arrays inside an array?

Abhinanda Shri
Updated on 16-Jun-2020 10:11:04

2K+ Views

ExampleLive Demoimport java.util.Arrays; public class ArrayWithinAnArray{    public static void main(String args[]) {       int[] myArray1 = {23, 56, 78, 91};       int[] myArray2 = {123, 156, 178, 191};       int[] myArray3 = {223, 256, 278, 291};       int[] myArray4 = {323, 356, 378, 391};       int [][] arrayOfArrays = {myArray1, myArray2, myArray3, myArray4};       System.out.println(Arrays.deepToString(arrayOfArrays));    } }Output[[23, 56, 78, 91], [123, 156, 178, 191], [223, 256, 278, 291], [323, 356, 378, 391]]

Why are classes sometimes declared final in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:20

618 Views

If a class is declared final, you cannot inherit it. If you try it gives you a compile-time error as − Example final class Super { private int data = 30; } public class Sub extends Sub { public static void main(String args[]){ } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Sub.main(Sub.java:7)

Are the private variables and private methods of a parent class inherited by the child class in Java?

varma
Updated on 30-Jul-2019 22:30:20

847 Views

No, a child class can’t inherit private members of the parent class, it can inherit only protected, default, and public members of it. If you try it gives you a compile time error as: − Example class Super{ private int data = 30; public void display(){ System.out.println("Hello this is the method of the super class"); } } public class Sub extends Super{ public void greet(){ System.out.println("Hello this is the method of the sub class"); ... Read More

What are Java parent and child classes in Java?

usharani
Updated on 16-Jun-2020 08:47:34

17K+ Views

Java supports inheritance, an OOPs concept where one class acquires the members (methods and fields) of another.You can inherit the members of one class from another, use the extends keyword as:class A extends B{}The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).Following is an example which demonstrates inheritance. Here, we have two classes namely Sample and MyClass. Where Sample is the parent class and the class named MyClass is the child class.ExampleLive Democlass Sample{    public ... Read More

What are static members of a Java class?

varun
Updated on 30-Jul-2019 22:30:20

28K+ Views

In Java, static members are those which belongs to the class and you can access these members without instantiating the class. The static keyword can be used with methods, fields, classes (inner/nested), blocks. Static Methods − You can create a static method by using the keyword static. Static methods can access only static fields, methods. To access static methods there is no need to instantiate the class, you can do it just using the class name as − Example Live Demo public class MyClass { public static void sample(){ ... Read More

How to declare, define and call a method in Java?

Prabhas
Updated on 30-Jul-2019 22:30:20

2K+ Views

Following is the syntax to declare a method in Java. Syntax modifier return_type method_name(parameters_list){ //method body } Where, modifier − It defines the access type of the method and it is optional to use. return_type − Method may return a value. method_name − This is the method name. The method signature consists of the method name and the parameter list. parameters_list − The list of parameters, it is the type, order, and a number of parameters of a method. These are optional, method may contain zero parameters. method body − The method body defines ... Read More

What is the difference between simple name, canonical name and class name in a Java class?

vanithasree
Updated on 30-Jul-2019 22:30:20

3K+ Views

Canonical name of a Java class is the name of the class along with the package. For example, the canonical name of the class File is java.io.File. You can also get the canonical name of a particular class using Java method. The class named Class provides a method getCanonicalName(), this method returns canonical name of the current class. Example Live Demo import java.lang.*; public class ClassDemo { public static void main(String[] args) { ClassDemo c = new ClassDemo(); Class cls = c.getClass(); ... Read More

Advertisements