Question

In: Computer Science

Description The primary purpose is to gain experience building a simple programmer-defined class. Requirements • Class...

Description
The primary purpose is to gain experience building a simple programmer-defined class.
Requirements
• Class Rectangle: Create a new class named Rectangle that
o is in its own Rectangle.cs file
o has properly formatted class header docs with your name as the author and a simple class description such as “Class Rectangle represents a rectangle with a length and width.”
o has private attributes (fields) named length and width that will hold integer values.
o has public properties named Length and Width that correspond to those attributes. The gets for Length and Width simply return the corresponding attribute. The sets will assign value to the attribute only if value is non-negative (>= 0)
• Class Program
o Copy/paste the code below and then
▪ Realign the code as needed
▪ Insert your name as the author
▪ Add code where it says ** ADD CODE **
o Run to ensure that your output matches the sample run
Program.cs
using System;
/**
* @author First and Last Name
* Class Program tests the Rectangle class. */
class Program
{
public static void Main()
{
// **ADD CODE **
// Create a new Rectangle object named aRectangle
// Then set the Length to 3 and the Width to 4
// Display the rectangle dimensions
Console.WriteLine($"Length: {aRectangle.Length}, " +
$"Width: {aRectangle.Width}");
// Test that negative values are ignored
aRectangle.Length = -2;
aRectangle.Width = -4;
Console.WriteLine($"Length: {aRectangle.Length}, " +
$"Width: {aRectangle.Width}");
} // end Main
} // end class
Sample Run Sample Run
Length: 3, Width: 4
Length: 3, Width: 4
Submission
• Upload the two source files: Program.cs and Rectangle.cs
• If in the on-campus section, also print the two source files before class and turn in at the beginning of class.

Solutions

Expert Solution

Dear Student ,

As per requirement submitted above kindly find below solution.

Here new console application in C# is created using visual studio 2019 with name "RectangleApplication". This application contains below classes.

Rectangle.cs :

//namespace
using System;
//application namespace
namespace RectangleApplication
{
//Class Rectangle represents a rectangle with a length and width.
class Rectangle //C# class
{
//private attributes (fields)
private int length;
private int width;
//public properties
public int Length
{
get { return this.length; }
set
{
//checking length
if (value >= 0)
{
this.length = value;
}
}
}
public int Width
{
get { return this.width; }
set
{
//checking width
if (value >= 0)
{
this.width = value;
}
}
}

}
}

************************************

Program.cs :

using System;//namespace
/**
* @author First and Last Name
* Class Program tests the Rectangle class. */
//application namespace
namespace RectangleApplication
{
class Program //C# class
{
//entry point of the application
static void Main(string[] args)
{
// **ADD CODE **
// Create a new Rectangle object named aRectangle
Rectangle aRectangle = new Rectangle();
// Then set the Length to 3 and the Width to 4
aRectangle.Length = 3;
aRectangle.Width = 4;
// Display the rectangle dimensions
Console.WriteLine($"Length: {aRectangle.Length}, " +
$"Width: {aRectangle.Width}");
// Test that negative values are ignored
aRectangle.Length = -2;
aRectangle.Width = -4;
Console.WriteLine($"Length: {aRectangle.Length}, " +
$"Width: {aRectangle.Width}");
}//end main method
}//end class
}

==================================

Output :Run application using F5 and will get the screen as shown below

Screen 1:Program.cs

NOTE :PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

In software defined infrastructure, what is the primary purpose of using a template and what impact...
In software defined infrastructure, what is the primary purpose of using a template and what impact does use of a template have on securing the environment? please give me a detailed in depth response thank you
Class, what is the primary purpose of investing in Stock Index Futures? Name any 3 advantages...
Class, what is the primary purpose of investing in Stock Index Futures? Name any 3 advantages of investing in Stock Index Futures vs. individual stocks.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT