Coding example of inheritance



using System;
using System.Linq;

class Person{
    protected string firstName;
    protected string lastName;
    protected int id;
    
    public Person(){}
    public Person(string firstName, string lastName, int identification){
            this.firstName = firstName;
            this.lastName = lastName;
            this.id = identification;
    }
    public void printPerson(){
        Console.WriteLine("Name: " + lastName + ", " + firstName + "\nID: " + id); 
    }
}

class Student : Person{
    private int[] testScores;  

    public Student(){}

    public Student(string firstName, string lastName, int identification, int[] testScores){
        this.testScores = testScores;
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = identification;
    }
     
     public string Calculate(){

            var j = 0;
            var sum = 0;

            for (var i=0; i <  testScores.Count(); i++){
                sum = sum + testScores[i];
                j = j+1;
            }
          
            var average = sum/j;

            if (100 >= average && average >= 90)
                return "O";

            if (90 > average && average >= 80)
                return "E";

            if (80 > average && average >= 70)
                return "A";

            if (70 > average && average >= 55)
                return "P";
            
            if (55 > average && average >= 40)
                return "D";
            
            return "T";
     }

    /*  
    *   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
    
    /*  
    *   Method Name: Calculate
    *   Return: A character denoting the grade.
    */
    // Write your method here
}

class Solution {
    static void Main() {
        string[] inputs = Console.ReadLine().Split();
        string firstName = inputs[0];
        string lastName = inputs[1];
        int id = Convert.ToInt32(inputs[2]);
        int numScores = Convert.ToInt32(Console.ReadLine());
        inputs = Console.ReadLine().Split();
        int[] scores = new int[numScores];
        for(int i = 0; i < numScores; i++){
            scores[i]= Convert.ToInt32(inputs[i]);
        } 
        
        Student s = new Student(firstName, lastName, id, scores);
        s.printPerson();
        Console.WriteLine("Grade: " + s.Calculate() + "\n");
    }
}


Success
Input (stdin)
Download
  • Heraldo Memelli 8135627
  • 2
  • 100 80
Expected Output
Download
  • Name: Memelli, Heraldo
  • ID: 8135627
  • Grade: O


留言

這個網誌中的熱門文章

香港袐密行動

要老是忘記, 我更記不起