0% found this document useful (0 votes)
35 views14 pages

Pbo 2

The document contains 13 code snippets in Java with no other context provided. Each snippet demonstrates different Java concepts like arrays, variables, operators, conditionals, loops, strings, files, and tokens. The snippets are numbered and each is followed by an output section with no results shown. The document appears to be teaching materials or examples for learning basic Java programming concepts.

Uploaded by

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

Pbo 2

The document contains 13 code snippets in Java with no other context provided. Each snippet demonstrates different Java concepts like arrays, variables, operators, conditionals, loops, strings, files, and tokens. The snippets are numbered and each is followed by an output section with no results shown. The document appears to be teaching materials or examples for learning basic Java programming concepts.

Uploaded by

The Chengkre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Pokok Bahasam 1

1. Script :

Hasil :

2. Script :
public class satudua {
public static void main(String[] args) {
String[] s = { "aku", "kamu", "dia", "saya" };
System.out.println("Jumlah elemen s = " + s.length);
for (int i = 0; i < s.length; i++)
System.out.println(s[i] + " panjang = " +
s[i].length());
}

Hasil :

3. Script :
public class satutiga {
public static void main(String[] args) {
int angka = 89;
int x, y, z;
int hitung = 0;
x = y = z = angka;

System.out.println("angka = " + angka);


System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);

}
}
Hasil :
Pokok Bahasan 2

1. Script :
public class satusatu {
public static void main(String[] args) {
byte b = 0x8;
byte c, d, e, f, g, h, i;
c = (byte) ~b;
c &= 0xf;
d = (byte) (b | c);
e = (byte) (c >> 2);
f = (byte) (e + 3);
g = (byte) (c & f);
h = c;
h ^= 0x3;
i = (byte) (h >>> 1);
i <<= 2;

System.out.println("c = " + c);


System.out.println("d = " + d);
System.out.println("e = " + e);
System.out.println("f = " + f);
System.out.println("g = " + g);
System.out.println("h = " + h);
System.out.println("i = " + i);
}
}

Hasil :
2. Script :
public class duadua {
public static void main(String[] args) {
int x = -3;
if (x < 0)
System.out.println("Negatif");
}
}

Hasil :

3. Script :
public class duatiga {
public static void main(String[] args) {
int a = 10, b = 13, x = 0, y = 0;
if (a > b)
x = a;
else {
x = b;
y = 100;
}
System.out.println("a=" + a + " b=" + b + " x" + x + "
y=" + y);
}
}
Hasil :

4. Script :
public class duaempat {
public static void main(String[] args) {
char c = '_';
switch (c) {
case '+':
System.out.println("Operasi penjumlahan");
break;
case '_':
System.out.println("Operasi pengurangan");
break;
case '/':
System.out.println("Operasi pembagian");

break;
case '*':
System.out.println("Operasi perkalian");
break;
default:
System.out.println("Operasi tidak dikenal");

}
}
}

Hasail :
5. Script :
public class dualima {
public static void main(String[] args) {
int m;
m = 5;
while (m > 0) {
System.out.println("Nils=ai m sama dengan = " + m);
m--;
}
}

Hasil :

6. Script :
public class TestDoWhile {
public static void main (String argv []) {
int m = 5;
do {
System.out.println(" m ="+m);
m--;
}while (m > 0);
}
}

Hasil :

7. Script :
public class TestFor {
public static void main (String argv []) {
int m ;
for ( m = 5 ; m > 0 ; m--) {
System.out.println("nilai m adalah "+m);
}
}
}

Hasil :
8. Script :
public class TestBreak {
public static void main (String argv[]) {
int i, j ;
j = 50 ;
for (i=5; i>0; i--) {
if (j>100)
break;
j += 50;
System.out.println(" i = " +i);
}
System.out.println("Program berhenti i="+i +" j="+j);
}
}

Hasil :

9. Script :
public class TestContinue {
public static void main (String argv []) {
int i, j ;
j = 50 ;
for (i=5; i>0; i--) {
if (j>100)
continue;
j += 50;
System.out.println(" i = " +i);
}
System.out.println("Program berhenti i="+i +" j="+j);
}
}

Hasil :

10. Script :
import java.io.*;
public class Compare {
public static void main (String argv[]) throws IOException{
String tabel []={"ari","gali","ika","rozi"};
BufferedReader dis=new BufferedReader(new
InputStreamReader (System.in));
System.out.println("Berikan nama :");
System.out.flush();
String nama=dis.readLine();
for (int i=0;i<tabel.length;i++){
if (tabel[i].equals(nama)){
System.out.println(nama+"terdaftar dalam tabel");
break;
}
}
}
}
Hasil :

11. Script :
public class StringOperasi {
public static void main (String argv[]){
String salam="Terima Kasih, Anda Belajar Java
Programming";
String bagian=salam.substring(5,16);
String satu="Satu", dua="Dua", tiga="Tiga";
System.out.println(bagian);
System.out.println(salam.toUpperCase());
System.out.println(salam.toLowerCase());
System.out.println(satu.compareTo("Satu"));
System.out.println(satu.compareTo(dua));
System.out.println(dua.compareTo(tiga));
}
}

Hasil :

12. Script :
import java.util.*;
public class TestToken {
public static void main (String argv[]){
String kalimat="Di sini Pencerahan Bersemi";
StringTokenizer st=new
StringTokenizer (kalimat," ");
while (st.hasMoreTokens()){
System.out.println(st.nextToken());
}
} }

Hasil :

13. Script :
import java.io.*;
import java.util.*;

public class TokenFile {


public static void main(String[] args) {
TokenFile tt = new TokenFile();
tt.dbTest();
}

public void dbTest() {


BufferedReader dis = null;
String dbRecord = null;
try {
File f = new File("customer.txt");
FileInputStream fis = new FileInputStream(f);
BufferedReader bis = new BufferedReader(new
InputStreamReader(fis));
dis = new BufferedReader(new InputStreamReader(fis));

while ((dbRecord = dis.readLine()) != null) {


StringTokenizer st = new
StringTokenizer(dbRecord, ":");
String namaDepan = st.nextToken();
String namaBelakang = st.nextToken();
String kota = st.nextToken();
String propinsi = st.nextToken();

System.out.println("Nama Depan: " + namaDepan);


System.out.println("Nama Belakang: " +
namaBelakang);
System.out.println("kota: " + kota);
System.out.println("propinsi: " + propinsi + "\
n");
}
} catch (IOException e) {
System.out.println("IOException error!" +
e.getMessage());
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
}
}
}
}

Hasil :

You might also like