Rectangle
width ivars
height
constructor(aWidth, aHeight) { //Initialize object structure this.width = aWidth; this.height = aHeight; }
getWidth() { //Getter to get and return ivar "width" return this.width; }
getHeight() { //Getter to get and return ivar "height" return this.height; }
setWidth(value) { //Getter to assign passed param "value" to ivar "width" this.width = value; }
setHeight(value) { //Getter to assign passed param "value" to ivar "height" this.height = value; }
getArea() { //Compute and return area return this.width * this.height; }
getPerimeter() { //Compute and return perimeter return 2 * (this.width + this.height); }
getDiagonal() { //Compute and return diagonal const w = this.width, h = this.height; return Math.sqrt(w*w + h*h); }
getShapeType() { //Return "square, wide rectangle, or narrow rectangle" as appropriate const w = this.width, h = this.height; if (w === h) { return "square"; } return (w > h) ? "wide rectangle" : "narrow rectangle"; }
flip() { //Flip width and height const w = this.width; this.width = this.height; this.height = w; }
multiply(factor) { //Increase dimensions by multiplying them by passed param this.width = this.width * factor; this.height = this.height * factor; }
toString() { //Friendly display string return `width=${this.width} by height=${this.height}`; }
Rectangle: width=10 by height=5 Rectangle Area: 50 Rectangle Shape Type: wide rectangle
//object play function prn(o) { console.log(o); } let rec = new Rectangle(10, 5); prn("\nRectangle: " + rec.toString()); prn("Rectangle Area: " + rec.getArea()); prn("Rectangle Perimeter: " + rec.getPerimeter()); prn("Rectangle Diagonal: " + rec.getDiagonal()); prn("Rectangle Shape Type: " + rec.getShapeType()); rec = new Rectangle(1000, 1000); prn("\nRectangle: " + rec.toString()); prn("Rectangle Area: " + rec.getArea()); prn("Rectangle Perimeter: " + rec.getPerimeter()); prn("Rectangle Diagonal: " + rec.getDiagonal()); prn("Rectangle Shape Type: " + rec.getShapeType());
/* Rectangle.js See associated document ("407 - Classes" - How to Code a Class) for detailed explanation of this class. */ class Rectangle { /*ivars (listed here for info only): width height*/ //------------------------------------------------ constructor(aWidth, aHeight) { //Initialize object structure this.width = aWidth; this.height = aHeight; } //------------------------------------------------ //Getters (Basic) getWidth() { //Getter to get and return ivar "width" return this.width; } getHeight() { //Getter to get and return ivar "height" return this.height; } //------------------------------------------------ //Setters setWidth(value) { //Getter to assign passed param "value" to ivar "width" this.width = value; } setHeight(value) { //Getter to assign passed param "value" to ivar "height" this.height = value; } //------------------------------------------------ //Calcs getArea() { //Compute and return area return this.width * this.height; } getPerimeter() { //Compute and return perimeter return 2 * (this.width + this.height); } getDiagonal() { //Compute and return diagonal const w = this.width, h = this.height; return Math.sqrt(w*w + h*h); } getShapeType() { //Return "square, wide rectangle, or narrow rectangle" as appropriate const w = this.width, h = this.height; if (w === h) { return "square"; } return (w > h) ? "wide rectangle" : "narrow rectangle"; } //------------------------------------------------ //Actions flip() { //Flip width and height const w = this.width; this.width = this.height; this.height = w; } multiply(factor) { //Increase dimensions by multiplying them by passed param this.width = this.width * factor; this.height = this.height * factor; } //------------------------------------------------ //Common toString() { //Friendly display string return `width=${this.width} by height=${this.height}`; } } //object play function prn(o) { console.log(o); } let rec = new Rectangle(10, 5); prn("\nRectangle: " + rec.toString()); prn("Rectangle Area: " + rec.getArea()); prn("Rectangle Perimeter: " + rec.getPerimeter()); prn("Rectangle Diagonal: " + rec.getDiagonal()); prn("Rectangle Shape Type: " + rec.getShapeType()); rec = new Rectangle(1000, 1000); prn("\nRectangle: " + rec.toString()); prn("Rectangle Area: " + rec.getArea()); prn("Rectangle Perimeter: " + rec.getPerimeter()); prn("Rectangle Diagonal: " + rec.getDiagonal()); prn("Rectangle Shape Type: " + rec.getShapeType());