在本实验指导中,编写了一个对正方形提供绘制和操作的代码。在该代码中,正方形类的字段有正方形的边长和窗口上正方形的位置;正方形类的方法有绘制正方形和返回正方形连长、开始位置、周长、面积。
(1)启动Visual Studio .NET。创建一个名为rectu的控制台应用程序。
(2)添加类Point。该类的结构如下:
class Point
{
public int x;
public int y;
public Point(int nx, int ny)
{ }
public Point(Point n)
{ }
public void Set(int nx, int ny)
{ }
public void Set(Point n)
{ }
public int getx()
{ }
public int gety()
{ }
}
(3)在类Class1中添加字段、构造函数和方法,结构如下:
class Class1
{
private int side;
private Point location;
public Class1(Point location, int length)
{ }
public void Class(Point center,int length)
{ }
public void graph(Point center, int length)
{ }
public int getarea()
{ }
public int getperimeter()
{ }
public int getlength()
{ }
public Point getlocation()
{ }
}
(4)在主函数Main()中添加代码:
static void Main(string[] args)
{
int x, y, a;
Console.WriteLine("请输入正方形的边长及位置:");
Console.WriteLine("边长:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("位置:");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
Class1 sd = new Class1(new Point(x, y), a);
Console.WriteLine("Square:");
Console.WriteLine("Length=" + sd.getlength());
Console.WriteLine("Location=" + sd.getlocation().getx() + sd.getlocation().gety());
Console.WriteLine("Perimeter=" + sd.getperimeter());
Console.WriteLine("Area=" + sd.getarea());
}
最后是完整的程序源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Point
{
public int x;
public int y;
public Point(int nx, int ny)
{
Set(nx, ny);
}
public Point(Point n)
{
Set(n);
}
public void Set(int nx, int ny)
{
x = nx;
y = ny;
}
public void Set(Point n)
{
x = n.x;
y = n.y;
}
public int getx()
{
return x;
}
public int gety()
{
return y;
}
}
class Class1
{
private int side;
private Point location;
public Class1(Point location, int length)
{
this.side = length;
this.location = location;
graph(location,length);
}
public void Class(Point center,int length)
{
}
public void graph(Point center, int length)
{
for(int i = 0; i < center.y; i++)
Console.WriteLine(" ");
for (int i = 0; i < length; i++)
{
string line = " ";
for (int j = 0; j < center.x * 2 + length * 2 - 1; j++)
{
if (i == 0 || i == length - 1)
{
if (j >= center.x * 2 && (j - center.x * 2) % 2 == 0)
line += "*";
else line += " ";
}
else
{
if (j == center.x * 2 || j == center.x * 2 + length * 2 - 2)
line += "*";
else line += " ";
}
}
Console.WriteLine(line);
}
}
public int getarea()
{
return side * side;
}
public int getperimeter()
{
return side * 4;
}
public int getlength()
{
return side;
}
public Point getlocation()
{
return location;
}
static void Main(string[] args)
{
int x, y, a;
Console.WriteLine("请输入正方形的边长及位置:");
Console.WriteLine("边长:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("位置:");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
Class1 sd = new Class1(new Point(x, y), a);
Console.WriteLine("Square:");
Console.WriteLine("Length=" + sd.getlength());
Console.WriteLine("Location=" + sd.getlocation().getx() + sd.getlocation().gety());
Console.WriteLine("Perimeter=" + sd.getperimeter());
Console.WriteLine("Area=" + sd.getarea());
}
}
}