Objective:-Create a class called Person with the following properties and methods:Properties:-- name (string)- age (number)- gender (string)Methods:- speak() - logs a message to the console saying "Hello, my name is [name] and I am [age] years old."- Create a class called Student with the following properties and methods:-Properties:1- person (object) - an object that contains the "name", "age", and "gender" properties of the student2- major (string)3- gpa (number)-Methods:1- study() - logs a message to the console saying "I am studying [major] and my GPA is [gpa]."2- speak() - logs a message to the console saying "Hello, my name is [name] and I am [age] years old. I am also a student studying [major]."Input :-const person1 = new Person("John",20,"M");person1.speak();const stud1 = new Student(person1,"CS",9);stud1.speak();stud1.study();Output:-Hello, my name is John and I am 20 years old.Hello, my name is John and I am 20 years old. I am also a student studying CS.I am studying CS and my GPA is 9.
Question
Objective:-Create a class called Person with the following properties and methods:Properties:-- name (string)- age (number)- gender (string)Methods:- speak() - logs a message to the console saying "Hello, my name is [name] and I am [age] years old."- Create a class called Student with the following properties and methods:-Properties:1- person (object) - an object that contains the "name", "age", and "gender" properties of the student2- major (string)3- gpa (number)-Methods:1- study() - logs a message to the console saying "I am studying [major] and my GPA is [gpa]."2- speak() - logs a message to the console saying "Hello, my name is [name] and I am [age] years old. I am also a student studying [major]."Input :-const person1 = new Person("John",20,"M");person1.speak();const stud1 = new Student(person1,"CS",9);stud1.speak();stud1.study();Output:-Hello, my name is John and I am 20 years old.Hello, my name is John and I am 20 years old. I am also a student studying CS.I am studying CS and my GPA is 9.
Solution
The language of the text is JavaScript. Here is the step by step solution:
Step 1: Create a class called Person with properties name, age, and gender.
class Person {
constructor(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
Step 2: Add a method speak() to the Person class.
class Person {
constructor(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
speak() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
Step 3: Create a class called Student with properties person, major, and gpa.
class Student {
constructor(person, major, gpa) {
this.person = person;
this.major = major;
this.gpa = gpa;
}
}
Step 4: Add methods study() and speak() to the Student class.
class Student {
constructor(person, major, gpa) {
this.person = person;
this.major = major;
this.gpa = gpa;
}
study() {
console.log(`I am studying ${this.major} and my GPA is ${this.gpa}.`);
}
speak() {
console.log(`Hello, my name is ${this.person.name} and I am ${this.person.age} years old. I am also a student studying ${this.major}.`);
}
}
Step 5: Create instances of Person and Student and call their methods.
const person1 = new Person("John", 20, "M");
person1.speak();
const stud1 = new Student(person1, "CS", 9);
stud1.speak();
stud1.study();
This will output:
Hello, my name is John and I am 20 years old.
Hello, my name is John and I am 20 years old. I am also a student studying CS.
I am studying CS and my GPA is 9.
Similar Questions
What is the correct syntax to create a class named Student that will inherit properties and methods from a class named Person?
Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
Define a class StudentDefine __init__ method with arguments self, name and age.Inside the __init__ method, set self.name = name, self.age = age.Take name and age from the console by calling input and store them in the variables s1_name, s1_ageCreate an instance Stud_1 of class Student by passing the values as argumentsTake name and age from the console by calling input and store them in the variables s2_name, s2_ageCreate an instance Stud_2 of class Student by passing the values as argumentsPrint Stud_1.namePrint Stud_2.name
Student class is defined with public variables name, age, group and reportDefine setDetails() method that sets the student name, age, group and report values.Define getDetails(self) method that prints all the details of the student.Create an instance of class StudentSet the name, age, group and report values of the student by taking the input from the student and call the setDetails() valuesNow print the details of Student by calling the method showDetails()Sample Test Cases
2. A Course, Getters, and SettersmandatoryImplement a class named HolbertonCourse:Constructor attributes:name (String)length (Number)students (array of Strings)Make sure to verify the type of attributes during object creationEach attribute must be stored in an “underscore” attribute version (ex: name is stored in _name)Implement a getter and setter for each attribute.bob@dylan:~$ cat 2-main.jsimport HolbertonCourse from "./2-hbtn_course.js";const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"])console.log(c1.name);c1.name = "Python 101";console.log(c1);try { c1.name = 12;} catch(err) { console.log(err);}try { const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);}catch(err) { console.log(err);}bob@dylan:~$ bob@dylan:~$ npm run dev 2-main.js ES6HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ]}TypeError: Name must be a string ...TypeError: Length must be a number ...bob@dylan:~$
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.