Question

In: Computer Science

JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...

JavaScript - Create a class using "names" as the identifier.

Create a constructor. The constructor must have elements as follow:

first ( value passed will be String )
last ( value passed will be String )
age ( value passed will be Numeric )

The constructor will assign the values for the three elements and should use the "this" keyword

Create a function, using "printObject" as the identifier

printObject:

This function will have three input parameters:

allNames , sortType, message

Will sort the array of objects (allNames) using sortType as the key to how the arrays objects will be sorted.

These sort functions, are higher order functions, one required for each sort option except none. A higher order function type is calling a function with an argument that is a function. In this assignment, one would be using the builtin sort for arrays, allNames.sort( your function here ); This function will override the default sort order, built into array.sort()


The sort options are first , last , age, none.

Will print the message to the console


Will use a loop to print out the array by index ascending and display the values located in the elements: first last age, for each index accessed

Create a function main, no input parameters


main:


Create the empty array using the identifier "allNames"

Using the push() method, load 4 new objects of names to the array allNames, populating the fields first , last , age ( data is of your choosing, for example: allNames.push( new names("Cammo", "Hammer", 39));


Call printObject, passing the array , sort method as a string ( options are first, last, age, none )

Last line of the script, would be the call to the main

Note: It is required to use the ES6 style of keywords => instead of the older function and for variables use the keyword let, not var, class and within the constructor, not function.

Solutions

Expert Solution

class Names {

  constructor(first, last, age) {

    this.first = first;

    this.last = last;

    this.age = age;

  }

}

function printObject(allNames, sortType, message) {

  if (sortType == "first") {

    allNames.sort(function(a, b) {

      return a.first.localeCompare(b.first);

    });

  }

  if (sortType == "last") {

    allNames.sort(function(a, b) {

      return a.last.localeCompare(b.last);

    });

  }

  if (sortType == "age") {

    allNames.sort(function(a, b) {

      return a.age - b.age;

    });

  }

  for (let i = 0; i < allNames.length; i++) {

    console.log(message);

    console.log(

      allNames[i].first + " " + allNames[i].last + " " + allNames[i].age

    );

  }

}

function main() {

  var allNames = [];

  allNames.push(new Names("Cammo", "Hammer", 39));

  allNames.push(new Names("Jhon", "Doe", 50));

  allNames.push(new Names("Liza", "Helen", 70));

  allNames.push(new Names("Anthony", "Jhonson", 30));

  allNames.push(new Names("Zakir", "Nayak", 25));

  printObject(allNames, "age", "Message");

}

main();


Related Solutions

Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
Write the code to create a class Square implementing the interface Figure with constructor to initialize...
Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s...
Create an IceCreamConeException class whose constructor recetves a String that consists of an ice creams cone’s flavour and the number of scoops Create an IceCreamCone class with two fields - flavor and scoops The IceCreamCone constructor calls two data entry methods — getFlavor() and getScoops() The getScoops() method throws an IceCreamConeException when the scoop quantity exceeds 3 Write a program that establish three TceCreamCone objects and handles the Exception
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment...
Create an ApartmentException class whose constructor receives a string that holds a street address, an apartment number, a number of bedrooms, and a rent value for an apartment. Upon construction, throw an ApartmentException if any of the following occur: • The apartment number does not consist of 3 digits • The number of bedrooms is less than 1 or more than 4 • The rent is less than $500.00 or over $2500 Write a driver class that demonstrates creating valid...
Java program Create a constructor for a class named Signal that will hold digitized acceleration data....
Java program Create a constructor for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp data The constructor...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT