12. Tulislah sebuah program yang mengunakan metode getChars, yang dapat dipakai untuk mengekstrak lebih dari satu karakter pada suatu waktu.
Contoh Program:
public class DemogetChars {
public static void main(String args[]){
String s = "Ini ada demo penggunaan metode getchars.";
int awal = 8;
int akhir = 21;
char buf[] = new char [akhir - awal];
s.getChars(awal, akhir, buf, 0);
System.out.println(buf);
}
}
Hasil:
13. metode equals dan equalsIgnoreCase dapat dipakai untuk membandingkan string string. Tulislah suatu program untuk mengilustrasikan penggunaannya.
Contoh Program:
public class Demoequals {
public static void main(String args[]){
String s1 = "Hallo";
String s2 = "Hallo";
String s3 = "Biarlah";
String s4 = "HALLO";
System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIngnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
}
}
Hasil:
14. Tulislah Sebuah program yang membedakan operator = = dari metode equals.
Contoh Program:
public class DemoequalsVersusOperator {
public static void main(String args[]){
String s1 = "Hallo";
String s2 = new String(s1);
System.out.println(s1 + "equals" + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
Hasil:
15. tulislah sebuah program yang mengurutkan array yang memuat string string. program harus menggunakan metode compareTo untuk menetapkan penataan urutan pada pengurutan bubble.
Contoh Program:
public class DemocompareTo {
static String arr[]= {
"Anda", "bila", "senyum", "tawa", "minat", "membawaku", "alam", "kelabu",
"semakin", "percaya", "dinda", "senja", "terang", "kenangan", "berkesan", "kekal"
};
public static void main(String args[]){
for(int j = 0; j < arr.length; j++){
for(int i = j + 1; i < arr.length; i++){
if(arr[i].compareTo(arr[j]) < 0){
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}
Hasil:
16. Berikanlah suatu program untuk menciptakan objek objek dari kelas StringBuffer.
Contoh Program:
public class ObjekStringBuffer {
public static void main(String args[]){
StringBuffer buf = new StringBuffer(10);
System.out.println("buf = " + buf);
System.out.println("buf.length() = " + buf.length());
System.out.println("buf.capacity() = " + buf.capacity());
}
}
Hasil:
17. Berikan suatu program untuk mengilustrasikan fleksibilitas objek objek dari kelas StringBuffer. Program hanya menciptakan satu objek, buf, yang kemudian dimodifikasi beberapa kali menggunakan operator penyambung dan metode append.
Contoh Program:
public class ModifikasiObjekStringBuffer {
public static void main(String[] args){
StringBuffer buf = new StringBuffer(10);
buf.append("Hari ini");
System.out.println("buf = " + buf);
System.out.println("buf.length()= " + buf.length());
System.out.println("buf.capacity() = " + buf.capacity());
buf.append(" mendung menyelimuti ");
System.out.println("buf = " + buf);
System.out.println("buf.length() = " + buf.length());
System.out.println("buf.capacity() = " + buf.capacity());
buf.append("matahari");
System.out.println("buf = " + buf);
System.out.println("buf.length()= " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
}
}
Hasil Program:
18. Berikan suatu program untuk mengganti objek objek StringBuffer menggunakan metode setCharAt dan insert.
Contoh Program:
public class GantiObjekStringBuffer {
public static void main(String[] args){
StringBuffer buf = new StringBuffer();
buf.append("Hari ini awan menyelimuti matahari.");
System.out.println("buf= " + buf);
System.out.println("buf.length()=" + buf.length());
System.out.println("buf.capacity() = " + buf.capacity());
buf.setCharAt(11, 'w');
System.out.println("buf= " + buf);
buf.setCharAt(12, '0');
System.out.println("buf= " + buf);
buf.insert(12, "r");
System.out.println("buf = " + buf);
}
}
Hasil:
19. Berikanlah suatu program untuk mengkonversi objek StringBuffer menjadi objek String menggunakan metode toString.
Contoh Program:
public class StringBufferMenjadiString {
public static void main(String[] args){
StringBuffer buf = new StringBuffer("Ia tak ingin kau bersedih,");
System.out.println("buf= " + buf);
System.out.println("buf.length()= " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
String str = buf.toString();
System.out.println("str= " + str);
System.out.println("str.length() = " + str.length());
buf.append(" " + str.substring(0, 17) + "bila engkau sedang sendiri, ");
System.out.println("buf= " + buf);
System.out.println("buf.length()= " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
System.out.println("str = " + str);
}
}
Hasil:
20. Metode setLength dan reverse dapat dipakai untuk menetapkan panjang dan membalikkan objek objek StringBuffer. Berikan suatu program untuk mengilustrasikannya.
Contoh Program:
public class UjiSetLength {
public static void main(String[] args){
StringBuffer buf = new StringBuffer ("Mari sirami cinta yang tulus di dalam hati.");
System.out.println("buf= " + buf);
System.out.println("buf.length() = " + buf.length());
System.out.println("buf.capacity() = " + buf.capacity());
buf.setLength(60);
System.out.println("buf= " + buf);
System.out.println("buf.length() = " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
buf .setLength(30);
System.out.println("buf = " + buf);
System.out.println("buf.lenfth() = " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
buf.reverse();
System.out.println("buf= " + buf);
System.out.println("buf.length() = " + buf.length());
System.out.println("buf.capacity()= " + buf.capacity());
}
}
Hasil:
No comments:
Post a Comment