Monday, November 29, 2010

RESUME KULIAH PEMROGRAMAN BERORIENTASI OBJEK PERTEMUAN KE – 10 (23 November 2010)

NIM : 09.41010.0013
Nama : Yurike Magdhalena


FreqArray → letterfreq adalah frekuensi untuk menampung nilai suatu variabel array yang telah dideklarasikan nilainya terlebih dahulu pada awal sebuah program, yang masing-masing dapat diset nilainya, misalnya letterFreq (5)
Letter : a, b, c, d, e
Freq : 0,0,0,0,0
Freq dapat diubah dengan decreement (++) atau increement (--). Nilai awalnya adalah 0
misalnya :
int[] counters = new int['z'-'a'+1]; // hold counts here
...
char x = ... // the char to be counted
...
counters[x - 'a']++; // count occurances of char in x
Reply With Quote

contoh program :
import java.io.File;
import java.util.*;
import java.io.*;

public class LetterFrequency
{

public static void main(String[] args )
{
char[] capital = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

char[] small = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

Scanner scan;
try {
scan = new Scanner(new File("C:\\Users\\user\\Documents\\abc.txt"));
} catch (Exception e) {
System.out.println("File not found");
return;
}
int[] count = new int[26];
while(scan.hasNextLine()) {
String line = scan.nextLine();
System.out.println("Line read: " + line);
char[] digit = line.toCharArray();
for(int i = 0; i < digit.length; i++) { for(int j = 0; j < 26; j++) { if(digit[i] == capital[j] || digit[i] == small[j]) { count[j]++; break; } } } } for (int i = 0; i < 26; i++) { System.out.print(" " + capital[i]); System.out.println(" " + count[i]); } } } contoh lain : private void countCharFreq(String line){ int [] charFrequency = new int[26]; //default values: 0 char letter; for(int i = 0; i < line.length(); ++i){ letter = line.charAt(i); if(letter >= 97 && letter <= 122){
++charFrequency[letter - 97];
}
}
char c;
for(int i = 0; i < 26; ++i){
c = i + 97; //not sure if this is how to do this but u get idea
display.append(c + " count: " + charFrequency[i] + " ");
}
}

No comments:

Post a Comment