30 Days of Code - Day 12: Inheritance

Coding challenges are a great resource for learning coding techniques and improve analytical thinking, this is a collection of challenges from different platforms.


Today, we’re delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video!


You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.

Complete the Student class by writing the following:

  • A Student class constructor, which has 4 parameters:

    1. A string, firstName.
    2. A string, lastName.
    3. An integer, id.
    4. An integer array (or vector) of test scores, scores.
  • A char calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average:

    LetterAverage (a)
    O90 <= a <= 100
    E80 <= a < 90
    A70 <= a < 80
    P55 <= a < 70
    D40 <= a < 55
    Ta < 40

Input Format

The locked stub code in your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments).

You are not responsible for reading the following input from stdin:

The first line contains firstName, lastName, and id, respectively. The second line contains the number of test scores. The third line of space-separated integers describes scores.


  • 1 <= |fistName|,|lastName| <= 10

  • |id| = 7

  • 0 <= score,average <= 100

Output Format

This is handled by the locked stub code in your editor. Your output will be correct if your Student class constructor and calculate() method are properly implemented.

Sample 00

Heraldo Memelli 8135627
100 80
Name: Memelli, Heraldo
ID: 8135627
Grade: O


This student had 2 scores to average: 100 and 80. The student’s average grade is (100 + 80)/2 = 90. An average grade of 90 corresponds to the letter grade O, so our calculate() method should return the character 'O'.


package main

import "fmt"

func main() {
  var (
    firstName string
    lastName  string
    id, tests int

  fmt.Scan(&firstName, &lastName, &id, &tests)
  scores := make([]int, tests)

  for i := 0; i < tests; i++ {

  s := NewStudent(firstName, lastName, id, scores)
  fmt.Printf("Grade: %v", s.calculate())

type Person struct {
  firstName string
  lastName  string
  id        int

func NewPerson(firstName, lastName string, id int) Person {
  return Person{firstName, lastName, id}

func (p Person) printPerson() {
  fmt.Printf("Name: %v, %v\nID: %v\n", p.lastName, p.firstName, p.id)

type Student struct {
  scores []int

func NewStudent(firstName, lastName string, id int, scores []int) Student {
  return Student{NewPerson(firstName, lastName, id), scores}

func (s Student) calculate() string {
  avg := average(s.scores...)

  switch {
  case avg >= 90:
    return "O"
  case avg >= 80:
    return "E"
  case avg >= 70:
    return "A"
  case avg >= 55:
    return "P"
  case avg >= 40:
    return "D"
    return "T"

func average(numbers ...int) (result int) {
  for _, n := range numbers {
    result += n

  return result / len(numbers)