Page 1 of 1

I have posted this numerous times now and no one has answered it correctly. PLEASE READ ALL FIELDS. There are two parts

Posted: Fri May 20, 2022 3:40 pm
by answerhappygod
I have posted this numerous times now and no one has answered it
correctly. PLEASE READ ALL FIELDS. There are two parts to this.
PART 1 - please post the C# code EXACTLY to what the assignment
is asking (you will notice that is supposed to be in bad
programming, so just follow along)
PART 2 - this will be the better design of the code. Again,
please follow EXACTLY what is stated, naming the methods as they
are written in the assignment.
Thank you.
User-Define types – Classes
Classes are first-class citizen in C#. i.e. The classes that you
create have the same properties as the built-in ones that comes
with C# such as int, double and string. You may define classes
anywhere, even inside other classes (nested classes), however
defining them in separate file improves maintainability, stream
lines development and greatly simplifies access in your code.
This exercise comprises of two parts that is implemented in two
separate projects.
Part I
This first design is a poor one for many reasons that we will
see later in this lab and in this course.
Rectangle class (Original)
For now, a class is a user made-up data
type.
Create a project called BadRectangleDemo and code the
following type outside the existing Program class.
public class BadRectangle
{
//these are data members of type int
public int length;
public int width;
}
Creating methods that uses the Rectangle class
Completed the following programming tasks:
Write the following static methods in your Program
class:
Method: CreateRectangle()
Name: CreateRectangle
Returns: A BadRectangle object
Argument: int representing the width, int
representing the length
The dot operator allows to you access members of a
type.
Action:
Method: DescribeRectangle()
Name: DescribeRectangle
Returns: void
Argument: BadRectangle object
Action: Displays the width and length of the
Rectangle object
Method: CalculateAndDisplayArea()
Name: CalculateAndDisplayArea
Returns: void
Argument: BadRectangle object
Action:
In your main method write the code to do the following:
The new operator creates a new object.
What is wrong with the above Rectangle class?
Part II
This design is a better one because we will fix the above design
flaws and make the class easier to use.
Rectangle class (Modified)
Create another project called RectangleDemo and code the
following type outside the existing Program class.
public class Rectangle
{
//These type of data members are called fields.
//Notice the access level is restricted to private.
private int length;
private int width;
//Constructors have the same name as the class.
//Define like methods but no return type is
specified.
//It is called immediately after the object is
created.
//It is a good place to put code to make your object
useful.
public Rectangle(int len, int wid)
{
length = len;
width = wid;
}
//May contain action members that is normally called
methods.
public int GetArea()
{
return length * width;
}
//This method is called when you need to print an
object.
//Very useful when developing code.
public override string ToString()
{
return $"Length:{length}, width:{width}";
}
}
Write the code to exercise the Rectangle class
In your Main method of the existing Program class write the code
to do the following:
Create a rectangle object of length 8 and width 5.
Print the object.
Print the area of the object.
Create a rectangle object of length 5 and width 3.
Print the object.
Print the area of the object.
Create a rectangle object of length 20 and width 10.
Print the object.
Print the area of the object.
Enhancements to the Rectangle class
Can you list some features that are lacking in the last design
of the Rectangle class? Think of the operation that you would like
to do with "real" rectangle objects.
How to check if two objects are the same?
How to compare area?
Is position of the object important? What about
intersection?