4. Writing a classmandatoryWrite a Class named StudentClass:The constructor accepts firstName(string) and lastName(string) argumentsThe class has a method named workOnHomework that return the string Currently workingThe class has a method named displayName. It returns the firstName of the studentThe constructor of the class should be described through an InterfaceThe class should be described through an InterfaceRequirements:You can reuse the Webpack configuration from the previous exercise to compile the code.When running npm run build, no TypeScript error should be displayed.Every variable should use TypeScript when possible.
Question
- Writing a classmandatoryWrite a Class named StudentClass:The constructor accepts firstName(string) and lastName(string) argumentsThe class has a method named workOnHomework that return the string Currently workingThe class has a method named displayName. It returns the firstName of the studentThe constructor of the class should be described through an InterfaceThe class should be described through an InterfaceRequirements:You can reuse the Webpack configuration from the previous exercise to compile the code.When running npm run build, no TypeScript error should be displayed.Every variable should use TypeScript when possible.
Solution
Here is a step-by-step guide on how to write the class as per your requirements in TypeScript:
- First, we need to define the interfaces for the constructor and the class. The constructor interface will have
firstNameandlastNameproperties. The class interface will haveworkOnHomeworkanddisplayNamemethods.
interface IStudentConstructor {
firstName: string;
lastName: string;
}
interface IStudentClass {
workOnHomework(): string;
displayName(): string;
}
- Now, we can define the
StudentClassthat implements theIStudentClassinterface. The constructor of this class will acceptfirstNameandlastNameas arguments.
class StudentClass implements IStudentClass {
constructor(private student: IStudentConstructor) {}
workOnHomework(): string {
return "Currently working";
}
displayName(): string {
return this.student.firstName;
}
}
- Now, you can create an instance of
StudentClassand call its methods.
const student = new StudentClass({ firstName: 'John', lastName: 'Doe' });
console.log(student.workOnHomework()); // Outputs: Currently working
console.log(student.displayName()); // Outputs: John
- To ensure that there are no TypeScript errors, you can run
npm run build. If there are no errors, the code is correctly written.
Remember, every variable in TypeScript should have a type. In this case, firstName and lastName are of type string, and the methods workOnHomework and displayName return a string.
Similar Questions
1. Let's build a Teacher interfacemandatoryCreate a directory task_1 and copy these configuration files into this folder: package.json, tsconfig.json, webpack.config.jsfirstName(string) and lastName(string). These two attributes should only be modifiable when a Teacher is first initializedfullTimeEmployee(boolean) this attribute should always be definedyearsOfExperience(number) this attribute is optionallocation(string) this attribute should always be definedAdd the possibility to add any attribute to the Object like contract(boolean) without specifying the name of the attributeExample:const teacher3: Teacher = { firstName: 'John', fullTimeEmployee: false, lastName: 'Doe', location: 'London', contract: false,};console.log(teacher3);// should print// Object// contract: false// firstName: "John"// fullTimeEmployee: false// lastName: "Doe"// location: "London"
How do you configure the build process to compile and bundle your TypeScript code?Use a build tool like WebpackUse a build tool like GulpUse a build tool like GruntUse a build tool like Babel
The following TypeScript code has an error. Can you identify and correct it?class Animal { constructor(public name: string) {}}let dog = new Animal();console.log(dog.name);What is the error, and how would you correct it?
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
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.