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:~$
Question
- 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:~ bob@dylan:~
Solution
// Implementing the HolbertonCourse class
class HolbertonCourse {
// Constructor to initialize the attributes
constructor(name, length, students) {
// Type validation for name
if (typeof name !== 'string') {
throw new TypeError('Name must be a string');
}
// Type validation for length
if (typeof length !== 'number') {
throw new TypeError('Length must be a number');
}
// Type validation for students
if (!Array.isArray(students) || !students.every(student => typeof student === 'string')) {
throw new TypeError('Students must be an array of strings');
}
// Storing attributes with underscore prefix
this._name = name;
this._length = length;
this._students = students;
}
// Getter for name
get name() {
return this._name;
}
// Setter for name with type validation
set name(newName) {
if (typeof newName !== 'string') {
throw new TypeError('Name must be a string');
}
this._name = newName;
}
// Getter for length
get length() {
return this._length;
}
// Setter for length with type validation
set length(newLength) {
if (typeof newLength !== 'number') {
throw new TypeError('Length must be a number');
}
this._length = newLength;
}
// Getter for students
get students() {
return this._students;
}
// Setter for students with type validation
set students(newStudents) {
if (!Array.isArray(newStudents) || !newStudents.every(student => typeof student === 'string')) {
throw new TypeError('Students must be an array of strings');
}
this._students = newStudents;
}
}
// Testing the HolbertonCourse class
import HolbertonCourse from "./2-hbtn_course.js";
const c1 = new HolbertonCourse("ES6", 1, ["Bob", "Jane"]);
console.log(c1.name); // Output: ES6
c1.name = "Python 101";
console.log(c1); // Output: HolbertonCourse { _name: 'Python 101', _length: 1, _students: [ 'Bob', 'Jane' ] }
try {
c1.name = 12;
} catch (err) {
console.log(err); // Output: TypeError: Name must be a string
}
try {
const c2 = new HolbertonCourse("ES6", "1", ["Bob", "Jane"]);
} catch (err) {
console.log(err); // Output: TypeError: Length must be a number
}
Similar Questions
1. Let's make some classroomsmandatoryImport the ClassRoom class from 0-classroom.js.Implement a function named initializeRooms. It should return an array of 3 ClassRoom objects with the sizes 19, 20, and 34 (in this order).bob@dylan:~$ cat 1-main.jsimport initializeRooms from './1-make_classrooms.js';console.log(initializeRooms());bob@dylan:~$ bob@dylan:~$ npm run dev 1-main.js [ ClassRoom { _maxStudentsSize: 19 }, ClassRoom { _maxStudentsSize: 20 }, ClassRoom { _maxStudentsSize: 34 }]bob@dylan:~$
2. Extending the Teacher classmandatoryWrite an interface named Directors that extends Teacher. It requires an attribute named numberOfReports(number)Example:const director1: Directors = { firstName: 'John', lastName: 'Doe', location: 'London', fullTimeEmployee: true, numberOfReports: 17,};console.log(director1);// should print// Object// firstName: "John"// fullTimeEmployee: true// lastName: "Doe"// location: "London"// numberOfReports: 17
Create a function named getListStudents that returns an array of objects.Each object should have three attributes: id (Number), firstName (String), and location (String).The array contains the following students in order:Guillaume, id: 1, in San FranciscoJames, id: 2, in ColumbiaSerena, id: 5, in San Franciscobob@dylan:~$ cat 0-main.jsimport getListStudents from "./0-get_list_students.js";console.log(getListStudents());bob@dylan:~$ bob@dylan:~$ npm run dev 0-main.js [ { id: 1, firstName: 'Guillaume', location: 'San Francisco' }, { id: 2, firstName: 'James', location: 'Columbia' }, { id: 5, firstName: 'Serena', location: 'San Francisco' }]bob@dylan:~$
DescriptionYou need to create a Constructor function which will create object for Student details of the following properties:name: It will store student nameroll_no: Stores the roll numberclass: Stores the classsection: Stores the sectionmarks_of_5_subjects: It will store marks of science, maths, social science, english & hindi. You can store marks in this format:{ science: 72, maths: 75, social_science: 79, english: 80, hindi: 67}printTop3Subjects: It is a function method. It will print top three subject names based on marks.printReportCard: It is also a function method. It will create and print a report card of a student. For example:+--------------------+| REPORT CARD |+--------------------+| Name - Huzaifa || Roll no. - 16 || Class - X || Section - A || Science - 73 || S.Sc - 79 || Maths - 75 || English - 80 | | Hindi - 67 || Prectage - 74.8 % |+--------------------+
0. You used to attend a place like this at some pointmandatoryImplement a class named ClassRoom:Prototype: export default class ClassRoomIt should accept one attribute named maxStudentsSize (Number) and assigned to _maxStudentsSizebob@dylan:~$ cat 0-main.jsimport ClassRoom from "./0-classroom.js";const room = new ClassRoom(10);console.log(room._maxStudentsSize)bob@dylan:~$ bob@dylan:~$ npm run dev 0-main.js 10bob@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.