Day 12: Inheritance (30 Days of Code)

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:

  • Student class constructor, which has 4 parameters:
    1. A string, firstName.
    2. A string, lastName.
    3. An integer, idNumber.
    4. An integer array (or vector) of test scores, scores.
  • char calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average:

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 firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.

Constraints

  • 1<= length of firstName,length of lastName <= 10
  • length of idNumber = 7
  • 0 <= score <= 100

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

Solutions:

1)Java:

import java.util.*;

class Person {
    protected String firstName;
    protected String lastName;
    protected int idNumber;
    
    // Constructor
    Person(String firstName, String lastName, int identification){
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = identification;
    }
    
    // Print person data
    public void printPerson(){
         System.out.println(
                "Name: " + lastName + ", " + firstName 
            +   "\nID: " + idNumber); 
    }
     
}

class Student extends Person{
    private int[] testScores;

    // Write your constructor here
    Student(String firstName,String lastName,int idNumber,int [] scores){
        super(firstName,lastName,idNumber);
        testScores=scores;
    }
    // Write your method here
    public char calculate(){
        int avg=0;
        for(int i=0;i<testScores.length;i++)
        avg+=testScores[i];
        avg=avg/testScores.length;
        return avg>89?'O':avg>79?'E':avg>69?'A':avg>54?'P':avg>39?'D':'T';
    }
}

class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String firstName = scan.next();
        String lastName = scan.next();
        int id = scan.nextInt();
        int numScores = scan.nextInt();
        int[] testScores = new int[numScores];
        for(int i = 0; i < numScores; i++){
            testScores[i] = scan.nextInt();
        }
        scan.close();
        
        Student s = new Student(firstName, lastName, id, testScores);
        s.printPerson();
        System.out.println("Grade: " + s.calculate() );
    }
}

2)Javascript:

'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 {
   
    // Write your constructor here
    constructor(firstName,lastName,idNumber,scores){
        super(firstName,lastName,idNumber);
        this.scores=scores;
    }
  
    // Write your method here
    calculate(){
        let sum=0,i,avg=0;
        for(i in this.scores)
        sum+=this.scores[i];
        avg=sum/this.scores.length;
       return avg> 89 ?'O': avg>79 ? 'E' : avg > 69 ? 'A' : avg > 54 ? 'P' :avg > 39 ? 'D' : '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())
}

Leave a Reply