TheBulgarianWolf

Inheritance Hackerrrank

May 9th, 2021
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Person
  5. {
  6.     protected string firstName;
  7.     protected string lastName;
  8.     protected int id;
  9.  
  10.     public Person() { }
  11.     public Person(string firstName, string lastName, int identification)
  12.     {
  13.         this.firstName = firstName;
  14.         this.lastName = lastName;
  15.         this.id = identification;
  16.     }
  17.     public void printPerson()
  18.     {
  19.         Console.WriteLine("Name: " + lastName + ", " + firstName + "\nID: " + id);
  20.     }
  21. }
  22.  
  23. class Student : Person
  24. {
  25.     private int[] testScores;
  26.  
  27.     public Student(string firstName,string lastName,int id,int[] scores)
  28.     {
  29.         this.firstName = firstName;
  30.         this.lastName = lastName;
  31.         this.id = id;
  32.         this.testScores = scores;
  33.     }
  34.  
  35.     public char Calculate()
  36.     {
  37.         double sum = 0;
  38.         int n = 0;
  39.         foreach(int number in testScores)
  40.         {
  41.             sum += number;
  42.             n++;
  43.         }
  44.         double result = sum / n;
  45.         if(result >= 90 && result <= 100)
  46.         {
  47.             return 'O';
  48.         }
  49.         else if (result >= 80 && result < 90)
  50.         {
  51.             return 'E';
  52.         }
  53.         else if (result >=70  && result < 80)
  54.         {
  55.             return 'A';
  56.         }
  57.         else if (result >= 55 && result < 70)
  58.         {
  59.             return 'P';
  60.         }
  61.         else if (result >= 40 && result < 55)
  62.         {
  63.             return 'D';
  64.         }
  65.         else if (result < 40)
  66.         {
  67.             return 'T';
  68.         }
  69.         else
  70.         {
  71.             return 'T';
  72.         }
  73.     }
  74.    
  75.  
  76.     /* 
  77.     *   Class Constructor
  78.     *  
  79.     *   Parameters:
  80.     *   firstName - A string denoting the Person's first name.
  81.     *   lastName - A string denoting the Person's last name.
  82.     *   id - An integer denoting the Person's ID number.
  83.     *   scores - An array of integers denoting the Person's test scores.
  84.     */
  85.     // Write your constructor here
  86.  
  87.     /* 
  88.     *   Method Name: Calculate
  89.     *   Return: A character denoting the grade.
  90.     */
  91.     // Write your method here
  92. }
  93.  
  94. class Solution
  95. {
  96.     static void Main()
  97.     {
  98.         string[] inputs = Console.ReadLine().Split();
  99.         string firstName = inputs[0];
  100.         string lastName = inputs[1];
  101.         int id = Convert.ToInt32(inputs[2]);
  102.         int numScores = Convert.ToInt32(Console.ReadLine());
  103.         inputs = Console.ReadLine().Split();
  104.         int[] scores = new int[numScores];
  105.         for (int i = 0; i < numScores; i++)
  106.         {
  107.             scores[i] = Convert.ToInt32(inputs[i]);
  108.         }
  109.  
  110.         Student s = new Student(firstName, lastName, id, scores);
  111.         s.printPerson();
  112.         Console.WriteLine("Grade: " + s.Calculate() + "\n");
  113.     }
  114. }
Add Comment
Please, Sign In to add comment