Hello,
Having trouble with a program for school. Prof gave a shell that has color declared as static. Here is what I have so far.
class MyRectangle
{
private double width = 1; //private class instance fields
private double height = 1;
private static String color = "white";
public MyRectangle(){ } //empty constructor
public MyRectangle(double widthParam, double heightParam, String colorParam)
{
width = widthParam; // passed in values assigned
height = heightParam; //to class variables
color = colorParam;
}
public double getWidth(double widthParam)
{
return width;
}
public double getHeight(double heightParam)
{
return height;
}
public static String getColor(String colorParam)
{
return MyRectangle.color;
}
public double findArea()
{
double area = width * height;
return area;
}
public void OutputArea()
{
Console.WriteLine("Width = {0}\n" + "Height = {1}\n" +
"Color = {2}\n" + "Area = {3}\n",
width, height, color, width*height);
}
}//end rectangle class
class rectangle_assignment //wrapper class for main
{
static void Main(string[] args)
{
MyRectangle shape1 = new MyRectangle(3, 5, "red"); //instantiates object
MyRectangle shape2 = new MyRectangle(2, 5, "yellow"); //instantiates object
shape1.findArea();
shape1.OutputArea();
Console.WriteLine();
shape2.findArea();
shape2.OutputArea();
}//end main
Write the code in main to create two MyRectangle objects. Assign a width and height to each of the two objects using the constructors. Assign the first object the color red, and the second, yellow. Display all properties of both objects including their area. The output should look something like this:
Width = 3
Height = 5
Color = red
Area = 15
Width = 2
Height = 5
Color = yellow
Area = 10
Press any key to continue . . .
I have everything working and printing the way it is supposed to . . . problem is, the color is yellow for both shapes. I know it has something to do with static. I have debugged and the first shape has its color as red until the yellow is passed, then both shapes are yellow.
Please help. Thanks.