Question

Please use javascript. Square Class Create a square class defined by the following Property side Methods...

Please use javascript.

Square Class

Create a square class defined by the following

  • Property
    • side
  • Methods
    • perimeter (side times 4)
    • area (side squared)
    • diagonal (square root of 2 * side squared)
    • describe – shows the squares information as follows:

Square with side 2 has perimeter of 8, area of 4, and diagonal of 2.828

Your program is to create three squares and use the describe method to show each squares information.

Homework Answers

Answer #1

class Square {
//constructor to set the side of the square
constructor(side){
this.side = side;
}

//method that returns perimeter of square
perimeter(){
return 4 * this.side;
}

//method that returns area of square
area(){
return this.side * this.side;
}

//method that returns diagonal of square
diagonal(){
return Math.sqrt(2 * this.side * this.side).toFixed(3);
}

//method that returns a string describing the square
describe(){
return "Square with side "+this.side+" has perimeter of "+this.perimeter()+", area of "+this.area()+", and diagonal of "+this.diagonal();
}
}

//creating square of side 2
square1 = new Square(2);
console.log(square1.describe());

//creating square of side 3
square2 = new Square(3);
console.log(square2.describe());

//creating square of side 4
square3 = new Square(4);
console.log(square3.describe());

---------------------------------------------------------------------------------------------------

Note: Comments have been added for better understanding of code.

Output of the code:

Code Snippet:

class Square {
  //constructor to set the side of the square
  constructor(side){
    this.side = side;
  }

  //method that returns perimeter of square
  perimeter(){
    return 4 * this.side;
  }

  //method that returns area of square
  area(){
    return this.side * this.side;
  }

  //method that returns diagonal of square
  diagonal(){
    return Math.sqrt(2 * this.side * this.side).toFixed(3);
  }

  //method that returns a string describing the square
  describe(){
    return "Square with side "+this.side+" has perimeter of "+this.perimeter()+", area of "+this.area()+", and diagonal of "+this.diagonal();
  }
}

//creating square of side 2
square1 = new Square(2);
console.log(square1.describe());

//creating square of side 3
square2 = new Square(3);
console.log(square2.describe());

//creating square of side 4
square3 = new Square(4);
console.log(square3.describe());

---------------------------------------------------------------------------------------------------

Hope this helps. If you like the answer, please upvote. Cheers!!

Know the answer?
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
Similar Questions
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
6) Consider the following class descriptions for objects in a video game: An NPC (non-player character)...
6) Consider the following class descriptions for objects in a video game: An NPC (non-player character) in a video game has some basic data that belongs to all entities in a game. All NPCs have a name, which is a String, health, which is an integer, and a description, which is a String. NPCs have accessor methods for each of their instance variables. NPCs also have an act() method, which is void; however, since there are no true NPCs in...
6) Consider the following class descriptions for objects in a video game: An NPC (non-player character)...
6) Consider the following class descriptions for objects in a video game: An NPC (non-player character) in a video game has some basic data that belongs to all entities in a game. All NPCs have a name, which is a String, health, which is an integer, and a description, which is a String. NPCs have accessor methods for each of their instance variables. NPCs also have an act() method, which is void; however, since there are no true NPCs in...