Sort by

recency

|

1183 Discussions

|

  • + 0 comments

    If this is your first time with cpp classes I recommend you these 2 videos:

    Constructor video

    Inheritance video

    For constructors with inheritance you can do something like this:

    child (dataType inheritedParam1, dataType Param2) : father (inheritedParam1) {
    	m_variable = Param2;
    }
    
  • + 0 comments

    import java.util.Scanner;

    class Person { String firstName; String lastName; int id;

    public Person(String firstName, String lastName, int id) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
    }
    

    }

    class Student extends Person { private int[] testScores;

    public Student(String firstName, String lastName, int id, int[] testScores) {
        super(firstName, lastName, id);
        this.testScores = testScores;
    }
    
    public char calculate() {
        int sum = 0;
        for (int score : testScores) {
            sum += score;
        }
        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';
    }
    

    }

    public class Solution { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String firstName = scn.next(); String lastName = scn.next(); int id = scn.nextInt(); int numScores = scn.nextInt(); int[] scores = new int[numScores];

        for (int i = 0; i < numScores; i++) {
            scores[i] = scn.nextInt();
        }
    
        Student std = new Student(firstName, lastName, id, scores);
        System.out.println("Name: "+lastName+", "+firstName);
        System.out.println("ID: "+id);
        System.out.println("Grade: "+std.calculate());
        scn.close();
    }
    

    }

  • + 0 comments

    String calculate(){ int sum=0; for(int a:testScores){ sum+=a; } int avg=sum/testScores.length; if(avg>40){ if(avg<55) return "D"; if(avg<70) return "P"; if(avg<80) return "A"; if(avg<90) return "E"; else return "O"; } else{ return "T"; } }

  • + 0 comments

    Python Code: This is my solution:

    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):
            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):
            average = sum(self.scores)/ len(self.scores)
            letter = '' "
            
            if average < 40:
                letter = 'T'
            elif 40 <= average < 55:
                letter = 'D'
            elif 55 <= average < 70:
                letter = 'P'
            elif 70 <= average < 80:
                letter = 'A'
            elif 80 <= average < 90:
                letter = 'E'
            elif 90 <= average <= 100:
                letter = 'O'
            return(letter)
            
    
    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
    class Student extends Person{
    	private int[] testScores;
        Student(String firstName, String lastName, int identification, int a[]){
            super(firstName,lastName,identification);
            this.testScores=a;
        }
        public String calculate(){
            int sum=0;
            for(int i=0; i<testScores.length; i++){
                sum+=testScores[i];
            }
            int avg=sum/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";
            }
            return "T";
        }
    }