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:...
(Please use java to write these questions) Q1. Create a class on an object Computer with...
(Please use java to write these questions) Q1. Create a class on an object Computer with two fields and two methods as follows: (5 points) field: cpu and memory, method: compile( ) and execute( ) Q2. Write a source code to activate a class created in Q1. (5 points)
Create your own date class. Create a MyDate class with these methods. A constructor that accepts...
Create your own date class. Create a MyDate class with these methods. A constructor that accepts a month, day, and year A method that determines if the year is a leap year A method that returns the date in this format, "1/9/2020" A method that returns the date in this format, "January 9, 2020" A method that returns the day A method that returns the name of the month A method that returns the number of days in the month...
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 an ExtLinkedList class by extending the LinkedList class to include the following methods and estimate...
Create an ExtLinkedList class by extending the LinkedList class to include the following methods and estimate the run-time complexity of each method. 3. public ExtLinkedList evenList() // gets the elements E in locations 0,2,4,6,… of this list and puts them in a new ExtLinkedList in that order and returns it // make sure to handle errors 4. public ExtLinkedList intsersect (ExtLinkedList eli) // picks out elements that are common to this ExtLinkedList and eli // and returns them in the...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
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...