Minggu, 22 November 2009

Pemecahan Array


Pemecahan Array

Mula-mula kita harus membuat kelas MyArray sbb :

MyArray.java

1

public class MyArray {

private int size;

private double x[];

public MyArray(int _size)

{

size = _size;

x = new double [size];

}

public void setSize(int _size)

{

size = _size;

x = new double [size];

}

public void read()

{

System.out.print("Banyaknya data :");

int n = MyInput.inInt();

setSize(n);

int i;

for (i=0; i

{

System.out.print("Data "+(i+1)+" : ");

x[i] = MyInput.inDouble();

}

}

public void write()

{

int i;

for (i=0; i

{

System.out.println((i+1)+". "+x[i]);

}

}

public double average()

{

int i;

double total = 0;

for (i=0; i

{

total = total + x[i];

}

return total/size;

}

public void distribusiFrek()

{

int i,f1=0,f2=0,f3=0,f4=0,f5=0;

int asan=200;

for (i=0;i

if (x[i]<=20) f1++;

else if (x[i]<=40) f2++;

else if (x[i]<=60) f3++;

else if (x[i]<=80) f4++;

else if (x[i]<=100) f5++;

}

System.out.println("\n\nAda "+size+" data");

System.out.println("\nRange Frekuensi");

System.out.println("0-20 "+f1);

System.out.println("21-40 "+f2);

System.out.println("41-60 "+f3);

System.out.println("61-80 "+f4);

System.out.println("81-100 "+f5);

}

public void sort()

{

int i,j;

double temp;

System.out.println("Urutan Data : ");

for (i=0;i

for (j=i+1;j

if (x[j]

temp=x[i];

x[i]=x[j];

x[j]=temp;

}

}

System.out.println ((int)x[i]+" ");

}

System.out.println("");

}

public void statistik()

{

int i;

double total=0;

for (i=0; i

{

total = total + x[i];

}

System.out.println("\nRata-rata= "+total/size);

double maks = x[ 0 ];

for (int j=0; j

{

if ( x[j] > maks )

maks = x[j];

}

System.out.println("\nMaksimum= "+maks);

double min = x[ 0 ];

for ( int k=0; k

{

if ( x[k] <>

min = x[k];

}

System.out.println("\nMinimum= "+min);

}

}

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

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

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

Lalu kita membuat kelas MyInput.java

MyInput.java

import java.io.*;

public class MyInput {

private static InputStream in = System.in;

private static PrintStream out = System.out;

private static String buffer = null;

private static int pos = 0;

public static byte inByte()

{

return (byte)readInteger(-128L,127L);

}

public static short inShort()

{

return (short)readInteger(-32768L,32767L);

}

public static int inInt()

{

return (int)readInteger((long)Integer.MIN_VALUE,

(long)Integer.MAX_VALUE);

}

public static long inLong()

{

return readInteger(Long.MIN_VALUE, Long.MAX_VALUE);

}

public static char inChar()

{

char ch = lookChar();

while (ch == ' ' || ch == '\n')

{

readChar();

if (ch == '\n')

dumpString("",0);

ch = lookChar();

}

return readChar();

}

public static double inDouble() {

double x = 0.0;

while (true) {

String str = readRealString();

if (str.equals("")) {

errorMessage("Range " , + Double.MIN_VALUE +

" sampai " + Double.MAX_VALUE);

}

else {

Double f = null;

try { f = Double.valueOf(str); }

catch (NumberFormatException e) {

errorMessage("Range " , + Double.MIN_VALUE +

" sampai " + Double.MAX_VALUE);

continue;

}

if (f.isInfinite()) {

errorMessage("Range " , + Double.MIN_VALUE +

" sampai " + Double.MAX_VALUE);

continue;

}

x = f.doubleValue();

break;

}

}

return x;

}

private static String readRealString()

{

StringBuffer s=new StringBuffer(50);

char ch=lookChar();

while (ch == ' ' || ch == '\n') {

readChar();

if (ch == '\n')

dumpString("",0);

ch = lookChar();

}

if (ch == '-' || ch == '+') {

s.append(readChar());

ch = lookChar();

while (ch == ' ') {

readChar();

ch = lookChar();

}

}

while (ch >= '0' && ch <= '9') {

s.append(readChar());

ch = lookChar();

}

if (ch == '.') {

s.append(readChar());

ch = lookChar();

while (ch >= '0' && ch <= '9') {

s.append(readChar());

ch = lookChar();

}

}

if (ch == 'E' || ch == 'e') {

s.append(readChar());

ch = lookChar();

if (ch == '-' || ch == '+') {

s.append(readChar());

ch = lookChar();

}

while (ch >= '0' && ch <= '9') {

s.append(readChar());

ch = lookChar();

}

}

return s.toString();

}

private static long readInteger(long min, long max)

{

long x=0;

while (true) {

StringBuffer s=new StringBuffer(34);

char ch=lookChar();

while (ch == ' ' || ch == '\n') {

readChar();

if (ch == '\n')

dumpString("",0);

dumpString("",0);

ch = lookChar();

}

if (ch == '-' || ch == '+') {

s.append(readChar());

ch = lookChar();

while (ch == ' ') {

readChar();

ch = lookChar();

}

}

while (ch >= '0' && ch <= '9') {

s.append(readChar());

ch = lookChar();

}

if (s.equals("")){

errorMessage("Range integer : ", + min +

" sampai " + max);

}

else {

String str = s.toString();

try {

x = Long.parseLong(str);

}

catch (NumberFormatException e) {

errorMessage("Range integer : " , + min +

" sampai " + max);

continue;

}

if (x <> max) {

errorMessage("Range integer : ", + min +

" sampai " + max);

continue;

}

break;

}

}

return x;

}

private static void dumpString(String str, int w)

{

for (int i=str.length(); i

out.print(' ');

for (int i=0; i

if ((int)str.charAt(i) >= 0x20 &&

(int)str.charAt(i) != 0x7F)

out.print(str.charAt(i));

else if (str.charAt(i) == '\n' || str.charAt(i) ==

'\r')

newLine();

}

private static void errorMessage(String message,

String expecting)

{

newLine();

dumpString(" *** Input Salah: \n", 0);

dumpString(" *** Range: " + expecting + "\n", 0);

newLine();

dumpString("Masukkan lagi: ", 0);

readChar(); // discard the end-of-line character

}

private static char lookChar()

{

if (buffer == null || pos > buffer.length())

fillBuffer();

if (pos == buffer.length())

return '\n';

return buffer.charAt(pos);

}

private static char readChar()

{

char ch = lookChar();

pos++;

return ch;

}

private static void newLine()

{

out.println();

out.flush();

}

private static boolean possibleLinefeedPending = false;

private static void fillBuffer()

{

StringBuffer b = new StringBuffer();

out.flush();

try {

int ch = in.read();

if (ch == '\n' && possibleLinefeedPending)

ch = in.read();

possibleLinefeedPending = false;

while (ch != -1 && ch != '\n' && ch != '\r') {

b.append((char)ch);

ch = in.read();

}

possibleLinefeedPending = (ch == '\r');

if (ch == -1) {

System.out.println("*** Found an end-of-file while trying to read from standard input! ***");

System.out.println("*** Program will be terminated. *** \n");

throw new RuntimeException(" End-of-file on standard input.");

}

}

catch (IOException e) {

System.out.println(" Unexpected system error on input.");

System.out.println("Terminating program.");

System.exit(1);

}

buffer = b.toString();

pos = 0;

}

}

Apalagi ya? Oh ya! MyMenu.java :

MyMenu.java

1

public class MyMenu {

public static int getPilih()

{

int pilih;

do

{

System.out.println("\n MENU PROGRAM");

System.out.println("1. Input Data");

System.out.println("2. Tampilkan Data");

System.out.println("3. Distribusi Frekuensi");

System.out.println("4. Statistik");

System.out.println("5. Urutkan Data");

System.out.print ("Pilihan Anda : ");

pilih = MyInput.inInt();

System.out.println("");

}while ((pilih<0)||(pilih>5));

return pilih;

}

}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

FINALNYA! Kelas Main.java!

Main.java

1

public class Main {

public static void main (String[] args) {

int pilih;

MyArray arr = new MyArray(1);

do{

pilih = MyMenu.getPilih();

switch(pilih)

{

case 1 : arr.read();break;

case 2 : arr.write();break;

case 3 : arr.distribusiFrek();break;

case 4 : arr.statistik();break;

case 5 : arr.sort();break;

}

}while (pilih!=0);

}

}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19




















Outputnya ! :