Anda di halaman 1dari 12

Nosy Khalista Primasari 09.4.00017 Sistem Informasi S1 Tugas PBO II Membuat Kalkuator 1.

. Kalkulator ini di desain dalam satu form bernama home, dengan menggunakan tab control, yaitu kalkulator sederhana dan kalkulator scientific yang bersifat inherits. a. Kalkulator sederhana.

Didalam kalkulator sederhana ini ada sebuah class bernama sederhana, yang didalamnya terdapat: Property : operan1 dan operan2. Function : tambah, kurang, kali, bagi, pangkat, persen, perx. Didalam form terdapat procedure untuk menghitung dan menghapus yaitu dipanggil di dalam button sama dengan untuk procedure hitung, dan di button C dan Backspace untuk procedure hapus. Setiap function yang ada di dalam class sederhana di panggil di dalam procedure hitung dengan menggunakan kondisi yaitu select case.

b. Kalkulator scientific.

Didalam kalkulator scientific ini ada sebuah class bernama scientific, yang didalamnya terdapat inherits dari class sederhana, yaitu untuk: Property : operan1 dan operan2. Function : tambah, kurang, kali, bagi, pangkat, persen, perx. Dan juga di tambahkan function-function yang di class sederhana belum ada yaitu ke dalam class scientific : Function :sin, cos, tan, exp, pangkat3, pangkat2, ln, log, faktorial, modu, dan, atau, atautidak, lsh, dan, tidak, int. Didalam form terdapat procedure untuk menghitung dan menghapus yaitu dipanggil di dalam button sama dengan untuk procedure hitung1, dan di button C dan Backspace untuk procedure hapus. Setiap function yang ada di dalam class scientific di panggil di dalam procedure hitung1 dengan menggunakan kondisi yaitu select case.

Public Class sederhana Private x As Single, y As Single Public Property operan1() As Single Get Return x End Get Set(ByVal value As Single) x = value End Set End Property Public Property operan2() As Single Get Return y End Get Set(ByVal value As Single) y = value End Set End Property Public Function tambah(ByVal x As Single, ByVal y As Single) As Single Return x + y End Function Public Function kurang(ByVal x As Single, ByVal y As Single) As Single Return x - y End Function Public Function kali(ByVal x As Single, ByVal y As Single) As Single Return x * y End Function Public Function bagi(ByVal x As Single, ByVal y As Single) As Single Return x / y End Function Public Function pangkat(ByVal x As Single, ByVal y As Single) As Single Return x ^ y End Function Public Function perx(ByVal x As Single) As Single Return 1 / x End Function Public Function persen(ByVal x As Single) As Single Return x / 100 End Function End Class

Public Class scientific Inherits sederhana Private x As Single, y As Single, z As Char Public Function sin(ByVal x As Single) As Single Return System.Math.Sin(x * (3.14159265358979 / 180)) End Function Public Function cos(ByVal x As Single) As Single Return System.Math.Cos(x * (3.14159265358979 / 180)) End Function Public Function tan(ByVal x As Single) As Single Return System.Math.Tan(x * (3.14159265358979 / 180)) End Function Public Function exp(ByVal x As Single) As Single Return System.Math.Exp(x) End Function Public Function pangkat3(ByVal x As Single) As Single Return x ^ 3 End Function Public Function pangkat2(ByVal x As Single) As Single Return x ^ 2 End Function Public Function ln(ByVal x As Single) As Single Return System.Math.Log(x) End Function Public Function log(ByVal x As Single) As Single Return System.Math.Log10(x) End Function Public Function modu(ByVal x As Single, ByVal y As Single) As Single Return x Mod y End Function Public Function dan(ByVal x As Single, ByVal y As Single) As Single Return x And y End Function Public Function atau(ByVal x As Single, ByVal y As Single) As Single Return x Or y End Function Public Function atautidak(ByVal x As Single, ByVal y As Single) As Single Return x Xor y End Function Public Function tidak(ByVal x As Single, ByVal y As Single) As Single Return Not x End Function Public Function faktorial(ByVal n As Integer) As Long Dim i As Integer, hasil As Long hasil = 1 For i = 1 To n hasil = hasil * i Next Return hasil End Function Public Function int(ByVal x As Single) As Int16 Return x End Function Public Function lsh(ByVal x As Single) As Single Return x End Function End Class

Public Class home Dim objs As New sederhana Dim objl As New scientific Dim op, hasil As Double Dim i, j As String Public Sub hitung() Select Case op Case 1 : hasil = objs.tambah(objs.operan1, objs.operan2) Case 2 : hasil = objs.kurang(objs.operan1, objs.operan2) Case 3 : hasil = objs.kali(objs.operan1, objs.operan2) Case 4 : hasil = objs.bagi(objs.operan1, objs.operan2) Case 5 : hasil = objs.pangkat(objs.operan1, objs.operan2) Case 6 : hasil = objs.persen(objs.operan1) Case 7 : hasil = objs.perx(objs.operan1) Case Else hasil = txt_tampil.Text End Select txt_tampil.Text = hasil End Sub Public Sub hapus() Try i = Len(txt_tampil.Text) j = Len(txt_tampil1.Text) If txt_tampil.Text <> "" Then txt_tampil.Text = Mid(txt_tampil.Text, 1, i - 1) Else txt_tampil1.Text = Mid(txt_tampil1.Text, 1, j - 1) End If Catch ex As Exception End Try End Sub 'form sederhana Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click txt_tampil.Text = txt_tampil.Text + Button27.Text End Sub Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click txt_tampil.Text = txt_tampil.Text + Button22.Text End Sub Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click txt_tampil.Text = txt_tampil.Text + Button21.Text End Sub

Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click txt_tampil.Text = txt_tampil.Text + Button20.Text End Sub Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click txt_tampil.Text = txt_tampil.Text + Button17.Text End Sub Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click txt_tampil.Text = txt_tampil.Text + Button16.Text End Sub Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click txt_tampil.Text = txt_tampil.Text + Button15.Text End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click txt_tampil.Text = txt_tampil.Text + Button8.Text End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click txt_tampil.Text = txt_tampil.Text + Button9.Text End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click txt_tampil.Text = txt_tampil.Text + Button10.Text End Sub Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click txt_tampil.Text = txt_tampil.Text + Button25.Text End Sub Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click txt_tampil.Text = "-" + txt_tampil.Text End Sub Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click If txt_tampil.Text = "" Then objs.operan2 = 1 Call hitung() Else objs.operan2 = txt_tampil.Text Call hitung() End If End Sub Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click op = 1 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click op = 2 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click op = 3 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click op = 4 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click op = 5 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click op = 6 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click op = 7 objs.operan1 = txt_tampil.Text txt_tampil.Text = "" End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Call hapus() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Call hapus() End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click txt_tampil.Clear() End Sub

Public Sub hitung1() Select Case op Case 1 : hasil = objl.tambah(objl.operan1, objl.operan2) Case 2 : hasil = objl.kurang(objl.operan1, objl.operan2) Case 3 : hasil = objl.kali(objl.operan1, objl.operan2) Case 4 : hasil = objl.bagi(objl.operan1, objl.operan2) Case 5 : hasil = objl.pangkat(objl.operan1, objl.operan2) Case 6 : hasil = objl.perx(objl.operan1) Case 7 : hasil = objl.sin(objl.operan1) Case 8 : hasil = objl.cos(objl.operan1) Case 9 : hasil = objl.tan(objl.operan1) Case 10 : hasil = objl.exp(objl.operan1) Case 11 : hasil = objl.pangkat2(objl.operan1) Case 12 : hasil = objl.pangkat3(objl.operan1) Case 13 : hasil = objl.ln(objl.operan1) Case 14 : hasil = objl.log(objl.operan1) Case 15 : hasil = objl.modu(objl.operan1, objl.operan2) Case 16 : hasil = objl.dan(objl.operan1, objl.operan2) Case 17 : hasil = objl.atau(objl.operan1, objl.operan2) Case 18 : hasil = objl.atautidak(objl.operan1, objl.operan2) Case 19 : hasil = objl.tidak(objl.operan1, objl.operan2) Case 20 : hasil = objl.int(objl.operan1) Case 21 : hasil = objl.faktorial(objl.operan1) Case 22 : hasil = objl.lsh(objl.operan1)

Case Else hasil = txt_tampil1.Text End Select txt_tampil1.Text = hasil End Sub

'form lengkap Private Sub Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button28.Click txt_tampil1.Text = txt_tampil1.Text + Button28.Text End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click txt_tampil1.Text = "-" + txt_tampil1.Text End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click txt_tampil1.Text = txt_tampil1.Text + Button3.Text End Sub Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click txt_tampil1.Text = txt_tampil1.Text + Button33.Text End Sub Private Sub Button32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button32.Click txt_tampil1.Text = txt_tampil1.Text + Button32.Text End Sub Private Sub Button31_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button31.Click txt_tampil1.Text = txt_tampil1.Text + Button31.Text End Sub Private Sub Button38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button38.Click txt_tampil1.Text = txt_tampil1.Text + Button38.Text End Sub Private Sub Button37_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button37.Click txt_tampil1.Text = txt_tampil1.Text + Button37.Text End Sub Private Sub Button36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button36.Click txt_tampil1.Text = txt_tampil1.Text + Button36.Text End Sub Private Sub Button43_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button43.Click txt_tampil1.Text = txt_tampil1.Text + Button43.Text End Sub Private Sub Button42_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button42.Click txt_tampil1.Text = txt_tampil1.Text + Button42.Text End Sub Private Sub Button41_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button41.Click txt_tampil1.Text = txt_tampil1.Text + Button41.Text End Sub Private Sub Button44_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button44.Click Call hapus() End Sub Private Sub Button46_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button46.Click Call hapus() End Sub

Private Sub Button45_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button45.Click txt_tampil1.Clear() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click op = 1 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If txt_tampil1.Text = "" Then objl.operan2 = 1 Call hitung1() Else objl.operan2 = txt_tampil1.Text Call hitung1() End If End Sub Private Sub Button30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button30.Click op = 2 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button35_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button35.Click op = 3 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button40_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button40.Click op = 4 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button47_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button47.Click op = 6 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button53_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button53.Click op = 5 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button55_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button55.Click op = 7 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button54_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button54.Click op = 10 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub

10

Private Sub Button51_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button51.Click op = 11 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button52_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button52.Click op = 12 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button49_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button49.Click op = 14 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button39_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button39.Click op = 15 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button62_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button62.Click op = 16 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button34_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button34.Click op = 17 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button61_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button61.Click op = 18 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button60_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button60.Click op = 19 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button56_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button56.Click op = 8 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button57_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button57.Click op = 9 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub

11

Private Sub Button50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button50.Click op = 13 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button59_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button59.Click op = 20 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button48_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button48.Click op = 21 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub Private Sub Button29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button29.Click op = 22 objl.operan1 = txt_tampil1.Text txt_tampil1.Text = "" End Sub End Class

12