Inheritance

Sort by

recency

|

8 Discussions

|

  • + 0 comments
    class Student(Person):
        #   Class Constructor
            def __init__(self, firstName, lastName, idNum, scores):
                self.firstName = firstName
                self.lastName = lastName
                self.idNumber = idNum
                self.scores = scores
        #
            def calculate(self):
                avg = sum(self.scores) / len(self.scores)
                if ((90<=avg) and (avg<=100)):
                    return str("O")
                elif ((80<=avg) and (avg<90)):
                    return str("E")
                elif ((70<=avg) and (avg<80)):
                    return str("A")
                elif ((55<=avg) and (avg<70)):
                    return str("P")
                elif ((40<=avg) and (avg<55)):
                    return str("D")
                else:
                    return str("T")
    
  • + 0 comments
    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.
        #
        # Write your constructor here
        def __init__(self, firstName, lastName, idNumber, scores):
            Person.__init__(self,firstName, lastName, idNumber)
            self.scores = scores
    
    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here
    def calculate(self):
        sum = 0
        for i in range(len(self.scores)):
            sum = sum + self.scores[i]
    
            avg = sum/len(self.scores)
    
        if ((90<=avg) and (avg<=100)):
            return str("O")
        elif ((80<=avg) and (avg<90)):
            return str("E")
        elif ((70<=avg) and (avg<80)):
            return str("A")
        elif ((55<=avg) and (avg<70)):
            return str("P")
        elif ((40<=avg) and (avg<55)):
            return str("D")
        else:
            return str("T")
    
  • + 0 comments

    Java solution - passes 100% of test cases

    From my HackerRank solutions.

    class Student extends Person {
        private int[] testScores;
    
        /*  
        *   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.
        */
        public Student(String firstName, String lastName, int id, int [] scores) {
            super(firstName, lastName, id);
            testScores = scores;
        }
    
        /*  
        *   Method Name: calculate
        *   @return A character denoting the grade.
        */
        public char calculate() {
            double average = 0;
            for (int score : testScores) {
                average += score;
            }
            average /= testScores.length;
            
            if  (average >= 90) {
                return 'O';
            } else if (average >= 80) {
                return 'E';
            } else if (average >= 70) {
                return 'A';
            } else if (average >= 55) {
                return 'P';
            } else if (average >= 40) {
                return 'D';
            } else {
                return 'T';
            }
        }
    }
    
  • + 0 comments

    CPP solution

    class Student :  public Person{
    	private:
    		vector<int> testScores;  
    	public:
         
               Student(string firstName, string lastName,int identification,vector<int> scores)
                    :Person(firstName,lastName,identification){
                    this->testScores=scores;
                }
           
         char calculate(){
            int sum=0;
            for(int i=0;i<testScores.size();i++){
                sum+=testScores[i];
            }
            int avg=sum/testScores.size();
             if(90<=avg && avg<=100){
                return 'O';
            }else if(80<=avg && avg<90){
                return 'E';
            }else if(70<=avg && avg<80){
                return 'A';
            }else if(55<=avg && avg<70){
                return 'P';
            }else if(40<=avg && avg<55){
                return 'D';
             }else {
                return 'T';
            } 
        }
    };
    
  • + 0 comments

    Here is my wokring solution in java-

    class Student extends Person{
    	private int[] testScores;
    
        /*	
        *   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
        Student(String firstName,String lastName,int id,int[] scores){
            super(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.length;i++){
                sum+=testScores[i];
            }
            int avg=sum/testScores.length;
             if(90<=avg && avg<=100){
                return 'O';
            }else if(80<=avg && avg<90){
                return 'E';
            }else if(70<=avg && avg<80){
                return 'A';
            }else if(55<=avg && avg<70){
                return 'P';
            }else if(40<=avg && avg<55){
                return 'D';
             }else {
                return 'T';
            } 
        }
    }