laporan praktikum konstruktor

20
LABORATORIUM KOMPUTER DASAR PROGRAM STUDI INFORMATIKA / ILMU KOMPUTER PROGRAM TEKNOLOGI INFORMASI DAN ILMU KOMPUTER UNIVERSITAS BRAWIJAYA PRAKTIKUM PEMROGRAMAN LANJUT MODUL 2 Judul Bab : Class, Konstruktor dan Method Instan Disusun Oleh : Vera Rusmalawati NIM : 135150201111194 Dilaksanakan Tanggal : 21 Maret 2014 Asisten 1 : Bagus Abdan Aziz Fahriansyah Asisten 2 : Amalia Ramadhanty Dosen Pengampu : Ir. Sutrisno, MT Pengesahan Tanggal: Asisten SEMESTER :GENAP TAHUN : 2013/2014

Upload: vera-rusmalawati

Post on 01-Oct-2015

28 views

Category:

Documents


1 download

DESCRIPTION

Tugas Praktikum Pemrograman Lanjut

TRANSCRIPT

LABORATORIUM KOMPUTER DASAR

PROGRAM STUDI INFORMATIKA / ILMU KOMPUTER

PROGRAM TEKNOLOGI INFORMASI DAN ILMU KOMPUTER

UNIVERSITAS BRAWIJAYA

PRAKTIKUM PEMROGRAMAN LANJUT

MODUL 2Judul Bab : Class, Konstruktor dan Method InstanDisusun Oleh: Vera RusmalawatiNIM: 135150201111194Dilaksanakan Tanggal: 21 Maret 2014Asisten 1: Bagus Abdan Aziz Fahriansyah

Asisten 2: Amalia RamadhantyDosen Pengampu: Ir. Sutrisno, MTPengesahan

Tanggal:

Asisten

A. Definisi Masalah

1. Tambahkan operator operator lain untuk melengkapi kelas bilangan rasional diatas, seperti : (minimal 5 operator)

a. Operator Unary -= , unary /=, unary *=, unary ++, unary--

b. Operator < , =

c. Operator pembanding ==, pembanding !=

d. Operator + , - , * , /

B. Source Code1. Class Rasional3

1

2

3

4

5

6

7

8

9

10

11

12

13

1415

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109package Konstruktor;

public class Rasional3 { private int pembilang, penyebut; public Rasional3() {

pembilang = 0;

penyebut = 0;

} public Rasional3(int pbl, int pyb) {

pembilang = pbl;

penyebut = pyb;

}

public boolean isRasional() {

return (penyebut != 0);

}

public void Sederhana() {

int temp, A, B; if (penyebut == 0) {

return;

} A = (pembilang < penyebut) ? penyebut : pembilang;

B = (pembilang < penyebut) ? pembilang : penyebut;

while (B != 0) {

temp = A % B;

A = B;

B = temp;

} pembilang /= A;

penyebut /= A; } public double Cast() {

return (penyebut == 0.0) ? 0.0 : (double) pembilang / (double) penyebut;

} public boolean moreThan(Rasional3 A) {

return (pembilang * A.penyebut > penyebut * A.pembilang);

}

public boolean kurangDari(Rasional3 A) {

return (pembilang * A.penyebut < penyebut * A.pembilang);

}

public boolean kurangDariSamaDengan(Rasional3 A) {

return (pembilang * A.penyebut = penyebut * A.pembilang);

} public boolean samaDengan(Rasional3 A) {

return (pembilang * A.penyebut == penyebut * A.pembilang);

} public boolean tidakSamaDengan(Rasional3 A) {

return (pembilang * A.penyebut != penyebut * A.pembilang);

}

public void negasi() {

pembilang = -pembilang;

}

public void unaryPlus(Rasional3 A) {

pembilang = pembilang * A.penyebut + penyebut * A.pembilang;

penyebut *= A.penyebut;

}

public void unaryMinus(Rasional3 A) {

pembilang = pembilang * A.penyebut - penyebut * A.pembilang;

penyebut *= A.penyebut;

}

public void unaryCross(Rasional3 A) {

pembilang *= A.pembilang;

penyebut *= A.penyebut;

}

public void unaryDivide(Rasional3 A) {

pembilang /= A.pembilang;

penyebut /= A.penyebut;

}

public void unaryPlusPlus() {

pembilang = pembilang * 1 + penyebut * 1;

penyebut *= 1;

}

public void unaryMinusMinus() {

pembilang = pembilang * 1 - penyebut * 1;

penyebut *= 1;

}

public void cetak() {

System.out.println(pembilang + "/" + penyebut);

}

public Rasional3 tambah(Rasional3 A, Rasional3 B) {

penyebut = A.penyebut * B.penyebut;

A.pembilang *= (penyebut / A.penyebut);

B.pembilang *= (penyebut / B.penyebut);

pembilang = A.pembilang + B.pembilang;

Rasional3 hasil = new Rasional3(pembilang, penyebut);

return hasil;

}

public Rasional3 kurang(Rasional3 A, Rasional3 B) {

penyebut = A.penyebut * B.penyebut;

A.pembilang *= (penyebut / A.penyebut);

B.pembilang *= (penyebut / B.penyebut);

pembilang = A.pembilang - B.pembilang;

Rasional3 hasil = new Rasional3(pembilang, penyebut);

return hasil;

}

public Rasional3 kali(Rasional3 A, Rasional3 B) {

pembilang = A.pembilang * B.pembilang;

penyebut = A.penyebut * B.penyebut;

Rasional3 hasil = new Rasional3(pembilang, penyebut);

return hasil;

}

public Rasional3 bagi(Rasional3 A, Rasional3 B) {

pembilang = A.pembilang * B.penyebut;

penyebut = A.penyebut * B.pembilang;

Rasional3 hasil = new Rasional3(pembilang, penyebut);

return hasil;

}

}

2. Class Rasional3Demo

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80package Konstruktor;

public class Rasional3Demo { public static void main(String[] args) {

Rasional3 R1 = new Rasional3(1, 2);

Rasional3 R2 = new Rasional3(1, 3); System.out.println("R1.isRasional: " + R1.isRasional());

System.out.println("R2.isRasional: " + R1.isRasional());

System.out.println(); System.out.println("R1 > R2 : " + R1.moreThan(R2));

System.out.println("R1 < R2 : " + R1.kurangDari(R2));

System.out.println("R1 >= R2 : " + R1.lebihDariSamaDengan(R2));

System.out.println("R1