HackerRank Java

Covariant Return Types – Hacker Rank Solution

Objective Java allows for Covariant Return Types, which means you can vary your return type as long you are returning a subclass of your specified return type. Method Overriding allows a subclass to override the behavior of an existing superclass method and specify a return type that is some subclass of the original return type. It is best practice …

Covariant Return Types – Hacker Rank Solution Read More »

Java Annotations – Hacker Rank Solution

Objective Java annotation can be used to define the metadata of a Java class or class element. We can use Java annotation at the compile time to instruct the compiler about the build process. Annotation is also used at runtime to get insight into the properties of class elements. Java annotation can be added to …

Java Annotations – Hacker Rank Solution Read More »

Java Visitor Pattern – Hacker Rank Solution

Objective Note: In this problem you must NOT generate any output on your own. Any such solution will be considered as being against the rules and its author will be disqualified. The output of your solution must be generated by the uneditable code provided for you in the solution template. An important concept in Object-Oriented Programming is the open/closed …

Java Visitor Pattern – Hacker Rank Solution Read More »

Java Singleton Pattern – Hacker Rank Solution

Objective “The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.”– Wikipedia: Singleton Pattern Complete the Singleton class in your editor which contains the following components: A private Singleton non parameterized constructor. A public String instance variable named . Write a static method …

Java Singleton Pattern – Hacker Rank Solution Read More »

Java Factory Pattern – Hacker Rank Solution

Objective According to Wikipedia, a factory is simply an object that returns another object from some other method call, which is assumed to be “new”. In this problem, you are given an interface Food. There are two classes Pizza and Cake which implement the Food interface, and they both contain a method getType(). The main function in the Main class creates an instance of the FoodFactory class. …

Java Factory Pattern – Hacker Rank Solution Read More »

Prime Checker – Hacker Rank Solution

Objective You are given a class Solution and its main method in the editor. Your task is to create a class Prime. The class Prime should contain a single method checkPrime. The locked code in the editor will call the checkPrime method with one or more integer arguments. You should write the checkPrime method in such a way that the code prints only the prime numbers. Please read …

Prime Checker – Hacker Rank Solution Read More »