Sunteți pe pagina 1din 2

CSC301 TUTORIAL 3

1.

using System;

namespace test
{
class MainClass
{
public static void Main (string[] args)
{
Rectangle rect1 = new Rectangle("Rect1", (float)3.0, (float)4.0);
printArea(rect1);
rect1.printWidthHeight();

Triangle triang1 = new Triangle("Triangle", (float)3.0, (float)4.0);


printArea(triang1);
triang1.printWidthHeight();

}
public static void printArea(Polygon poly) {
float area;
area = poly.calArea();
Console.WriteLine("{0}", area);
}

public class Polygon {


protected String name;
protected float width;
protected float height;

public Polygon(String theName, float theWidth, float theHeight) {


name = theName;
width = theWidth;
height = theHeight;
}
public abstract float calArea() {
return 0;
}
public void printWidthHeight() {
Console.WriteLine("Width={0}, Height={1}", width, height);
}

public class Rectangle : Polygon {


public Rectangle(String theName, float theWidth, float theHeight) :
base(theName,theWidth,theHeight) {

public override float calArea() {


return width * height;
}
}

public class Triangle : Polygon {


public Triangle(String theName, float theWidth, float theHeight) :
base(theName,theWidth,theHeight) {

public override float calArea() {


return 1/2 * width * height;
}

}
}

2.
(a) 7
(b) #
(c) 7
(d) (1 4 9 16)
(e) #

3 (a)
(i)

S-ar putea să vă placă și