Tampilkan postingan dengan label program. Tampilkan semua postingan
Tampilkan postingan dengan label program. Tampilkan semua postingan

Selasa, 08 Oktober 2013

Mengakses Elemen Aray.


public class array
{
   public static void main (String args[])
    {
int nilai []=new int[3];
 nilai[0]=70; nilai[1]=80;
nilai[2]=65;
double ratarata=0.0;
for (int i=0; i<nilai.length; i++)
ratarata+=nilai[i];
ratarata/=nilai.length;
System.out.println("Nilai rata-rata = " + ratarata);
 }
 }

import java.text.NumberFormat;
public class array2
{
 public static void main(String args[])
 {
NumberFormat nf=NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
int nilai[][]=new int[2][3];
nilai[0][0]=85;
nilai[0][1]=81;
 nilai[0][2]=78;
 nilai[1][0]=65;
 nilai[1][1]=73;
 nilai[1][2]=71;
 String MK[]={"RPL", "PBO"};
 double ratarataMK[]=new double[nilai.length];
 for (int i=0; i<nilai.length; i++)
 {
 for (int j=0; j<nilai[0].length; j++)
 {
 ratarataMK[i]+=nilai[i][j];
 }
 ratarataMK[i]/=nilai[0].length;
 }
 System.out.println("Nilai Mata Kuliah\n");
 System.out.println("MK\tMinggu\tMinggu2\tMinggu3\t Rata-rata");
 for (int i=0; i<nilai.length; i++)
 {
 System.out.println(MK[i] + "\t");
 for (int j=0; j<nilai[0].length; j++)
 {
 System.out.print(nilai[i][j] + "\t");
 }
 System.out.print(nf.format(ratarataMK[i])+"\n");
 }            
 }
 }

public class array2
{
public static void main(String args[])
                {
int x[][][][]=new int[2][][][];
                                x[0]=new int [1][][];
                                x[0][0]=new int[2][];
                                x[0][0][0]=new int[3];
                                x[0][0][1]=new int[2];
                                x[1]=new int[2][][];
                                x[1][0]=new int[1][];
                                x[1][0][0]=new int [2];
                                x[1][1]=new int[2][];
                                x[1][1][0]=new int[1];
                                x[1][1][1]=new int[3];
                                System.out.println(x.length);
                                System.out.println(x[0].length);
                                System.out.println(x[0][0].length);
                                System.out.println(x[0][0][0].length);
                                System.out.println(x[0][0][1].length);
                                System.out.println();
                                System.out.println(x[1].length);
                                System.out.println(x[1][0].length);
                                System.out.println(x[1][0][0].length);
                                System.out.println(x[1][1].length);
                                System.out.println(x[1][1][0].length);
                                System.out.println(x[1][1][1].length);    } }           
Pernyataan if
Menyatakan bahwa pernyataan akan dieksekusi jika memenuhi kondisi yang ditentukan
public class PernyataanIf
{
public static void main (String[ ] args)
                {
int nilai = 70;                       // pendefinisian variabel
if (nilai >= 60)
System.out.println ("Nilai = " + nilai + "      Lulus ujian");
}
}
Input data
Contoh dibawah menunjukkan penggunaan input data mlalui keyboard dengan kelas JOptionPane . Input diterima dari kotak dialog dalam bentuk string.Untuk konversi srting menjadi interger diperlukan metode Integer.parseInt().
public class PernyataanIfIn
{  
                public static void main (String[] args)
                {       
                                int nilai;
                                String bilangan = JOptionPane.showInputDialog("Masukkan nilai:");     
                                System.out.println("Input angka: " + bilangan);
                                nilai = Integer.parseInt(bilangan);
                                if (nilai >= 60)
                                System.out.println ("Nilai = " + nilai + "      Lulus Ujian");        
                                System.exit(0);  
                 }
}

If bersarang
Contoh : Seseorang dapat diterima sebagai karyawan apabila umur paling banyak 30 tahun dan berpendidikan sarjana.
public class PernyataanIfNested
{
                public static void main (String[ ] args)
                {
                                int umur = 28;                    // pendefinisian variabel
                                String pendidikan = "sarjana";
                                System.out.println ("Umur = " + umur);
                                System.out.println ("Pendidikan = " + pendidikan);                         
                               
                if (umur <= 30);
                                                {
                                                if (pendidikan == "sarjana")
                                                System.out.println ("Diterima menjadi karyawan");
                                                }
                }
}

Pernyataan if-else
public class PernyataanIfelse
{
                public static void main (String[ ] args)
                {
int pembelian = 110;                       // pendefinisian variabel
                                System.out.println ("Jumlah pembelian = " + pembelian);                                                                            if (pembelian >= 100)
System.out.println ("Diskon = 20%");
                                Else
System.out.println ("Diskon = 5%");                                       
                }
}
If-else yang melibatkan if kedua ketiga dst
public class PernyataanIfelsen
{
public static void main (String[ ] args)
{
int nilai;                                // pendefinisian variabel
nilai = 75;
System.out.println ("Nilai =  " + nilai);
if (nilai >= 80)
System.out.println ("Predikat A");
else if (nilai >= 69)
System.out.println ("Predikat B");
else if (nilai >= 56)
System.out.println ("Predikat C");
else if (nilai >= 40)
System.out.println ("Predikat D");          
else
System.out.println ("Predikat E");           
}
}


Contoh  4
import java.util.Scanner;
public class input_05
{
                public static void main(String[] args)
                {
                                Scanner Sa = new Scanner(System.in);

                                System.out.print("Npm          : "); String npm = Sa.nextLine();
                                System.out.print("Nama         : "); String nm = Sa.nextLine();
                                System.out.print("Mata Kuliah  : "); String mk = Sa.nextLine();
                                System.out.print("Nilai        : "); int nilai = Sa.nextInt();
                                System.out.println("-----------------------------");
                                System.out.println("NPM                 : "+npm);
                                System.out.println("Nama                : "+nm);
                                System.out.println("Mata Kuliah     : "+mk);
                                System.out.println("Nilai                 : "+nilai);
                }
}

: Input Data String
import java.io.*;
class InputString {
public static void main(String[] args) throws IOException {
String nama;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("Masukkan nama Anda: ");  
nama = br.readLine();
System.out.println("Hallo " + nama + ", sudahkah Anda mengerti Java?");    }}      

Contoh 3 : Input Data Numerik
import java.io.*;
class InputNumerik1 {  
public static void main(String[] args) throws IOException {
System.out.print("Masukkan sebuah bilangan bulat: ");
String temp;
int bilangan = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// input data dianggap sebagai string
temp = br.readLine();
try {
// konversi dari string ke integer
bilangan = Integer.parseInt(temp);
} catch (NumberFormatException nfe) {
System.out.println("Data yang dimasukkan " +
"bukan bilangan bulat");
System.exit(1);
}
System.out.println("Bilangan yang dimasukkan " + "adalah " + bilangan);
}}




//PROGRAM IF TIGA KONDISI VARIASI 2
class Latihan_4_7
{
  public static void main(String[] args)
  {          
    char nilaiIndeks;
    double nilaiUTS, nilaiUAS, nilaiAkhir;
       
    // contoh data yang dimasukkan
    nilaiUTS = 75.0;
    nilaiUAS = 60.0;
       
    // menghitung nilai akhir menggunakan rumus di atas
    nilaiAkhir = (0.4 * nilaiUTS) + (0.6 * nilaiUAS);
     
    if (nilaiAkhir >= 80)
    {
      nilaiIndeks = 'A';
    }
    else if (nilaiAkhir >= 70)
    {        
      nilaiIndeks = 'B';
    }
    else if (nilaiAkhir >= 50)
    {        
      nilaiIndeks = 'C';
    }
    else if (nilaiAkhir >= 30)
    {        
      nilaiIndeks = 'D';       
    }
    else
    {    // (nilaiAkhir < 30)
      nilaiIndeks = 'E';
    }
    System.out.println("Nilai Akhir\t: " + nilaiAkhir);
    System.out.println("Nilai Indeks\t: " + nilaiIndeks);          
  }
}

//PROGRAM IF TIGA KONDISI VARIASI 1
class Latihan_4_6
{
  public static void main(String[] args)
  {          
    int bilangan = 4;
       
    if (bilangan < 0)
    {
      System.out.println(bilangan + " merupakan bilangan NEGATIF.");
    }
    else if (bilangan == 0)
    {        
      System.out.println("Nilai yang dimasukkan adalah NOL");
    }
    else
    { // (bilangan > 0)
      System.out.println(bilangan + " merupakan bilangan POSITIF.");      }                                    
  }
}
//PROGRAM 3-1 : Aritmetika Dasar
class Latihan_3_1
{
  public static void main(String[] args)
  {                    
    System.out.println("Operasi aritmetika " + "pada tipe integer");
    int a = 2 + 1;
    int b = a - 1;
    int c = a * b;
    int d = c / 3;
    int e = -a;
    System.out.println("Nilai a: " + a);
    System.out.println("Nilai b: " + b);
    System.out.println("Nilai c: " + c);
    System.out.println("Nilai d: " + d);
    System.out.println("Nilai e: " + e);
    System.out.println();
           
    System.out.println("Operasi aritmetika " + "pada tipe floating-point");
    double fa = 2 + 1;
    double fb = fa - 1;
    double fc = fa * fb;
    double fd = fc / 3;
    double fe = -a;
    System.out.println("Nilai fa: " + fa);
    System.out.println("Nilai fb: " + fb);
    System.out.println("Nilai fc: " + fc);
    System.out.println("Nilai fd: " + fd);
    System.out.println("Nilai fe: " + fe);           
  }
}

//PROGRAM 3-2 : Modulus
class Latihan_3_2
{
  public static void main(String[] args)
  {                    
    int a=11, b=4;
    int c = a % b;

    double da = 13.75;       
    double dc = da % b;

    System.out.println("Sisa bagi " + a + "/" + b + " adalah " + c);
    System.out.println("Sisa bagi " + da + "/" + b + " adalah " + dc); 
  }

//PROGRAM 3-3 : Increment
class Latihan_3_3
{
  public static void main(String[] args)
  {                    
    int a=5;
           
    System.out.println("Pre-increment");
    System.out.println("a\t: " + a);
    System.out.println("++a\t: " + ++a);
    System.out.println("a\t: " + a);
           
    int b=5;
           
    System.out.println("\nPost-increment");
    System.out.println("b\t: " + b);
    System.out.println("b++\t: " + b++);
    System.out.println("b\t: " + b);                 
  }
}

//PROGRAM 3-6 : Operator Logika
class Latihan_3_6
{    
  public static void main(String[] args)
  {
                       
    System.out.println("Operasi AND");
    System.out.println("true && true   = " + (true && true));
    System.out.println("true && false  = " + (true && false));
    System.out.println("false && true  = " + (false && true));
    System.out.println("false && false = " + (false && false));
           
    System.out.println("\nOperasi OR");
    System.out.println("true || true   = " + (true || true));
    System.out.println("true || false  = " + (true || false));
    System.out.println("false || true  = " + (false || true));
    System.out.println("false || false = " + (false || false));
           
    System.out.println("\nOperasi XOR");
    System.out.println("true ^ true    = " + (true ^ true));
    System.out.println("true ^ false   = " + (true ^ false));
    System.out.println("false ^ true   = " + (false ^ true));
    System.out.println("false ^ false  = " + (false ^ false));
           
    System.out.println("\nOperasi NOT");
    System.out.println("!true          = " + (!true));
    System.out.println("!false         = " + (!false));
  }  
}


//PROGRAM 3-5 : Relasional
class Latihan_3_5
{
  public static void main(String[] args)
  {                    
    int a=5, b=10;           

    System.out.println("a == b bernilai " + (a == b));
    System.out.println("a != b bernilai " + (a != b));
    System.out.println("a > b bernilai " + (a > b));
    System.out.println("a < b bernilai " + (a < b));
    System.out.println("a >= b bernilai " + (a >= b));
    System.out.println("a <= b bernilai " + (a <= b));           
  }
}

public class Boolean1
{
public static void main (String [] args)
      {
boolean b;
b = true;
System.out.println ("Nilai b = " +b);
if (b)
{
System.out.println("Statemen ke-1 dieksekusi");
}
b = false;
System.out.println("Nilai b = " +b);
if (b)
{
System.out.println("Statemen ke-2 tidak dieksekusi");
}
if (!b)
{
System.out.println("Statemen ke-3 dieksekusi");
}
System.out.println ("5 <= 10 mengembalikan nilai = " + (5 <= 10));
System.out.println("4 > 6 mengembalikan nilai " + (4 > 6));
}
}





import java.io.*;
public class java0142
{
      public static void main(String[]args)throws IOException
      {
            for(int a=0;a<5;a++)
            {
            double ahir;
            char huruf;
            int UAS,UTS;
            String nim,nama,fak,progdi,uas,uts;
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print ("Masukkan NIM                                   : ");
            nim=br.readLine();
            System.out.print ("Masukkan Nama                                : ");
            nama=br.readLine();
            System.out.print ("Masukkan Fakultas                : ");
            fak=br.readLine();
            System.out.print ("Masukkan Program Studi                   : ");
            progdi=br.readLine();
            System.out.print ("Masukkan Nilai UTS               : ");
            uts=br.readLine();
            System.out.print ("Masukkan Nilai UAS              : ");
            uas=br.readLine();
            UTS=Integer.parseInt(uts);
            UAS=Integer.parseInt(uas);
            ahir=UTS*0.4+UAS*0.6;
                  if (ahir>100){huruf='X';
                  }else if (ahir>=80){huruf='A';
                  }else if (ahir>=71){huruf='B';
                  }else if (ahir>=61){huruf='C';
                  }else if (ahir>=51){huruf='D';
                  }else {huruf='E';
                  }
                  System.out.println(" ");                    
                  System.out.println("***###***###***###*** Nilai Anda ***###***###***###***");              
                  System.out.println("NIM                               :"+nim);
                  System.out.println("Nama                            :"+nama);
                  System.out.println("Fakultas                        :"+fak);
                  System.out.println("Program Studi               :"+progdi);
                  System.out.println("Nilai UTS                       :"+UTS);
                  System.out.println("Nilai UAS                       :"+UAS);
                  System.out.println("Nilai Angka                    :"+ahir);
                  System.out.println("Nilai Huruf                      :"+huruf);
                  System.out.println("**** program oleh : 10.01.53.0110 Rochmad ***");
                  System.out.println(" ");        
            }
      }                
}

import java.io.*;
class persegi
{
  public static void main (String[]arg) throws IOException
  {
int p,l;
String nama, nim;
double luas, keliling;
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.print("Masukkan Nama                 :");nama= br.readLine();
System.out.print("Masukkan NIM                  :");nim= br.readLine();
System.out.print("Masukkan nilai panjang:");p=Integer.parseInt(br.readLine());
System.out.print("Masukkan nilai lebar :");l=Integer.parseInt(br.readLine());
        luas=p*l;
        keliling=p+p+l+l;
System.out.println("=========================================================");
        System.out.println("Nama                  :"+nama);
        System.out.println("Nim                   :"+nim);
        System.out.println("Nilai panjang   :"+p);
        System.out.println("Nilai Lebar           :"+l);
        System.out.println("Luas                  :"+luas);
        System.out.println("Keliling        :"+keliling);
              if(p==l)
        {
              System.out.println("Jenis Bangunan  :Perasegi");
        }
        else
        {
              System.out.println("Jenis Bangunan  :Bukan persegi");
        }
    System.out.println("===========================================================");
  }
}


Mengakses elemen array

public class array
{

    public static void main (String args[])
    {
        int nilai []=new int[3];
        nilai[0]=70; nilai[1]=80;
        nilai[2]=65;
        double ratarata=0.0;
       
        for (int i=0; i<nilai.length; i++)
              ratarata+=nilai[i];
              ratarata/=nilai.length;
              System.out.println("Nilai rata-rata = " + ratarata);
    }
}