Question

In: Computer Science

At the question 3 without using inheritance, how can I access to the both constructors, Student...

At the question 3 without using inheritance, how can I access to the both constructors, Student & Course with only one prototype method displayStudents to print out all of the both properties?

The JavaScript code defines a constructor function for a Student class.

  1. Add a Course constructor function that takes a title as a parameter. The Course class should have 2 properties: a title and an array of students. The constructor function should initialize the title property to the title parameter and initialize the students array to an empty array.
  2. Add a method to the constructor function called addStudent that takes a single student parameter. The addStudent()method should push the student to the back of the students array property.
  3. Add a prototype method called displayStudents that takes no parameters. The displayStudents() method should output the course title and all students' names and GPAs to the console.
  4. Instantiate 2 different courses with any titles, and add a few students (Student objects) to the courses using the addStudent() method.
  5. Display all the students in the 2 courses using the displayStudents() method.

given:

function Student(name, gpa) {
this.name = name;
this.gpa = gpa;}

Solutions

Expert Solution

//=======Student.js=====================
class Student{
   constructor(name,GPA){
       this.name=name;
       this.GPA=GPA;
   }
   getName(){
       return this.name;
   }
   getGPA(){
       return this.GPA;
   }
   setName(name){
       this.name=name;
   }
   setGPA(GPA){
       this.GPA=GPA;
   }
}
//_____________________
class Course{
   constructor(title){
       this.title=title;
       this.total=0;
       this.StudentArray={};
   }
   getTitle(){
       return this.title;
   }
   addStudent(stud){
       this.StudentArray[this.total]=stud;
       this.total=this.total+1;
   }
   displayStudents(){
       var i;
       console.log(this.title);
       console.log("_____________")
       for(i=0;i<this.total;i++){
           console.log(this.StudentArray[i].getName());
           console.log(this.StudentArray[i].getGPA())
           console.log("--------------")
       }
       console.log("_____________")
   }
}
//_________________________

let C1=new Course("Java");
let s1=new Student("Tom",6.7);
let s2=new Student("Alice",8.7);
let s3=new Student("Ronnie",9.0);
C1.addStudent(s1);
C1.addStudent(s2);
C1.addStudent(s3);

C1.displayStudents();

let C2=new Course("Algorithm");
let s4=new Student("Mourice",6.88);
let s5=new Student("Anita",7.7);
let s6=new Student("Alite",9.6);
C2.addStudent(s4);
C2.addStudent(s5);
C2.addStudent(s6);

C2.displayStudents();

//===========end of Student.js==================
//output

C:\Program Files\nodejs\node.exe Student.js
Java
_____________
Tom
6.7
--------------
Alice
8.7
--------------
Ronnie
9
--------------
_____________
Algorithm
_____________
Mourice
6.88
--------------
Anita
7.7
--------------
Alite
9.6
--------------
_____________


Related Solutions

How does maternal inheritance differ from X-linked inheritance? How can you distinguish both in pedigrees and...
How does maternal inheritance differ from X-linked inheritance? How can you distinguish both in pedigrees and experimentally?
How can I quanitfy DNA accurately without using a nanodrop? (Provide an equation)
How can I quanitfy DNA accurately without using a nanodrop? (Provide an equation)
How to calculate Traffic LOS '' Level of service '' without using FFS. I can use...
How to calculate Traffic LOS '' Level of service '' without using FFS. I can use collected volume, and how can I do that ? and what are the information's needed ?
How can you find the number of pairs of alleles involved in polygenic inheritance by using...
How can you find the number of pairs of alleles involved in polygenic inheritance by using the number of phenotypic forms of the trait they condition?
Please try to type your solution for this question, so I can read it without a...
Please try to type your solution for this question, so I can read it without a problem. I truly appreciate you for typing in advance. The Question: An FBI survey shows that about 80% of all property crimes go unsolved. Suppose that in your town 3 such crimes are committed and they are each deemed independent of each other. X is the number of crimes will be solved in your town. Complete the table below for the probability mass function...
Please try to type your solution for this question, so I can read it without a...
Please try to type your solution for this question, so I can read it without a problem. I truly appreciate you for typing in advance. The Question: For some Mechanical Engineer students, data for 224 student’s GPAs in their first 4 semesters of college were compared using several predictors, namely HSM, HSS, HSE, SATM, and SATV. Here significant level α is 0.05. Then, the researcher fit a regression model and got the following results (intercept is included in the model):...
without using excel can someone show me the steps to approach the following question Question 2...
without using excel can someone show me the steps to approach the following question Question 2 Rump Industries Ltd expects its new product will give it a significant first mover advantage in the market and that is expected to provide growth in earnings per share of 400% within the coming year, and 75% growth in each of the subsequent 3 years. After that time, it is expected competitors will have developed and brought to market similar products with the result...
Using the below question and values how do you calculate the standard deviation without using a...
Using the below question and values how do you calculate the standard deviation without using a calculator (by hand) of (s1^2/n1+s2^2/n2) with the standard deviation answer being 1.4938 and 1.1361 /sqrt(1.4938^2/10 + 1.1361^2/10) Choose two competing store and compare the prices of 10 items at both stores. Preform a two-sample hypothesis test to try an prove if one store is more expensive than the other based on your sample. Store A item #1-$5.49, #2 $3.77, #3 $0.87, #4 $3.29, #5...
Detect 3 or more consecutive 1’s. for this question , how can i make a state...
Detect 3 or more consecutive 1’s. for this question , how can i make a state diagram and state table ? based on what ? also, how can i know the state S0 will go for s2 or S0 for S1 and so on?
The question I have is that I want to draw without having to drag and then...
The question I have is that I want to draw without having to drag and then clicking the button whether I want a line or rectangle. It should work first by hitting either the line or rectangle button and then making a drag on the panel with mouse, then after releasing the drag it creates the graphic. Here is my current code. package Mod1; import java.awt.*; import java.awt.event.*; import javax.swing.*; class paintGUI extends JComponent { // Image in which we're...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT