Visitor

Sunday, December 15, 2013

Contoh Syntak Program Dasar Java dan Print Screen Output

Versi LENGKAP (ada gambarnya)  : [DOWNLOAD]  via 4shared.com


 Latihan 1

         Syntax Mahasiswa.java:
public class Mahasiswa{
   
public String nama;
public int angkatan;
public String asal;
public Mahasiswa(){
nama="Ani";
angkatan=2010;
asal = "Magelang";
}
public void ubah(String nm, int ak, String as){
nama = nm;
angkatan = ak;
asal=as;
}
public void cetak(){
System.out.println("nama     : "+(nama));
System.out.println("angkatan : "+(angkatan));
System.out.println("asal     : "+(asal));
}
}
Syntax TestMahasiswa.java
                public class TestMahasiswa{
public static void main (String[]args){
mahasiswa MHS = new mahasiswa();
System.out.println("IDENTITAS AWAL : ");
MHS.cetak();

MHS.ubah("DITA", 2010, "Madiun");
System.out.println ("IDENTITAS AKHIR : ");
MHS.cetak();
}
}
Output :


Latihan2

Syntax Linkup.java
class Lingkup{
//variabel instan
String sifat = "Marah";
//metode
void infoLingkup(){
String sifat="Malu";
        System.out.println("sifat pada metode : "+(sifat));
        System.out.println("sifat milik kelas : "+(this.sifat));
        }
        }

Syntax TestLingkup.java
public class TestLingkup{
public static void main (String[]args){
Lingkup varx = new Lingkup();
varx.infoLingkup();
}
}
Output :

Latihan3

Syntax Scoope.java
class Scoope{
//variabel instan
int angka=20;
//metode

void infoScoope(){
        int angka1=12;
        System.out.println("angka di method :"+(angka1));
        System.out.println("angka di kelas : "+(this.angka));
        int modulus = angka % angka1;
        System.out.println("sisa hasil ; "+(modulus));
        float bagi = angka/angka1;
        System.out.println("hasil bagi : "+(bagi));
        }
}

Syntax TestScoope.java
public class TestScoope{
public static void main (String[]args){
Scoope varx = new Scoope();
varx.infoScoope();
}
}
Output :


Latihan4

Syntax Report.java
import java.io.*;

public class Report{
public static String bacaInputan(){
String baca = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(input);
try{
baca = buf.readLine();
}catch(IOException e){
System.out.println("Error!");
}

return baca;
}

public static void main (String [] args){
//digunakan untuk menangkap exception
String name = "";
int nilaiMatematika;
int nilaiFisika;
int nilaiKimia;
System.out.println ("Masukkan nama : ");
name = bacaInputan();
System.out.println ("Masukkan nilai matematika : ");
nilaiMatematika = Integer.parseInt (bacaInputan());
System.out.println ("Masukkan nilai fisika");
nilaiFisika = Integer.parseInt (bacaInputan());
System.out.println ("Masukkan nilai kimia");
nilaiKimia = Integer.parseInt (bacaInputan());

System.out.println ("\n----------------------------------------------");
System.out.println ("             "+(name));
System.out.println ("Nilai kamu adalah ");
System.out.println ("Matematika : "+(nilaiMatematika));
System.out.println ("Fisika     : "+(nilaiFisika));
System.out.println ("Kimia      : "+(nilaiKimia));
}
}
Output :

Latihan5
Syntax Persegi.java
import java.io.*;
public class persegi{

public static void main (String[]args){
BufferedReader data = new BufferedReader (new InputStreamReader(System.in));
String x = " ";
int hitungLuas = 0;
System.out.println ("-----Luas Persegi-----");
System.out.println ("Masukkan nilai sisi : ");

try {
x = data.readLine();
int angka = Integer.parseInt(x);
hitungLuas = angka * angka;
} catch (Exception e){
System.out.println ("Error");
}
System.out.println ("Luas persegi dengan sisi " + x + " adalah " + (hitungLuas));
}
}
Output :


5.   Program Overloading

Syntax :
public class prak1{
static int tambah (int a, int b){
return a+b;}

static double tambah (double a, double b){
return a+b;}

static int tambah (int a, int b, int c){
return a+b+c;}

public static void main (String[] args){
  int a=3;      //deklarasi
  int b=4;      //deklarasi
  int c=8;      //deklarasi
  double d=0.5; //deklarasi
 
  System.out.println("\n======================");
  System.out.println("1. = " +tambah(a,b));
  System.out.println("2. a("+a+")+ b("+b+") = "+tambah(a,b));
  System.out.println("3. = " +tambah(a,b,c));
  System.out.println("4. = " +(tambah(a,b,c)-d));
  System.out.println("\n=======================");
}
}

Output :


6.      Program Boolean

Syntax :
public class prak2
{
  public static void eksekusi (int x)
  {
      boolean y;
      if(x<=10)
      {
            y=true;
            System.out.println("Nilai b=" +y+ "\n Statement Awal dieksekusi");
      }
      else
      {
            y=false;
            System.out.println("Nilai b=" +y+ "\n Statement Akhir dieksekusi");
      }
  }
 
  public static void eksekusi (int a, int b)
  {
      boolean y;
      if (a<=b)
      {
            y=true;
            System.out.println(+a+ "<=" +b+ " mengembalikan nilai true");
      }
      else
      {
            y=false;
            System.out.println(+b+ ">" +a+ " mengembalikan nilai false");
      }
  }
 
  public static void main(String[] args)
  {
      eksekusi (2);
      eksekusi (11);
      eksekusi (2,7);  //pemanggilan fungsi
      eksekusi (9,3);
      eksekusi (9,9);
  }
  }
Output :

7.      Program Input/Output

Syntax :
import java .io.*;

public class prak3 {
public static void main (String[]args){
BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));
String x = " "; //deklarasi
String y = " "; //deklarasi

try {
System.out.println ("\nEnter your name      : ");//input nama
x=dataIn.readLine();
System.out.println ("\nEnter yout Adjective : ");//input sifat
y=dataIn.readLine();
}
catch (IOException e){
System.out.println ("Error");
}
System.out.println ("\nHello...my name is "+(x));
System.out.println ("\nI'm very " +( y )+ " person");
}
}

Output 

0 komentar

Post a Comment