0% found this document useful (0 votes)
41 views8 pages

La4 - Modul Tour de Java - Lang

The document discusses using various Java classes such as Math, String, StringBuffer, Wrapper, Process, and System. It provides examples of methods from these classes and outputs the results.

Uploaded by

Hendro Setyono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

La4 - Modul Tour de Java - Lang

The document discusses using various Java classes such as Math, String, StringBuffer, Wrapper, Process, and System. It provides examples of methods from these classes and outputs the results.

Uploaded by

Hendro Setyono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Tujuan
Menggunakan class-class Java yang telah ada
● Math
● String
● StringBuffer
● Wrapper
● Process
● System

2.Percobaan
Percobaan 1 : Demo Method - Method dari Class Math

public class Java_Lang_MathDemo {

public Java_Lang_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)));
}
}

Distributed by Meruvian Education


Output Percobaan 1

Percobaan 2 : String Constructor Demo

public class Java_Lang_StringConstructorDemo {

public Java_Lang_StringConstructorDemo() {
}
public static void main(String args[]) {
String s1 = new String(); // creates an empty string
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);
}
}

Distributed by Meruvian Education


Output Percobaan 2 :

Percobaan 3 : Demo Method Class String

Output Percobaan 3 :

Distributed by Meruvian Education


Percobaan 4 : Demo Method Class String Buffer

public class Java_Lang_StringBufferDemo {


public Java_Lang_StringBufferDemo() {
}
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Jonathan");
System.out.println("sb = " + sb);
System.out.println("capacity of sb: " + sb.capacity());
System.out.println("append \'O\' to sb: " + sb.append("O"));
System.out.println("sb = " + sb);
System.out.println("3rd character of sb: " + sb.charAt(2));
char charArr[] = "Hi XX".toCharArray();
sb.getChars(0, 2, charArr, 3);
System.out.print("getChars method: ");
System.out.println(charArr);
System.out.println("Insert \'jo\' at the 3rd cell: " + sb.insert(2, "jo"));
System.out.println("Delete \'jo\' at the 3rd cell: " + sb.delete(2,4));
System.out.println("length of sb: " + sb.length());
System.out.println("replace: " + sb.replace(3, 9, " Ong"));
System.out.println("substring (1st two characters): " + sb.substring(0, 3));
System.out.println("implicit toString(): " + sb);
}
}

Output Percobaan 4 :

Distributed by Meruvian Education


Percobaan 5 : Class Boolean Wrapper
public class Java_Lang_BooleanWrapper {

public Java_Lang_BooleanWrapper() {
}
public static void main(String args[]) {
boolean booleanVar = 1>2;
Boolean booleanObj = new Boolean("TRue");
Boolean booleanObj2 = new Boolean(booleanVar);
System.out.println("booleanVar = " + booleanVar);
System.out.println("booleanObj = " + booleanObj);
System.out.println("booleanObj2 = " + booleanObj2);
System.out.println("compare2wrapperobjects:" + booleanObj.equals(booleanObj2));
booleanVar = booleanObj.booleanValue();
System.out.println("booleanVar = " + booleanVar);
}
}

Output Percobaan 5 :

Distributed by Meruvian Education


Percobaan 6 : Class Runtime – Membuka Registry Editor

public class Java_Lang_RuntimeDemo {

public Java_Lang_RuntimeDemo() {
}
public static void main(String args[]) {
Runtime rt = Runtime.getRuntime();
Process proc;
try {
proc = rt.exec("regedit");
proc.waitFor(); //try removing this line
} catch (Exception e) {
System.out.println("regedit is an unknown command.");
}
}
}

Output Percobaan 6 :

Distributed by Meruvian Education


Percobaan 7 : Demo Class System
import java.io.*;
public class Java_Lang_SystemDemo {

public Java_Lang_SystemDemo() {
}
public static void main(String args[]) throws IOException {
int arr1[] = new int[1050000];
int arr2[] = new int[1050000];
long startTime, endTime;

for (int i = 0; i < arr1.length; i++) {


arr1[i] = i + 1;
}

startTime = System.currentTimeMillis();
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
endTime = System.currentTimeMillis();
System.out.println("Time for manual copy: " + (endTime-startTime) + " ms.");
startTime = System.currentTimeMillis();
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
endTime = System.currentTimeMillis();
System.out.println("Time for manual copy: " + (endTime-startTime) + " ms.");

System.gc(); //force garbage collector


System.setIn(new FileInputStream("temp.txt"));
System.exit(0);
}
}

Distributed by Meruvian Education


Output Percobaan 7 :

Distributed by Meruvian Education

You might also like