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 JS Solution
class Student extends Person { /* * Class Constructor * * @param firstName - A string denoting the Person's first name. * @param lastName - A string denoting the Person's last name. * @param id - An integer denoting the Person's ID number. * @param scores - An array of integers denoting the Person's test scores. */ // Write your constructor here constructor(firstName,lastName,idNumber,scores){ super(firstName,lastName,idNumber) this.scores = scores; } /* * Method Name: calculate * @return A character denoting the grade. */ // Write your method here calculate(){ const totalAverage = this.scores.reduce((acc,curr) =>acc+=curr,0) / this.scores.length; let A = "" if(totalAverage>=90 && totalAverage<=100) A = "O" else if(totalAverage>=80 && totalAverage<=90) A = "E" else if(totalAverage>=70 && totalAverage<=80) A = "A" else if(totalAverage>=55 && totalAverage<=70) A = "P" else if(totalAverage>=40 && totalAverage<=55) A = "D" else A = "T" return A } }
+ 0 comments Using Java
class Student extends Person { private int[] testScores; Student(String firstName, String lastName, int identification, int[] testScores) { super(firstName, lastName, identification); this.testScores = testScores; } public String calculate() { String st = null; int tem = 0; for(int i = 0; i < testScores.length; i++) tem += testScores[i]; double avre = ((tem)/testScores.length); if(avre >= 90 && avre <= 100){ st = "O"; } else if(avre >= 80 && avre < 90) { st = "E"; } else if(avre >= 70 && avre < 80) { st = "A"; } else if(avre >= 55 && avre < 70) { st = "P"; } else if(avre >= 40 && avre < 55) { st = "D"; } else if(avre < 40) { st = "T"; } return st; } }
+ 0 comments Python 3 using super()
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): # 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. # def __init__(self, firstName, lastName, idNumber, scores): self.scores = scores super().__init__(firstName,lastName,idNumber) # Function Name: calculate # Return: A character denoting the grade. # def calculate(self): avg = sum(self.scores) / len(self.scores) if avg >= 90 and avg <= 100: grade = 'O' elif avg >= 80 and avg <= 90: grade = 'E' elif avg >= 70 and avg <= 80: grade = 'A' elif avg >= 55 and avg <= 70: grade = 'P' elif avg >= 40 and avg <= 55: grade = 'D' else: grade = 'T' return grade 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())
+ 0 comments Python 3 using supe()
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): # 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. # def __init__(self, firstName, lastName, idNumber, scores): self.scores = scores super().__init__(firstName,lastName,idNumber) # Function Name: calculate # Return: A character denoting the grade. # def calculate(self): avg = sum(self.scores) / len(self.scores) if avg >= 90 and avg <= 100: grade = 'O' elif avg >= 80 and avg <= 90: grade = 'E' elif avg >= 70 and avg <= 80: grade = 'A' elif avg >= 55 and avg <= 70: grade = 'P' elif avg >= 40 and avg <= 55: grade = 'D' else: grade = 'T' return grade 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())
+ 0 comments Python 3 using super()
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. # def __init__(self, firstName, lastName, idNumber, scores): self.scores = scores super().__init__(firstName,lastName,idNumber) # Function Name: calculate # Return: A character denoting the grade. # def calculate(self): avg = sum(self.scores) / len(self.scores) if avg >= 90 and avg <= 100: grade = 'O' elif avg >= 80 and avg <= 90: grade = 'E' elif avg >= 70 and avg <= 80: grade = 'A' elif avg >= 55 and avg <= 70: grade = 'P' elif avg >= 40 and avg <= 55: grade = 'D' else: grade = 'T' return grade 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())
Load more conversations
Sort 1144 Discussions, By:
Please Login in order to post a comment