Tính chu vi - diện tích hình tròn trong C, C# - Code Lập trình hướng đối tượng

Loading...
Bài tập tính chu vi, diện tích hình tròn trong C# mình làm theo hướng dẫn của thầy lập trình hướng đối tượng. Trong bài này những hàm phải xây dựng cho lớp Điểm (tọa độ): Nhập điểm, Cập nhật điểm (hoành độ, tung độ, ký tự điểm). Bên lớp hình tròn kế thừa lại lớp điểm để xây dựng thêm các hàm khác như: Tâm (tọa độ), tính chu vi hình tròn, tính diện tích hình tròn...
Tính chu vi - diện tích hình tròn trong C

Cụ thể:
Class Diem
Code:

public class Diem
{
// Khai bao thuoc tinh
private char kitu;
private int x,y;
// Phuong thuc nhap
public Diem() //Phuong thuc khoi tao khong co tham so
{
this.kitu='O';
this.x=this.y=0;
}
public Diem(char ch, int a, int b) // Phuong thuc khoi tao co tham so
{
this.kitu=ch;
this.x=a;
this.y=b;
}
public void Nhap()
{
Console.Write("\t- Diem: ");
Console.ReadLine();
Console.Write("\t- Tung do: ");
this.x=int.Parse(Console.ReadLine());
Console.Write("\t- Hoanh do: ");
this.y=int.Parse(Console.ReadLine());
}
public void Capnhapdiem(char ch)
{
this.kitu = ch;
}
public void Capnhaphoanhdo(int a)
{
this.x = a;
}
public void Capnhattungdo(int b)
{
this.y = b;
}
public char Dockitu()
{
return this.kitu;
}
public int Dochoanhdo()
{
return this.x;
}
public int Doctungdo()
{
return this.y;
}
public void Xuat()
{
Console.Write(kitu+"("+this.x+","+this.y+")");
}
}

Class HinhTron:Diem (lớp hình tròn kế thừa lớp điểm)
Code:

public class HinhTron:Diem
{
// Thuoc tinh
private double bankinh;
// Cac phuong thuc
public HinhTron():base()
{
this.bankinh = 0;
}
public HinhTron(int r):base()
{
this.bankinh = r;
}
public HinhTron(char ch, int a, int b, double r):base(ch,a,b)
{
this.bankinh = r;
}
public void Nhap()
{
Console.WriteLine("\t\n- Cac toa do cua tam");
base.Nhap();
Console.Write("\t - Ban kinh hinh tron: ");
this.bankinh = double.Parse(Console.ReadLine());
}
public void Capnhapbankinh(double r)
{
this.bankinh = r;
}
public double Docbankinh()
{
return this.bankinh;
}
public double Dientich()
{
return Math.PI * this.bankinh * this.bankinh;
}
public double Chuvi()
{
return 2 * Math.PI * this.bankinh;
}
public void Xuat()
{
Console.Write("\t Tam: ");
base.Xuat();
Console.WriteLine("\t Ban kinh: "+ this.bankinh);
Console.ReadLine();
}
public void Menu(out byte chon)
{
Console.Clear();
Console.WriteLine("\t||--------------------------------------------||");
Console.WriteLine("\t|| 1. - Nhap thong tin hinh tron ||");
Console.WriteLine("\t|| 2. - Cap nhap diem ||");
Console.WriteLine("\t|| 3. - Cap nhap hoanh do ||");
Console.WriteLine("\t|| 4. - Cap nhap tung do ||");
Console.WriteLine("\t|| 5. - Cap nhap ban kinh ||");
Console.WriteLine("\t|| 6. - Tinh chu vi hinh tron ||");
Console.WriteLine("\t|| 7. - Tinh dien tich hinh tron ||");
Console.WriteLine("\t|| 8. - Xuat thong tinh hinh tron ||");
Console.WriteLine("\t|| 0. - Ve menu chinh ||");
Console.WriteLine("\t||--------------------------------------------||");
Console.Write("\n\t\t NHAP LUA CHON: ");
chon5 = byte.Parse(Console.ReadLine());
}
public void Main()
{
HinhTron HT = new HinhTron();
//HT.NhapHT();
//double DTich;
//DTich = HT.Dientich();
//Console.Write("Dien tich hinh tron vua nhap la: {0}", DTich);
byte chon;
char kitu1;
int a, b;
double r1;
do
{
Menu5(out chon);
switch (chon)
{

case 1:
Console.Clear();
HT.Nhap();
break;
case 2:
Console.Clear();
Console.Write("\t= Nhap ten diem: ");
kitu1 = char.Parse(Console.ReadLine());
HT.Capnhapdiem(kitu1);
break;
case 3:
Console.Write("\t= Nhap hoanh do: ");
a = int.Parse(Console.ReadLine());
HT.Capnhaphoanhdo(a);
break;
case 4:
Console.Write("\t= Nhap tung do: ");
b = int.Parse(Console.ReadLine());
HT.Capnhattungdo(b);
break;
case 5:
Console.Write("\t= Nhap ban kinh r = ");
r1 = int.Parse(Console.ReadLine());
HT.Capnhapbankinh(r1);
break;
case 6:
Console.Clear();
double P;
P = HT.Chuvi();
Console.Write("\t\n Chu vi hinh tron la: " + P.ToString("0.000")); // xử lý cắt chuỗi
Console.ReadLine();
break;
case 7:
Console.Clear();
double S;
S = HT.Dientich();
Console.Write("\t\n Dien tich hinh tron la: " + S);
break;
case 8:
Console.Clear();
Console.Write("Thong tin hinh tron \n");
HT.Xuat();
break;
case 0:
Console.Clear();
break;
default:
Console.WriteLine("\n\t Khong co lua chon nay !");
break;

}
} while (chon != 0);
}
}

Mình đưa toàn bộ code lên, có thể hơi dài nhưng tiện cho các bạn copy and run ^_^
--------------------
Chúc các bạn học tập tốt!
Loading...
Chuyên mục: ,

0 nhận xét:

Đăng một bình luận