HR30 Day 12: Inheritance

1 개요[ | ]

Day 12: Inheritance
해커랭크 30 Days of Code
문제 풀이
10-19 Day e
HR30 Day 10: Binary Numbers

HR30 Day 11: 2D Arrays

HR30 Day 12: Inheritance

HR30 Day 13: Abstract Classes

HR30 Day 14: Scope

HR30 Day 15: Linked List

HR30 Day 16: Exceptions - String to Integer

HR30 Day 17: More Exceptions

HR30 Day 18: Queues and Stacks

HR30 Day 19: Interfaces

2 Java[ | ]

class Student extends Person {
    private int[] testScores;
    Student(String firstName, String lastName, int id, int[] scores) {
        super(firstName, lastName, id);
        this.testScores = scores;
    }
    char calculate() {
        double avg = Arrays.stream(this.testScores).average().getAsDouble();
        if( avg >= 90 ) return 'O';
        if( avg >= 80 ) return 'E';
        if( avg >= 70 ) return 'A';
        if( avg >= 55 ) return 'P';
        if( avg >= 40 ) return 'D';
        return 'T';
    }
}

3 PHP[ | ]

class Student extends person {
    private $testScores;
    public function __construct($first_name, $last_name, $identification, $scores) {
        parent::__construct($first_name, $last_name, $identification);
        $this->testScores = $scores;
    }
    function calculate() {
        $average = array_sum($this->testScores) / count($this->testScores);
        if( $average >= 90 ) return 'O';
        if( $average >= 80 ) return 'E';
        if( $average >= 70 ) return 'A';
        if( $average >= 55 ) return 'P';
        if( $average >= 40 ) return 'D';
        return 'T';
    }
}

4 Python[ | ]

import builtins
def print(*args,**kwargs):
    if "sep" not in kwargs and args[0] =="Grade: ":
        builtins.print(*args,**kwargs,sep="")
    else:
        builtins.print(*args,**kwargs)

class Student(Person):
    #   Class Constructor
    #   
    #   Parameters:
    #   firstName - A string denoting the Person's first name.
    #   lastName - A string denoting the Person's last name.
    #   id - An integer denoting the Person's ID number.
    #   scores - An array of integers denoting the Person's test scores.
    #
    # Write your constructor here
    def __init__(self, firstName, lastName, idNum, scores):
        super().__init__(firstName, lastName, idNum)
        self.scores = scores

    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if avg >= 90: return "O"
        if avg >= 80: return 'E'
        if avg >= 70: return 'A'
        if avg >= 55: return 'P'
        if avg >= 40: return 'D'
        return 'T'
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}