Contoh Penerapan Variabel Dalam Java


Hiii,, Ngebut ngerjain tugas neh jadinya :P, gue cuma mau share tentang contoh penerapan variable di java, sekedar testing lah selamat melahap yah :).

Program Penjumlahan.java

//--------------------------------------------------------------
//   Nama file: Penjumlahan.java
//   Deklarasi variabel int dan pemberian nilai variabel
//--------------------------------------------------------------
class Penjumlahan {
  public static void main(String args[]){
    int bilangan1 = 3;
    int bilangan2 = 7;
    int jumlah = bilangan1 + bilangan2;
    System.out.println("hasil penjumlahan dari bilangan1 dan bilangan2 = " + jumlah);
  }
}

Program Segitiga.java

//--------------------------------------------------------------
//   Nama file: Segitiga.java
//   Deklarasi variabel double dan pemberian nilai variabel
//--------------------------------------------------------------
class Segitiga {
  public static void main(String args[]){
    double alas = 3.5;
    double tinggi = 4.25;
    double luas = 0.5 * alas * tinggi;
    System.out.println("luas segitiga = " + luas);
  }
}

Program DekChar.java

//--------------------------------------------------------------
//   Nama file: DekChar.java
//   Deklarasi variabel char dan pemberian nilai variabel
//--------------------------------------------------------------
public class DekChar {
 public static void main (String[] args) {
 char huruf;     //deklarasi variabel
 huruf = 'I';    //memberikan nilai 'I' ke variabel huruf
 System.out.print("Karakter adalah = ");
 System.out.println(huruf);
 huruf = 73;     //memberikan nilai ASCII ke variabel huruf
 System.out.print("Karakter adalah = ");
 System.out.println(huruf);
 }
}

Program Logic.java

//--------------------------------------------------------------
//   Nama file: Logic.java
//   Deklarasi variabel boolean dan pemberian nilai variabel
//--------------------------------------------------------------
class Logic {
  public static void main(String[] args) {
    boolean kondisi1=true, kondisi2=false;
    int x=7, y=5;
    System.out.println(kondisi1 || kondisi2);
    System.out.println(kondisi1 && kondisi2);
    System.out.println(x > y);
    System.out.println(x < y);
    System.out.println(x == y);
    System.out.println(x != y);
  }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s