Question

The following code is given for the class Student: class Student{ var choice1, choice2, name; var...

The following code is given for the class Student:

class Student{

var choice1, choice2, name;

var numberOfChoices = 0;

constructor(studentName){

this.choice1 = null;

this.choice2 = null;

this.choice3 = null;

this.name = studentName;

}

getName = function(){

return this.name;

};

setChoice = function(c){

if(numberOfChoices ==0){

this.choice1 = c;

numberOfChoices++;

}

else{this.choice2 =c;

};

getChoice1 = function(){

return this.choice1();

};}

Write a section of code that creates a new StudentChoice object with name "Tom". Then set choice1 to "Algebra Review";

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<!DOCTYPE html>
<html>
<body>

   <h2>Student Class</h2>
   <p id="demo"></p>
   
   <script>
      class Student{
         //var this.choice1, this.choice2, this.name;
         //var numberOfChoices = 0;
         
         constructor(studentName){
            this.choice1 = null;
            this.choice2 = null;
            this.choice3 = null;
            this.name = studentName;
            this.numberOfChoices = 0;
         }
         
         getName = function(){
            return this.name;
         };
         
         setChoice = function(c){
            if(this.numberOfChoices == 0){
               this.choice1 = c;
               this.numberOfChoices++;
            }
            else{
               this.choice2 = c;
            };
         };
         
         getChoice1 = function(){
            return this.choice1;
         };
      }

      StudentChoice = new Student("Tom");
      StudentChoice.setChoice("Algebra Review");

      document.getElementById("demo").innerHTML = StudentChoice.getName() + ": " + StudentChoice.getChoice1();
   </script>

</body>
</html>

Note: Some minor fixes have been made to the class, for queries, drop me a comment.

Add a comment
Know the answer?
Add Answer to:
The following code is given for the class Student: class Student{ var choice1, choice2, name; var...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the code below write a public static main() method in class Student that: - creates...

    For the code below write a public static main() method in class Student that: - creates an ArrayList<Student> object called students - adds 4 new Student objects to the students list, with some made up names and dates - sort the students list by name and display the sorted collection to System.out. use function getCompByName() - sort the students list by enrollment date and display the sorted collection to System.out. use function getCompByDate() import java.util.Comparator;    import java.util.Date;    public...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

  • How to convert this JavaScript code to C#? / Constants (Type) var TYPE_EUI64 = 'EUI-64'; var...

    How to convert this JavaScript code to C#? / Constants (Type) var TYPE_EUI64 = 'EUI-64'; var TYPE_RA28 = 'RA-28'; var TYPE_ADVA48 = 'ADVA-48'; var TYPE_RADIO_PAYLOAD = 'RadioPayload'; var TYPE_UNDEFINED = 'Undefined'; // Constants var REELYACTIVE_OUI36 = '001bc5094'; /** * Identifier Class * Represents an identifier * @param {string} type Type of identifier. * @param {Object} value The value of the given identifier. * @constructor */ function Identifier(type, value) { var isValue = (value != null); // Constructor for EUI-64 if((type...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src...

    Set-Up · Create a new project in your Eclipse workspace named: Lab13 · In the src folder, create a package named: edu.ilstu · Import the following files from T:\it168\Labs\lab13. Note that the .txt file must be in the top level of your project, not inside your src folder. o Student.java o StudentList.java o students.txt Carefully examine the Student.java and StudentList.java files. You are going to complete the StudentList class (updating all necessary Javadoc comments), following the instruction in the code....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT