Skip to main content

Posts

Showing posts from June, 2021

Java Interface HackerRank Solution with Explanation

For solve this problem you first you need to understand about Interface in Java. What is Interface in Java? A Java interface can only contain method signatures and fields. The interface can be used to achieve polymorphism. Interface in Java with examples   Problem Description : You are given an interface AdvancedArithmetic which contains a method signature int divisor_sum(int n) . You need to write a class called MyCalculator which implements the interface. divisor_sum function just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1, 2, 3 and 6, so divisor_sum should return 12. The value of n will be at most 1000. Read the partially completed code in the editor and complete it. You just need to write the MyCalculator class only. See full problem description : https://www.hackerrank.com/challenges/java-interface/problem   Lets see solution now. Solution explanation : In code editor, we already have AdvancedArithmetic Interface and di

Java Abstract Class HackerRank Solution | Programming Blog

  What is Abstract Class in Java? A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. So if any other class extends abstract class then it must provide implementation of abstract method of that class otherwise you can also make child class as abstract.  Learn more about Abstract class : Abstract class in Java   See problem Description in HackeRank : Java Abstract Class Problem   Solution Explanation : In code editor, we already have Book class and Main method. We have to simply create new class MyBook that extends Book class.  As setTitle method is abstract in Book class, we must have to implement in our MyBook class if we extends Book class. So create method setTitle in MyBook class and set the title variable. And our solution is done. import java.util.*; abstract class Book{     String title;     abstract void setTitle(String s);     String getTitle(){         return tit

Java Inheritance II HackerRank solution with explanation

Problem Description : Write the following code in your editor below: A class named Arithmetic with a method named add that takes 2 integers as parameters and returns an integer denoting their sum. A class named Adder that inherits from a superclass named Arithmetic .   Input Format : You are not responsible for reading any input from stdin; a locked code stub will test your submission by calling the add method on an Adder object and passing it 2 integer parameters. Output Format : You are not responsible for printing anything to stdout. Your add method must return the sum of its parameters.   See full Problem Description : Java Inheritance 2 Problem Description Solution Explanation : In this problem, simply you have to create two new class : Arithmetic and Adder. Arithmetic class contains only one method : add. add method have two integer parameters and return addition of two parameters with int value. Adder class extends Arithmetic class and does not contains own method. Lets se

How to Reverse String using Recursion in Java | LeetCode Solution

Reverse Java String using Recursion technique Problem Description : Write a function that reverses a string. The input string is given as an array of characters s. Example 1 : Input: s = ['h', 'e', 'l', 'l', 'o'] Output: ['o','l','l','e','h] Example 2 : Input: s = ['H', 'a', 'n', 'n', 'a', 'h'] Output: ['h', 'a', 'n', 'n', 'a', 'H'] Lets see how we can solve this problem using recursion. Lets first understand about recursion : What is Recursion? Recursion is an approach to solving problems using a function that calls itself as a subroutine. Means we are calling same function over and over again until our base condition does not satisfy. Each time a recursive function calls itself, it reduces the given problem into subproblems. The recursion call continues until it reaches a point where the subproblem can be sol

Special String Again HackerRank Solution in Java

Java Solution for Special String Again HackerRank Probelm A string is said to be a special string if either of two conditions is met: All of the characters are the same, e.g. aaa . All characters except the middle one are the same, e.g. aadaa . A special substring is any substring of a string which meets one of those criteria. Given a string, determine how many special substrings can be formed from it. Example : str = mnonopoo str contains the following 12 special substrings {m, n, o, n, o, p, o, o, non, ono, opo, oo} See full description :   HackerRank Lets see solution and its Explanation Solution 1 : import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     // Complete the substrCount function below.     static long substrCount(int n, String str) {         for (int i = 0; i < str.length(); i++) {             char iIndexChar = str.charAt(i);   

How to convert JSONObject and JSONArray into Java List | JSONArray and JSONObject

Convert JSON Object data into Java List and Display accordingly | Convert String JSONObject and JSONArray data into List in Java Often times we need to call Third party API and we get JSON response, and want to display data into web page. Sometimes we got complex JSON response and difficult to convert into Java data structure. First learn how to call third party rest API in Java.   How to call 3rd party API in Java So lets see how we can easily convert JSON data into our Java data structure. Following sample JSON data. { "ProgrammingList": [ { "id": "1", "name": "Java", "edition": "third" }, { "id": "2", "name": "Python", "edition": "fourth" }, { "id": "3", "name": "JavaScript", "edition": "Fifth" }, { "id