Sort by

recency

|

1188 Discussions

|

  • + 0 comments
    import java.util.Arrays;
    import java.util.Scanner;
    
    class Person {
        protected String firstName;
        protected String lastName;
        protected int idNumber;
    
        Person(String firstName, String lastName, int identification){
            this.firstName = firstName;
            this.lastName = lastName;
            this.idNumber = identification;
        }
    
        public void printPerson(){
            System.out.println("Name: " + lastName + ", " + firstName + "\nID: " + idNumber);
        }
    }
    
    class Student extends Person{
        private final int[] testScores;
    
        Student(String firstName, String lastName, int identification, int[] testScores) {
            super(firstName, lastName, identification);
            this.testScores = testScores;
        }
    
        public char calculate() {
            double avgScore = Arrays.stream(testScores).average().orElse(0.0);
            int[] thresholds = {39, 54, 69, 79, 89, 100};
            char[] letters = {'T', 'D', 'P', 'A', 'E', 'O'};
    
            for (int i = 0; i < thresholds.length; i++) {
                if (avgScore <= thresholds[i]) {
                    return letters[i];
                }
            }
    
            return 'T';
        }
    }
    
    public class Solution4 {
        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());
        }
    }
    
  • + 0 comments

    Python code:

    class Student(Person):

    # Write your function here
    
    def __init__(self,firstName,lastName,idNumber,scores): 
    
        super().__init__(firstName,lastName,idNumber) 
    
        self.scores=scores
    
    def calculate(self): 
    
         m=sum(self.scores)/len(self.scores) 
    
         if m>=90: 
    
            return 'O' 
    
         elif m>=80: 
    
            return 'E' 
    
         elif m>=70: 
    
            return 'A' 
    
         elif m>=55: 
    
            return "P" 
    
         elif m>=40: 
            return 'D' 
         else: 
            return 'T'
    
  • + 0 comments

    Here is HackerRank Day 12 Inheritance solution - https://programmingoneonone.com/hackerrank-day-12-inheritance-30-days-of-code-solution.html

  • + 0 comments

    python code

    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): m=sum(self.scores)/len(self.scores) if m>=90: return 'O' elif m>=80: return 'E' elif m>=70: return 'A' elif m>=55: return "P" elif m>=40: return 'D' else: return 'T' 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

    Given code has compliation error C#:

    Solution.cs(91,21): error CS0019: Operator '+' cannot be applied to operands of type 'string' and 'void'

    Please fix it, we cannot edit these code. thanks!