Disini saya mendapatkan tugas dari asisten praktikum visual basic saya, berikut
soalnya:
Buat kelas
abstrak Shape dengan sebuah method abstrak bernama GetArea(). Definisikan juga
dua subkelas dari Shape dengan nama Rectangle dan Circle. Gunakan
rumus penghitungan luas untuk mengimplementasikan method GetArea().
Code:
Public
Class circle
Inherits shape
Dim radius As Double
Const phi =
3.14
Sub New(ByVal r As Decimal)
radius = r
End Sub
Public Overrides Function
getArea() As Double
area = phi * (radius * radius)
Return
area
End Function
End
Class
Public
Class rectangle
Inherits shape
Dim panjang
As Decimal
Dim lebar As Decimal
Sub New(ByVal l As Double, ByVal w As Double)
panjang = l
lebar = w
End Sub
Public Overrides Function
getArea() As Double
area = panjang * lebar
Return
area
End Function
End
Class
Public
MustInherit Class
shape
Protected
area As Double
Public MustOverride Function
getArea() As Double
End
Class
Public
Class Form1
Function
lingkaran(ByVal jari2)
Dim
myCircle As New
circle(jari2)
Dim
hasil As Double
hasil = myCircle.getArea
Return
hasil
End Function
Function
persegi(ByVal pj, ByVal
lb)
Dim
myRectangle As New
rectangle(pj, lb)
Dim
hasil As Double
hasil = myRectangle.getArea
Return
hasil
End Function
Private Sub Button1_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button1.Click
TextBox2.Text = CStr(lingkaran(CInt(TextBox1.Text)))
End Sub
Private Sub Button2_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
Button2.Click
TextBox5.Text = CStr(persegi(CInt(TextBox3.Text), CInt(TextBox4.Text)))
End Sub
End
Class
Hasil screenn shot:
0 comments:
Post a Comment