본문 바로가기
1 Day 1 Algorithms

[2019.01.08] Interfaces

by 곰돌찌 2019. 1. 8.

Problem


Objective 
Today, we're learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!

Task 
The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of .

Input Format

A single line containing an integer, .

Constraints

Output Format

You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.

Sample Input

6

Sample Output

I implemented: AdvancedArithmetic
12

Explanation

The integer  is evenly divisible by , and . Our divisorSum method should return the sum of these numbers, which is . The Solution class then prints  on the first line, followed by the sum returned by divisorSum (which is ) on the second line.


How I solved the problem


# interface의 정의와 얘를 사용하는 방법에 대해서 아는 게 중요!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.util.*;
 
interface AdvancedArithmetic{
   int divisorSum(int n);
}
class Calculator implements AdvancedArithmetic {
    public int divisorSum(int n) {
        int result = 0;
 
        for (int i = 0; i < n; i ++) {
            int division = i + 1;
 
            if (n % division == 0) {
                result += division;
            } 
        }
        
        return result;
    }
}
 
class Solution {
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
        
          AdvancedArithmetic myCalculator = new Calculator(); 
        int sum = myCalculator.divisorSum(n);
        System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
        System.out.println(sum);
    }
}
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.01.10] Generics  (0) 2019.01.10
[2019.01.09] Sorting  (0) 2019.01.09
[2019.01.07] Queues and Stacks  (0) 2019.01.07
[2019.01.04] More Exceptions  (0) 2019.01.04
[2019.01.03] Exceptions - String to Integer  (0) 2019.01.03

댓글