Found 2617 Articles for Java

Find the Nth Ugly Number in Java

Dev Prakash Sharma
Updated on 23-Feb-2021 18:37:00

835 Views

A number whose prime factors are either 2, 3 or 5 is called an Ugly Number.  Some of the ugly numbers are: 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, etc.We have a number N and the task is to find the Nth Ugly number in the sequence of Ugly numbers.For Example:Input-1:N = 5Output:5Explanation:The 5th ugly number in the sequence of ugly numbers [1, 2, 3, 4, 5, 6, 8, 10, 12, 15] is 5.Input-2:N = 7Output:8Explanation:The 7th ugly number in the sequence of ugly numbers [1, 2, 3, 4, 5, 6, 8, 10, 12, 15] is 8.Approach ... Read More

Find the Intersection Point of Two Linked Lists in Java

Dev Prakash Sharma
Updated on 23-Feb-2021 18:42:34

514 Views

A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to the other nodes in the list. The task is to find the node at which two linked lists intersect each other. If they don’t intersect, then return NULL or empty as output.For ExampleInput-1:Output:2Explanation: Since the given linked list intersects at the node with ... Read More

Can we override default methods in Java?

Maruthi Krishna
Updated on 08-Feb-2021 12:42:05

6K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.ExampleLive Demointerface MyInterface{      public static int num = 100;    public default ... Read More

How to solve diamond problem using default methods in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:53:31

11K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritance is where one class inherits the properties of multiple classes. In other words, ... Read More

How to convert a super class variable into a sub class type in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:52:56

9K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a super class reference variable into a sub class typeYou can try to convert ... Read More

How to convert a sub class variable into a super class type in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:52:37

1K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a sub class variable into a super class typeYou can directly assign a sub ... Read More

Explain the usage of the valueOf() method of the String class in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:36:43

117 Views

The String class of the java.lang package represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.The valueOf() method of the String class accepts a char or, char array or, double or, float or, int or, long or an object as a parameter and returns its String representation.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       ... Read More

How to convert a double value into a Java String using append method?

Maruthi Krishna
Updated on 08-Feb-2021 11:36:26

190 Views

The append method of the StringBuilder or StringBuffer objects accept a boolean or, char or, char array or, double or, float or, int or, long or, String value as parameter and adds it to the current object.You can append the required double value to the method and retrieve a String from obtained StringBuffer (or, StringBuilder) objects.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       double d = sc.nextDouble();       StringBuffer sb = new StringBuffer();       sb.append(d); ... Read More

How to iterate the values of an enum using a for loop in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:26:53

327 Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.Examplepublic class IterateEnum{    public static void main(String args[]) {       Days days[] = Days.values();       System.out.println("Contents of the enum are: ");             //Iterating enum using the for loop ... Read More

Can we define an enum inside a method in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:35:52

2K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.Examplepublic class EnumExample{    public void sample() {       enum Vehicles {          Activa125, Activa5G, Access125, Vespa, TVSJupiter;       }    } }ErrorEnumExample.java:3: error: enum types must not be local ... Read More

Advertisements