Pertemuan 3 - Kelas (Class), Objek (Object), dan Class Diagram - TeachMeSoft

Pertemuan 3 - Kelas (Class), Objek (Object), dan Class Diagram






Array


Contoh 1
public class La {
  public static void main(String[] args) {
    int[] angka = {20, 10};
    System.out.println(angka[0]);
 System.out.println(angka[1]);
  }
}

Output


Contoh 2
public class La {
  public static void main(String[] args) {
    int[] angka = new int[2];
  angka[0]=20;
  angka[1]=10;
    System.out.println(angka[0]);
 System.out.println(angka[1]);
  }
}



Output:


Contoh 3
public class La {
  public static void main(String[] args) {
    int[] angka = new int[2];
  angka[0]=20;
  angka[1]=10;
  
  for(int i = 0; i<2; i++){
   System.out.println(angka[i]);
  }
 }
}

Output:





Contoh 4
import java.util.Scanner;

public class La {
  public static void main(String[] args) {
    int[] angka = new int[2];
  angka[0]=20;
  angka[1]=10;
  Scanner s = new Scanner (System.in);
  for(int i = 0; i<2; i++){   
   System.out.print("Masukkan angka: ");
   angka[i]=s.nextInt();
   System.out.println(angka[i]);
  }
  
 }
}

Output:


Contoh 5
import java.util.Scanner;

public class La {
  public static void main(String[] args) {
    int[] angka = new int[2];
  angka[0]=20;
  angka[1]=10;
  Scanner s = new Scanner (System.in);
  for(int i = 0; i<2; i++){   
   System.out.print("Masukkan angka: ");
   angka[i]=s.nextInt();
   
  }
   for(int i : angka) {
   System.out.print(angka[i]);
  }
 }
}

Output



Contoh 6
import java.util.Scanner;

public class La {
  public static void main(String[] args) {
    int[] angka = new int[2];
  angka[0]=20;
  angka[1]=10;
  Scanner s = new Scanner (System.in);
  for(int i = 0; i<2; i++){   
   System.out.print("Masukkan angka: ");
   angka[i]=s.nextInt();
   
  }
    for(int i = 0; i<2; i++) {
        System.out.println(angka[i]);
  }
 }
}

Output









Class


Method = Fungsi di C++


Contoh1


output




Contoh2
class Handphone                     
{ 
 //deklarasi
 private String nama;
 private int harga;
 
 public void setNama(String nama)
 {
  this.nama=nama;
 }
 
 public void setHarga(int harga)
 {
  this.harga=harga;
 }
 public String getNama(){
  return nama;
 }
 
 public int getHarga(){
  return harga;
 }
 //getter
 
 //method tambahan 
} 

public class kelas3{
 public static void main(String[]args){
  Handphone hp = new Handphone();
  Handphone hp2 = new Handphone();
  
  System.out.println("=======================");
  
  hp.setNama("Nokia");
  hp.setHarga(2000);
  System.out.println("Nama HP = "+hp.getNama());
  System.out.println("Harga HP= "+hp.getHarga());
  
  System.out.println("=======================");
  
  hp2.setNama("Xiomi 5 Plus");
  hp2.setHarga(5000);
  System.out.println("Nama HP= "+hp2.getNama());
  System.out.println("Harga HP= "+hp2.getHarga());
  
  System.out.println("=======================");
 }
}


output








Disqus comments