Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create class Rectangle. The class has atributes length and width, each of which

ID: 3539326 • Letter: C

Question

Create class Rectangle. The class has atributes length and width, each of which defaults to 1. It has read-only properties for both length and width. The set accessors should verify that length and width are each floating point numbers greater than 0.0 and less than 20.0. WRITE AN APPLICATION TO TEST CLASS RECTANGLE!


I am pretty sure I have the Rectangle class down, but I am confused on how to test it. Do I create an entirely seperate application with a main().? Points will only be rewarded for those who answer this question correctly. Thanks!

Explanation / Answer

//HIGHLIGHTED CHANGES IN BOLD using System; using System.Linq; using System.Text; using System.Collections.Generic; class Rectangle { private double length; private double width; public Rectangle() { length = 1; width = 1; } public Rectangle(double l, double w) { this.setLength(l); this.setWidth(w); } public void setWidth(double w) { if (w > 0.0 && w < 20.0) { width = w; } else { Console.WriteLine("The width must be greater than 0.0 and less than 20.0"); Console.WriteLine("The width was reset to 1 by default"); width = 1; } } public void setLength(double l) { if (l > 0.0 && l < 20.0) { length = l; } else { Console.WriteLine("The length must be greater than 0.0 and less than 20.0"); Console.WriteLine("The length was reset to 1 by default"); length = 1; } } public double getLength() { return length; } public double getWidth() { return width; } public double getPerimeter() { return 2 * (length + width); } public double getArea() { return length * width; } //main function code public static void Main() { //initialize rectangle object Rectangle r1=new Rectangle(); double len,wid; //prompt user Console.WriteLine("Enter length of Rectangle:"); len= double.Parse(Console.ReadLine()); Console.WriteLine("Enter Width of Rectangle:"); wid= double.Parse(Console.ReadLine()); //set length and width r1.setLength(len); r1.setWidth(wid); Console.WriteLine("Perimeter of Rectangle is :"+r1.getPerimeter()); Console.WriteLine("Area of Rectangle is :"+r1.getArea()); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote