We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 12: Inheritance
Day 12: Inheritance
+ 0 comments Python solution
class Student(Person): def __init__(self, firstName, lastName, idNumber, scores): super().__init__(firstName, lastName, idNumber) self.scores = [] def calculate(self): prom = sum(scores) / len(scores) if prom >= 90: return 'O' elif prom >= 80: return 'E' elif prom >= 70: return 'A' elif prom >= 55: return 'P' elif prom >= 40: return 'D' else: return 'T'
+ 0 comments class Person: def __init__(self, firstName, lastName, idNumber): self.firstName = firstName self.lastName = lastName self.idNumber = idNumber def printPerson(self): print("Name:", self.lastName + ",", self.firstName) print("ID:", self.idNumber) class Student(Person): def __init__(self, firstName, lastName, idNumber, scores): super().__init__(firstName, lastName, idNumber) self.scores = scores def calculate(self): avg = sum(self.scores)/len(self.scores) if avg < 40: return 'T' elif 40<=avg<55: return 'D' elif 55<=avg<70: return 'P' elif 70<=avg<80: return 'A' elif 80<=avg<90: return 'E' elif 90<=avg<=100: return 'O' line = input().split() firstName = line[0] lastName = line[1] idNum = line[2] numScores = int(input()) # not needed for Python scores = list( map(int, input().split()) ) s = Student(firstName, lastName, idNum, scores) s.printPerson() print("Grade:", s.calculate())
+ 1 comment Is there any reason why I'm not passing test case 5 and 7? I'm pretty lost.
public Student(String firstName, String lastName, int idNumber, int[] scores) { super(firstName, lastName, idNumber); this.testScores = scores; } public char calculate() { int sum = 0; int grade = 0; for (int i = 0; i < testScores.length; i++) { sum += testScores[i]; } grade = sum / testScores.length; if (grade <= 100 && grade >= 90) { return 'O'; } else if (grade < 90 && grade >= 80) { return 'E'; } else if (grade < 80 && grade >= 70) { return 'A'; } else if (grade < 70 && grade >= 55) { return 'D'; } else if (grade < 40 && grade >= 0) { return 'T'; } else return Character.MIN_VALUE; }
}
+ 0 comments #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; class Person{ protected: string firstName; string lastName; int id; public: Person(string firstName, string lastName, int identification); void printPerson(); }; Person::Person(string firstName, string lastName, int identification) { this->firstName = firstName; this->lastName = lastName; this->id = identification; } void Person::printPerson(){ cout << "Name: " << lastName << ", " << firstName << "\nID: " << id << "\n"; } class Student : public Person{ private: vector<int> testScores; public: Student(int idNumber, const string& lastName, const string& firstName, const vector<int>& scores); char calculate(); }; Student::Student(int idNumber, const string& lastName, const string& firstName, const vector<int>& scores) : Person(firstName, lastName, idNumber){ this->id = idNumber; this->lastName = lastName; this->firstName = firstName; this->testScores = scores; } char Student::calculate(){ int sum = 0; for(auto &score : testScores){ sum += score; } int average = sum / testScores.size(); return (average >= 90) ? 'O' : (average >= 80) ? 'E' : (average >= 70) ? 'A' : (average >= 55) ? 'P' : (average >= 40) ? 'D' : 'T'; } int main() { string firstName; string lastName; int id; int numScores; cin >> firstName >> lastName >> id >> numScores; vector<int> scores; for(int i = 0; i < numScores; ++i){ int tmpScore; cin >> tmpScore; scores.push_back(tmpScore); } Student* s = new Student(id, lastName, firstName, scores); s->printPerson(); cout << "Grade: " << s->calculate() << "\n"; return 0; }
+ 0 comments C# Solution
class Student : Person{ private int[] testScores; /* * 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 public Student(string firstName, string lastName,int id, int[] scores) : base(firstName,lastName,id) { this.testScores = scores; } /* * Method Name: Calculate * Return: A character denoting the grade. */ // Write your method here public char Calculate(){ int sum = 0; for (int i=0; i < testScores.Count(); i++) { sum += testScores[i]; } double tb = sum / testScores.Count(); if (tb >= 90 && tb <= 100) { return 'O'; } else if (tb >= 80 && tb < 90) { return 'E'; } else if (tb >= 70 && tb < 80) { return 'A'; } else if (tb >= 55 && tb < 70) { return 'P'; } else if (tb >= 40 && tb < 55) { return 'D'; } else { return 'T'; } } }
Load more conversations
Sort 1117 Discussions, By:
Please Login in order to post a comment