Buat Project pada netbeans dengan cara klik file -> pilih New Project -> Dan tulis nama Package seperti di bawah ini :
klik next -> pilih tempat package dan klik finish , maka akan terdapat tampilan dibawah ini :
Buat new class dengan cara klik kanan pada package -> pilih new -> java class
beri nama class tersebut
dan ketikkan coding dibawah ini :
public class MathDemo {
public static void main(String args[]) {
System.out.println("absolute value of -5: " +
Math.abs(-5));
System.out.println("absolute value of 5: " +
Math.abs(-5));
System.out.println("random number(max value is 10): " +
Math.random()*10);
System.out.println("max of 3.5 and 1.2: " +
Math.max(3.5, 1.2));
System.out.println("min of 3.5 and 1.2: " +
Math.min(3.5, 1.2));
System.out.println("ceiling of 3.5: " + Math.ceil(3.5));
System.out.println("floor of 3.5: " + Math.floor(3.5));
System.out.println("e raised to 1: " + Math.exp(1));
System.out.println("log 10: " + Math.log(10));
System.out.println("10 raised to 3: " + Math.pow(10,3));
System.out.println("rounded off value of pi: " +
Math.round(Math.PI));
System.out.println("square root of 5 = " + Math.sqrt(5));
System.out.println("10 radian = " + Math.toDegrees(10) +
" degrees");
System.out.println("sin(90): " +
Math.sin(Math.toRadians(90)));
}
}
Run program dengan cara klik kanan pada class dan pilih Run file atau dapat melalui Shift + f6
Tampilan setelah di Run :
Buat new class dan beri nama seperti dibawah ini :
ketikkan coding dibawah ini :
public class StringConstructorsDemo {
public static void main(String args[]) {
String s1 = new String(); // membuat string kosong
char chars[] = { 'h', 'e', 'l', 'l', 'o'};
String s2 = new String(chars); // s2 = "hello";
byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
String s3 = new String(bytes); // s3 = "world"
String s4 = new String(chars, 1, 3);
String s5 = new String(s2);
String s6 = s2;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
}
}
Run programnya
tambah new class kembali
ketikkan coding dibawah ini :
public static void main(String args[]) {
String name = "Jonathan";
System.out.println("name: " + name);
System.out.println("3rd character of name: " +
name.charAt(2));
/* character yang pertama nampak secara berurutan
mempunyai nilai unicode lebih kecil */
System.out.println("Jonathan compared to Solomon: " +
name.compareTo("Solomon"));
System.out.println("Solomon compared to Jonathan: " +
"Solomon".compareTo("Jonathan"));
/* 'J' mempunyai nilai unicode yang lebih kecil dibanding
'j' */
System.out.println("Jonathan compared to jonathan: " +
name.compareTo("jonathan"));
System.out.println("Jonathan compared to jonathan (ignore case): " + name.compareToIgnoreCase("jonathan"));
System.out.println("Is Jonathan equal to Jonathan? " +
name.equals("Jonathan"));
System.out.println("Is Jonathan equal to jonathan? " +
name.equals("jonathan"));
System.out.println("Is Jonathan equal to jonathan (ignore case)? " + name.equalsIgnoreCase("jonathan"));
char charArr[] = "Hi XX".toCharArray();
/* Membutuhkan tambahan 1 untuk indeks endSrc dari
getChars */
"Jonathan".getChars(0, 2, charArr, 3);
System.out.print("getChars method: ");
System.out.println(charArr);
System.out.println("Length of name: " + name.length());
System.out.println("Replace a's with e's in name: " +
name.replace('a', 'e'));
/* Membutuhkan tambahan 1 untuk parameter endIndex dari
substring*/
System.out.println("A substring of name: " +
name.substring(0, 2));
System.out.println("Trim \" a b c d e f \": \"" +
" a b c d e f ".trim() + "\"");
System.out.println("String representation of boolean expression 10>10: " + String.valueOf(10>10));
/* method toString secara implisit dipanggil method
println */
System.out.println("String representation of boolean expression 10<10: " + (10<10));
/* Catatan, tidak ada perubahan pada nama objek String
meskipun setelah penggunaan semua method. */
System.out.println("name: " + name);
}
}
run program :