Hiking around HackerRank — 06

08/02/2020

Java Exception Handling (Try-catch)

Java Exception Handling (Try-catch), retrieved from https://www.hackerrank.com/challenges/java-exception-handling-try-catch/problem
import java.io.*;
import java.util.*;
public class Solution { public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
try {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
System.out.println(x / y);
}
catch (ArithmeticException | InputMismatchException e) {
if (e instanceof ArithmeticException) {
System.out.println("java.lang.ArithmeticException: / by zero");
}
else {
System.out.println("java.util.InputMismatchException");
}
}
}
}

Java Exception Handling

Java Exception Handling, retrieved from https://www.hackerrank.com/challenges/java-exception-handling/problem
import java.util.Scanner;
class MyCalculator {
/*
* Create the method long power(int, int) here.
*/
public static int power(int n, int p) throws Exception {
if (n < 0 || p < 0) {
System.out.println("java.lang.Exception: n or p should not be negative.");
}
if ( n == 0 || p == 0) {
System.out.println("java.lang.Exception: n and p should not be zero.");
}
return (int) Math.pow(n, p);
}
}public class Solution {
public static final MyCalculator my_calculator = new MyCalculator();
public static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
while (in .hasNextInt()) {
int n = in .nextInt();
int p = in .nextInt();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}
import java.util.Scanner;
class MyCalculator {
/*
* Create the method long power(int, int) here.
*/
public int power(int n, int p) throws Exception {
if (n < 0 || p < 0) {
throw new Exception ("java.lang.Exception: n or p should not be negative.");
}
if ( n == 0 && p == 0) {
throw new Exception ("java.lang.Exception: n and p should not be zero.");
}
int result = (int) Math.pow(n, p);
return result;
}
}
public class Solution {
public static final MyCalculator my_calculator = new MyCalculator();
public static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
while (in .hasNextInt()) {
int n = in .nextInt();
int p = in .nextInt();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}
import java.util.Scanner;
class MyCalculator {
/*
* Create the method long power(int, int) here.
*/
public int power(int n, int p) throws Exception{
int result;
if(n==0 && p==0){
throw new Exception("n and p should not be zero.");
}
else if(n<0 || p<0){
throw new Exception("n or p should not be negative.");
}
else {
result=(int)Math.pow(n,p);
return result;
}
}
}
public class Solution {
public static final MyCalculator my_calculator = new MyCalculator();
public static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
while (in .hasNextInt()) {
int n = in .nextInt();
int p = in .nextInt();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}

8/21/2020

Java Anagram

Java Anagram, retrieved from https://www.hackerrank.com/challenges/java-anagrams/problem
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Solution { static boolean isAnagram(String a, String b) {
// Complete the function

Boolean flag = false;
if(a != null && b != null) {
char tempArr1[] = a.toLowerCase().toCharArray();
char tempArr2[] = b.toLowerCase().toCharArray();
Arrays.sort(tempArr1);
Arrays.sort(tempArr2);
String newA = tempArr1.toString();
String newB = tempArr2.toString();
flag = newA.equals(newB);
}
return flag;
}
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
import java.util.Scanner;public class Solution {    static boolean isAnagram(String a, String b) {
// Complete the function
int aLen = a.length();
int bLen = b.length();
a = a.toLowerCase();
b = b.toLowerCase();
if (aLen != bLen || aLen == 0 || bLen == 0) return false; int[] charFreq = new int[26]; for (int i = 0; i < aLen; i++){
int index1 = a.charAt(i) - 'a';
int index2 = b.charAt(i) - 'a';
charFreq[index1]++;
charFreq[index2]--;
}
for (int i = 0; i < 26; i++){
if (charFreq[i] != 0) return false;
}
return true;
}
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}

Conclusion

Undergraduate student / Research assistant/ Always curious / Opinions are mine