0% found this document useful (0 votes)
2K views4 pages

HackerRank Solutions

This document contains summaries of and links to various Java challenges on HackerRank including Java exception handling using try-catch, the Java factory pattern, method overriding, hashsets, and comparators. It provides code snippets and summaries for challenges involving dividing numbers and handling different exception types, implementing a calculator class with exception handling, using a factory pattern to return different object types based on an order, overriding a method to print team details, adding string pairs to a hashset to find the unique pairs, and implementing a comparator to sort players by score then name.

Uploaded by

Steven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views4 pages

HackerRank Solutions

This document contains summaries of and links to various Java challenges on HackerRank including Java exception handling using try-catch, the Java factory pattern, method overriding, hashsets, and comparators. It provides code snippets and summaries for challenges involving dividing numbers and handling different exception types, implementing a calculator class with exception handling, using a factory pattern to return different object types based on an order, overriding a method to print team details, adding string pairs to a hashset to find the unique pairs, and implementing a comparator to sort players by score then name.

Uploaded by

Steven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Contenido

Java Exception Handling (Try-catch)...................................................................................................2

Java Exception Handling.....................................................................................................................2

Java Factory Pattern...........................................................................................................................3

Java Method Overriding.....................................................................................................................3

Java Hashset.......................................................................................................................................3

Java Comparator.................................................................................................................................3
Java Exception Handling (Try-catch)
https://www.hackerrank.com/challenges/java-exception-handling-try-catch/problem

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {


public static void main(String[] args) {
try{
Scanner scan=new Scanner(System.in);
int valA= scan.nextInt();
int valB=scan.nextInt();
System.out.println((valA/valB));
}catch(InputMismatchException e){
System.out.println(e.getClass().getName());
}catch(ArithmeticException e){
System.out.println(e);
}
}
}

Java Exception Handling


https://www.hackerrank.com/challenges/java-exception-handling/problem
class MyCalculator {
private static final String MSG_EXCEP_LESSS_ZERO="n or p should not be negative.";
private static final String MSG_EXCEP_EQUAL_ZERO="n and p should not be zero.";

public int power ( int n, int p) throws Exception{


if((n<0) || (p<0)){
throw new Exception(MSG_EXCEP_LESSS_ZERO);
}else if (n == 0 && p == 0) {
throw new Exception(MSG_EXCEP_EQUAL_ZERO);
}
return (int)Math.pow(n,p);
}
}
Java Factory Pattern
https://www.hackerrank.com/challenges/java-factory/problem

if (order.equals("cake"))
return new Cake();
else
return new Pizza();

Java Method Overriding


https://www.hackerrank.com/challenges/java-method-overriding/problem
@Override
void getNumberOfTeamMembers() {
System.out.println("Each team has 11 players in " + getName());
}

Java Hashset
https://www.hackerrank.com/challenges/java-hashset/problem

Set<String> setPairs = new HashSet<>();


for (int i = 0; i < t; i++) {
setPairs.add(pair_left[i] + " " + pair_right[i]);
System.out.println(setPairs.size());
}

Java Comparator
https://www.hackerrank.com/challenges/java-comparator/problem

HackerRank

class Checker implements Comparator<Player>{


@Override
public int compare(Player playerA, Player playerB) {
int resultScore = playerB.score - playerA.score;
if(resultScore == 0){
resultScore = playerA.name.compareTo(playerB.name);
}
return resultScore;
}
}

You might also like