Sort by

recency

|

1174 Discussions

|

  • + 0 comments

    Java Solution:

    Student(String fname, String lname, int id, int[] score){
            super(fname, lname, id);
            this.testScores = score;
        }
    
        char calculate(){
            int sum = 0;
            for(int i=0; i<testScores.length; i++){
                sum = sum +testScores[i];
            }
            int avg = sum/testScores.length;
            if(avg >= 90){
                return 'O';
            }else if(avg >= 80){
                return 'E';
            }else if(avg >= 70){
                return 'A';
            }else if(avg >= 55){
                return 'P';
            }else if(avg >= 40){
                return 'D';
            }else{
                return 'T';
            }
        }
    
  • + 1 comment

    any advice on my code? can you make it better or even shorter?

    Student class implementation with Python:

    class Student(Person):
        def __init__(self, firstName, lastName, idNumber, scores):
            self.firstName = firstName
            self.lastName = lastName
            self.idNumber = idNumber
            self.scores = scores
    
        def calculate(self):
            sum = 0
            for score in self.scores:
                sum += score
            
            avg = sum / len(self.scores)
            if avg <= 100 and avg >= 90:
                return "O"
            elif avg < 90 and avg >= 80:
                return "E"
            elif avg < 80 and avg >= 70:
                return "A"
            elif avg < 70 and avg >= 55:
                return "P"
            elif avg < 55 and avg >= 40:
                return "D"
            else:
                return "T"
    
  • + 0 comments

    javascript

    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, testScores){
            super(firstName, lastName, idNumber)
            
            this.testScores = testScores
           
            
        }
        /*	
        *   Method Name: calculate
        *   @return A character denoting the grade.
        */
        // Write your method here
        
        calculate(){
             let avg = 0
             let sum = 0
            for(let i=0; i<this.testScores.length; i++){
                 sum += this.testScores[i]
                 avg  = sum / this.testScores.length
            }
            
            if(avg>=90 && avg <=100){
                    return 'O'
                    
                }else if(avg>=80 && avg<90){
                    
                    return 'E'
                }else if(avg>=70 && avg<80){
                    
                    return 'A'
                }else if(avg>=55 && avg<70){
                    
                     return 'P'
                }else if(avg>=40 && avg<55){
                    
                    return 'D'
                }else if(avg<40){
                    
                    return 'T'
                }
            
        }
    }
    
  • + 1 comment

    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;

    Student(String firstName,String lastName,int id,int[] testScores){
        super(firstName,lastName,id);
        this.testScores=testScores;
    }
    
     public char calculate(){
        int sum=0;
        int n=testScores.length;
        for(int i=0;i<n;i++){
            sum += testScores[i];
        }
        float avg=sum/n;
        if(avg>=90&&avg<=100){
            return 'O';
        }
        else if(avg>=80){
            return 'E';
        }
        else if(avg>=70){
            return 'A';
         }
        else if(avg>=55){
            return 'P';
        }
        else if(avg>=40){
            return 'D';
        }
        else{
            return 'T';
        }
        //another method without using if-else 
        //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() );
    }
    

    }

  • + 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):
        #   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):
            super().__init__(firstName, lastName, idNumber)
            self.scores = scores
       
        def calculate(self):
            wynik2 = sum(self.scores) / len(self.scores)
            
            if 90 <= wynik2 and wynik2 <= 100:
                return "O"
            elif 80 <= wynik2 and wynik2 < 90:
                return "E"
            elif 70 <= wynik2 and wynik2 < 80:
                return "A"
            elif 55 <= wynik2 and wynik2 < 70:
                return "P"
            elif 40 <= wynik2 and wynik2 < 55:
                return "D"
            elif wynik2 < 40:
                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())