Switch Case di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package switchname;

/**
*
* @author komp19
*/
public class switchName {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
char firstInitial = ‘a’;
System.out.println(“Masukkan huruf awal nama Anda :”);
try {
firstInitial = (char)System.in.read();
} catch (Exception e){
System.out.println(“Error: ” + e.toString());
}
switch(firstInitial){
case ‘a’:
System.out.println(“Nama anda pasti Asep!”);
break;
case ‘b’:
System.out.println(“Nama anda pasti Brodin!”);
break;
case ‘c’:
System.out.println(“Nama anda pasti Cecep!”);
break;

default:
System.out.println(“Nama anda tidak dikenal!”);
}
}
}

Mengitung Selisih Waktu di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package selisihwaktu;

import java.util.Calendar;

/**
*
* @author Angga
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Creates two calendars instances
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

// Set the date for both of the calendar instance
cal1.set(2006,12,30);
cal2.set(2007,5,3);

//Get the represented date in milliseconds
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();

//Calculate difference in milliseconds
long diff = milis2 – milis1;

// Calculate difference in seconds
long diffSeconds = diff / 1000;

// Calculate difference in minutes
long diffMinutes = diff / (60 * 1000);

// Calculate difference in hours
long diffHours = diff / (60 * 60 * 1000);

//Calculate difference in days
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.println(“In milliseconds: “+ diff +” milliseconds.”);
System.out.println(“In seconds: “+ diffSeconds +” seconds.”);
System.out.println(“In minutes: “+ diffMinutes +” minutes.”);
System.out.println(“In hour: “+ diffHours +” hours.”);
System.out.println(“In days: “+ diffDays +” days.”);

}

}

Selection Sort di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author HP
*/
public class SelectionSort {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] data={-5,-8,-11,0,34,1,1,6,9,22,15};
int i,j,tampung,pos;
System.out.println(” Sorting (SELECTION) Oleh :”);
System.out.println(” NAMA : ANGGA ARI WIJAYA”);
System.out.println(” N.I.M : 102410101070″+”\n”);
System.out.println(” Data Sebelum sorting “);
for(i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println("");
for(i=0;i<data.length-1;i++){
pos=i;
for(j=i+1;j<data.length;j++){
if(data[j]<data[pos]){
pos=j;
}
}
if(pos!=i){
tampung=data[pos];
data[pos]=data[i];
data[i]=tampung;
}
}
System.out.println(" Data Setelah sorting");
for(i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println("");
}
}

Membuat Segitiga Bintang (*) Menyamping di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package segitigabintang;

/**
*
* @author Bio
*/
public class segitigaBintang {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int baris, kolom, tinggi=4, tinggi2=0;
// membuat 4 baris bintang pertama (naik)
for(baris=1; baris <= tinggi; baris++){
for(kolom=0; kolom tinggi2; –baris){
for(kolom=0; kolom <baris;kolom++)
System.out.print("*");
System.out.println("");
}
}
}

Menghitung Luas Segitiga Menggunakan JOptionPane di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package programluassegitiga;
import javax.swing.*;
/**
*
* @author Angga
*/
public class LuasSegitiga {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println(“Program Menghitung luas segitiga :”);

String alas = “”;
alas = JOptionPane.showInputDialog(“Ayuk Masukkan alasnya : “);
int a=Integer.parseInt(alas);

String tinggi = “”;
tinggi = JOptionPane.showInputDialog(“Ayuk Masukkan tingginya : “);
int t=Integer.parseInt(tinggi);

System.out.println (“Alas = “+(a));
System.out.println (“Tinggi = “+(t));
System.out.println (“Luasnya = “+(a*t/2));
System.out.println (“Created By ANGGA”);

}

}

Studi Kasus Menghitung Gaji Karyawan di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package percobaan1studikasusgajikaryawan;

/**
*
* @author Angga
*/
public class Main {

/**
* @param args the command line arguments
*/
/*program menhitung gaji bersih karyawan, Gaji bersih yang
diterima pegawai tersebut adalah:
Gaji bersih = gaji pokok + tunjangan – pajak
Tunjangan karyawan dihitung 20% dari gaji pokok, sedangkan
pajaknya adalah 15% dari gaji pokok + tunjangan.*/
public static void main(String[] args) {
// TODO code application logic here

float GajiPokok = 500000;
float Tunjangan;
float pajak;
float gaji_bersih;
String Nama_Karyawan = “Ignas Subekti”;

//perhitungan gaji pegawai
Tunjangan = 0.2f*GajiPokok;
pajak = 0.15f *(GajiPokok + Tunjangan);
gaji_bersih = GajiPokok + Tunjangan – pajak;
System.out.println (“Nama Karyawan :” + Nama_Karyawan);
System.out.println (“gaji bersihnya adalah :” + gaji_bersih);

}

}

Menyalin Nilai Array menggunakan CopyFrom dan CopyTo di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package mengcopyarray;

/**
*
* @author uptti
*/
public class mengCopyArray {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
char[] copyFrom = {‘d’,’e’,’c’,’a’,’f’,’f’,’e’,’i’,’n’,’a’,’t’,’e’,’d’};
char[] copyTo= new char[7];

System.arraycopy(copyFrom, 2,copyTo, 0, 7);

for(int i=0; i<copyTo.length; i++){
System.out.println(i+". "+copyTo[i]);
}

}

}

Menghitung Luas Persegi Panjang di Java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package lpersegipanjang;

/**
*
* @author Angga
*/
public class LPersegiPanjang {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int p = 8;
int l = 9;
int L;
//menghitung luas persegi panjang
//tanda *(kali) dan = (sama dengan) merupakan operator
//sedangkan p dan l merupakan operandnya
//semua yang ada dalam blok ({}) merupakan statemen
L = p * l;
System.out.println(“Luas persegi panjang tersebut adalah :”+L);

}

}