biryu2205 / training

0 stars 0 forks source link

Hackerrank Java Introduction #2

Open biryu2205 opened 7 years ago

biryu2205 commented 7 years ago

1.Welcome to Java!

public class Solution {

   public static void main(String[] args) {
      System.out.println("Hello, World.");
      System.out.println("Hello, Java.");
  }
}
biryu2205 commented 7 years ago

2.Java Stdin and Stdout I

import java.util.*;

public class Solution {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
      int a = scanner.nextInt();
      int b = scanner.nextInt();
      int c = scanner.nextInt();
      scanner.close();
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
  }
}
biryu2205 commented 7 years ago

3.Java IF Else

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) {

    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String ans = "";
    if (n % 2 == 1) {
      ans = "Weird";
    } else {
    }
    System.out.println(ans);
  }
}
biryu2205 commented 7 years ago

4.Java Stdin and Stdout II

import java.util.Scanner;

public class Solution {

  public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int i = scan.nextInt();
      System.out.println("String: " + s);
      System.out.println("Double: " + d);
      System.out.println("Int: " + i);
  }
}
biryu2205 commented 7 years ago

5.Java Output Formatting

import java.util.Scanner;

public class Solution {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("================================");
    for (int i = 0; i < 3; i++) {
      String s1 = sc.next();
      int x = sc.nextInt();
      //Complete this line
      System.out.printf("%-15s%03d%n", s1, x);
    }
    System.out.println("================================");
  }
}
biryu2205 commented 7 years ago

6.Java Loops I

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) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    int i = 0;
    int R;
    while (i < 10) {
      i = i + 1;
      R = N * i;
      System.out.print(N + " x " + i + " = " + R);

      System.out.print("\n");
    }
  }
}
biryu2205 commented 7 years ago

7.Java loops II

import java.util.*;
import java.io.*;

class Solution {
  public static void main(String[] argh) {
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    for (int i = 0; i < t; i++) {
      int a = in.nextInt();
      int b = in.nextInt();
      int n = in.nextInt();
    }
    in.close();
  }
}
biryu2205 commented 7 years ago

8.Java Datatypes

import java.util.*;
import java.io.*;

class Solution {
  public static void main(String[] argh) {

    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();

    for (int i = 0; i < t; i++) {

      try {
        long x = sc.nextLong();
        System.out.println(x + " can be fitted in:");
        if (x >= -128 && x <= 127) System.out.println("* byte");
        if (x >= Short.MIN_VALUE && x <=

            Short.MAX_VALUE) {
          System.out.println("* short");
        }
        if (x >= Integer.MIN_VALUE && x <=

            Integer.MAX_VALUE) {
          System.out.println("* int");
        }
        if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE

            ) {
          System.out.println("* long");
        }
      } catch (Exception e) {
        System.out.println(sc.next() + " can't be fitted anywhere.");
      }
    }
  }
}
biryu2205 commented 7 years ago

9.Java End-of-file


import java.util.Scanner;

public class Solution {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = 0;
    while (scan.hasNext()) {
      String s = scan.nextLine();
      i++;
      System.out.printf("%d %s \n", i, s);
    }
  }
}
biryu2205 commented 7 years ago

10.Java Static Initializer Block

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

public class Solution {
  static int B, H;
  static boolean flag;

  static {
    Scanner cin = new Scanner(System.in);
    B = cin.nextInt();
    H = cin.nextInt();
    if (B > 0 && H > 0) {
      flag = true;
    } else {
      System.out.println("java.lang.Exception: Breadth and height must be positive");
    }
  }

  public static void main(String[] args) {
    if (flag) {
      int area = B * H;
      System.out.print(area);
    }
  }
}
biryu2205 commented 7 years ago

11.Java Int to String

import java.util.*;
import java.security.*;

public class Solution {
  public static void main(String[] args) {

    DoNotTerminate.forbidExit();

    try {
      Scanner in = new Scanner(System.in);
      int n = in.nextInt();
      in.close();
      String s = Integer.toString(n);
      if (n == Integer.parseInt(s)) {
        System.out.println("Good job");
      } else {
        System.out.println("Wrong answer.");
      }
    } catch (DoNotTerminate.ExitTrappedException e) {
      System.out.println("Unsuccessful Termination!!");
    }
  }
}

//The following class will prevent you from terminating the code using exit(0)!
class DoNotTerminate {

  public static class ExitTrappedException extends SecurityException {

    private static final long serialVersionUID = 1;
  }

  public static void forbidExit() {
    final SecurityManager securityManager = new SecurityManager() {
      @Override
      public void checkPermission(Permission permission) {
        if (permission.getName().contains("exitVM")) {
          throw new ExitTrappedException();
        }
      }
    };
    System.setSecurityManager(securityManager);
  }
}
biryu2205 commented 7 years ago

13.Java Currency Formatter

import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class Solution {

  public static void main(String[] args) {
        /* Read input */
    Scanner scanner = new Scanner(System.in);
    double payment = scanner.nextDouble();
    scanner.close();

        /* Create custom Locale for India. 
          I used the "IANA Language Subtag Registry" to find India's country code */
    Locale indiaLocale = new Locale("en", "IN");

        /* Create NumberFormats using Locales */
    NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
    NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
    NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);

        /* Print output */
    System.out.println("US: " + us.format(payment));
    System.out.println("India: " + india.format(payment));
    System.out.println("China: " + china.format(payment));
    System.out.println("France: " + france.format(payment));
  }
}
biryu2205 commented 7 years ago

__12.Java Currency Formatter

import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class Solution {

  public static void main(String[] args) {
        /* Read input */
    Scanner scanner = new Scanner(System.in);
    double payment = scanner.nextDouble();
    scanner.close();

        /* Create custom Locale for India.
          I used the "IANA Language Subtag Registry" to find India's country code */
    Locale indiaLocale = new Locale("en", "IN");

        /* Create NumberFormats using Locales */
    NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
    NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
    NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);

        /* Print output */
    System.out.println("US: " + us.format(payment));
    System.out.println("India: " + india.format(payment));
    System.out.println("China: " + china.format(payment));
    System.out.println("France: " + france.format(payment));
  }
}
letientai299 commented 7 years ago

Not done yet. Read my issue again. You should put you code in a maven project, and push it to github instead.

letientai299 commented 7 years ago

Add test case. One I verify your test and code are completed, you can close this issue.

letientai299 commented 7 years ago

Deadline for the testing: 2017/11/08 12PM