Created
November 23, 2022 12:38
-
-
Save alwaisy/ac19838682da058fe4dd52ffc52229e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Objective | |
Today, we're delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video. | |
Task | |
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 parameters: | |
A string, . | |
A string, . | |
An integer, . | |
An integer array (or vector) of test scores, . | |
A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average: | |
Grading.png | |
Input Format | |
The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments. | |
The first line contains , , and , separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes . | |
Constraints | |
Output Format | |
Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented. | |
Sample Input | |
Heraldo Memelli 8135627 | |
2 | |
100 80 | |
Sample Output | |
Name: Memelli, Heraldo | |
ID: 8135627 | |
Grade: O | |
Explanation | |
This student had scores to average: and . The student's average grade is . An average grade of corresponds to the letter grade , so the calculate() method should return the character'O'. | |
**/ | |
// solution - all tests (0 to 7) | |
// | |
// | |
// | |
// | |
// | |
'use strict'; | |
var _input = ''; | |
var _index = 0; | |
process.stdin.on('data', (data) => { _input += data; }); | |
process.stdin.on('end', () => { | |
_input = _input.split(new RegExp('[ \n]+')); | |
main(); | |
}); | |
function read() { return _input[_index++]; } | |
/**** Ignore above this line. ****/ | |
class Person { | |
constructor(firstName, lastName, identification) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.idNumber = identification; | |
} | |
printPerson() { | |
console.log( | |
"Name: " + this.lastName + ", " + this.firstName | |
+ "\nID: " + this.idNumber | |
) | |
} | |
} | |
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, id, testScores) { | |
super( | |
firstName, lastName, id | |
) | |
this.firstName = firstName.length >= 1 ? firstName : null | |
this.lastName = lastName.length <= 10 ? lastName : null | |
this.idNumber = id.toString(); | |
this.scores = testScores | |
} | |
/* | |
* Method Name: calculate | |
* @return A character denoting the grade. | |
*/ | |
// Write your method here | |
// printPerson(){ | |
// // super.printPerson(); | |
// if((this.firstName.length >=1 && this.firstName.length <= 10) && (this.lastName.length >=1 && this.lastName.length <= 10)) { | |
// console.log("Name:"+ this.lastName+ ", "+ this.firstName) | |
// } | |
// } | |
calculate() { | |
let avarage | |
const sum = this.scores.reduce((a, b) => a + b, 0); | |
avarage = sum/this.scores.length | |
if(avarage >= 90 && avarage <= 100){ | |
return 'O'; | |
} else if(avarage >= 80 && avarage < 90){ | |
return 'E'; | |
} else if(avarage >= 70 && avarage < 80){ | |
return 'A'; | |
} else if(avarage >= 55 && avarage < 70){ | |
return 'P'; | |
} else if(avarage >= 40 && avarage < 55){ | |
return 'D'; | |
} else { | |
return 'T' | |
} | |
} | |
} | |
function main() { | |
let firstName = read() | |
let lastName = read() | |
let id = +read() | |
let numScores = +read() | |
let testScores = new Array(numScores) | |
for (var i = 0; i < numScores; i++) { | |
testScores[i] = +read() | |
} | |
let s = new Student(firstName, lastName, id, testScores) | |
s.printPerson() | |
s.calculate() | |
console.log('Grade: ' + s.calculate()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment