Day 12: Inheritance
Objective
Here we are dealing with Inheritance. So Codinghumans lets know what is inheritance.
Inheritance in java allows to build new classes from existing classes and make s resuse of codes easier.
Mission
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:
A Student class constructor, which has 4 parameters:
a. A string, firstName.
b. A string, lastName .
c. An integer, id.
d. An integer array (or vector) of test scores, .
A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average:
how score is grades ,For Average a
if 90<=a<=100 grade is O
else if 90<=a<=100 grade is E
else if 90<=a<=100 grade is A
else if 90<=a<=100 grade is P
else if 90<=a<=100 grade is D
else if 90<=a<=100 grade is T
Input Format
The locked stub code in your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments).
The first line contains , firstName, lastName and id , respectively. The second line contains the number of test scores. The third line of space-separated integers describes .
Constraints
1<=|FirstName|,|lastName|<=10
|id|= =7
0<= score,average<=100
Output Format
This is handled by the locked stub code in your editor. Your output will be correct if your Student class constructor and calculate() method are properly implemented.
Sample Input
Sans Sherpa 425698
2
70 90
Sample Output
Name: Sherpa, Sans
ID: 425698
Grade: E
Explanation
This student had 2 scores to average:70 and 90. The student's average grade is 80 . An average grade of corresponds to the letter grade , so our calculate() method should return the character'E'.
Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.
Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.
HAPPY CODING 😁
Solution:
( 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; public Student(String firstName,String lastName,int id,int []testScores){ super(firstName,lastName,id); this.testScores=testScores; this.firstName = firstName; this.lastName = lastName; this.idNumber = id; } public char calculate(){ int sum= 0; int a=0; for(int i=0;i<testScores.length;i++){ a=a+testScores[i]; } a=a/testScores.length; if(90<=a&&a<=100){ return 'O'; }else if(80<=a&&a<90){ return 'E'; }else if(70<=a&&a<80){ return 'A'; }else if(55<=a&&a<70){ return 'P'; }else if(40<=a&&a<55){ return 'D'; }else if(0<=a&&a<40){ return 'T'; } else return Character.MIN_VALUE; } } 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()); } }
If you need solutions in other programming languages or have any doubts regarding this problem then leave a comment down below .
in c++ please
ReplyDeletei recommend you to learn the basics of particular language first and try solving the problems.
ReplyDeleteAs per you request take the solution in c++
#include
#include
#include
#include
#include
using namespace std;
/*
*
* CodingHumans 2020
* https://www.codinghumans.xyz
*
*/
class Student{
protected:
string firstName;
string lastName;
int phone;
public:
Student(string fname,string lname,int p){
firstName=fname;
lastName=lname;
phone=p;
}
void display(){
cout<<"First Name: "score < 40) return 'D';
else if(this->score >= 40 && this->score < 60) ch = 'B';
else if(this->score >= 60 && this->score < 75) ch = 'A';
else if(this->score >= 75 && this->score < 90) ch = 'E';
else if(this->score >= 90 && this->score <= 100) ch = 'O';
return ch;
}
};
int main() {
string firstName,lastName;
int score,phone;
cin>>firstName;
cin>>lastName;
cin>>phone;
cin>>score;
Student *stu=new Grade(firstName,lastName,phone,score);
stu->display();
Grade *g=(Grade*)stu;
cout<< "\nGrade: "<calculate();
return 0;
}