Sabtu, 21 Desember 2013

Menu Switch Case pada JAVA



import javax.swing.*;
public class mymenu
{
public static void main (String args[])
{
do 
{
int pil=Integer.parseInt(JOptionPane.showInputDialog
("MENU\n 1. While\n 2. Deret\n 3.Nilai\n 4. Exit\n Masukkan Pilihan [1..4]"));
switch(pil)
{
case 1:
int nilai=Integer.parseInt(JOptionPane.showInputDialog("Masukkan Nilai : "));
while (nilai >= 1)
{
JOptionPane.showMessageDialog(null, "Aku Anak Soleh", 
"Output",JOptionPane.INFORMATION_MESSAGE);

nilai--;
}
System.out.println("\n");
break;

case 2:
int a=Integer.parseInt(JOptionPane.showInputDialog("Masukkan Deret : "));
String gmbr = "Segitiga \n";
for(int i=1;i<=a;i++)
{
              for(int j=1;j<=i;j++)
              {
                  gmbr += "*"+" ";
              }
              gmbr += "\n";
              }
              gmbr +="\n";
            JOptionPane.showMessageDialog(null, gmbr, "Segitiga",JOptionPane.INFORMATION_MESSAGE);
           
           break;

case 3:
int kondisi=Integer.parseInt(JOptionPane.showInputDialog("Masukkan Nilai : "));
if (kondisi > 60)
{
JOptionPane.showMessageDialog(null, "Selamat Anda Lulus","Output",JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Maaf Anda Gagal","Output",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println("\n");
break;
case 4:
System.exit(0);

default:
JOptionPane.showMessageDialog(null, "Silahkan Pilih Dari [1..4]", 
"Anda Salah Pilih",JOptionPane.WARNING_MESSAGE);

}
}
while(true);
}    
}

Penjumlahan Array 1 dimensi pada JAVA



import java.util.Scanner;
class hihi{
private static Scanner input;
public static void main(String[]args){
int a,n,jml_nil=0;
double nil_rata=0;
int[]nilai;
nilai=new int[10];

input=new Scanner(System.in);
System.out.print("Masukkan banyak data =  ");
n=input.nextInt();
System.out.println(" ");
for(a=0;a<n;a++){
System.out.print("Masukkan array ke"+a+"=");
nilai[a]=input.nextInt();}

System.out.println("\n\nData yang diinputkan ke elemen array\n");
for(a=0;a<n;a++){
System.out.print("\nNilai ke - "+a);
System.out.print("="+nilai[a]);
jml_nil=jml_nil+nilai[a];
}
nil_rata=jml_nil/n;
System.out.println("\n----------------");
System.out.println("Jumlah array nilai "+jml_nil);
System.out.println("Nilai rata-rata array nilai ="+nil_rata);
}
}

dejavu pada JAVA



import javax.swing.*;
import java.io.*;

class dejavu {
public static void main (String[]args) {


String a = JOptionPane.showInputDialog("Masukan Angka: ");
int x = Integer.parseInt(a);

if ((x%8==0)) {
a ="DEJAVU";
}
else if ((x%2==0)&&(x%4==0)){
a = "DEJA";
}
else if ((x%2==0)){
a = "DE";
}
JOptionPane.showMessageDialog(null,"Kalimat Terbentuk: "+a,
"DE-JA-VU", JOptionPane.INFORMATION_MESSAGE);
}

    

Array multi dimensi pada JAVA




import javax.swing.JOptionPane;
public class arraymulti{
public static void main(String[]args){
int baris = 0;
int kolom = 0;

baris = Integer.parseInt(JOptionPane.showInputDialog("baris: "));
kolom = Integer.parseInt(JOptionPane.showInputDialog("kolom: "));

int[][] matrixA = new int[baris][kolom];
int[][] matrixB = new int[baris][kolom];
int[][] matrixC = new int[baris][kolom];

for(int x=0; x<baris;x++){
for(int y=0; y<kolom;y++){
matrixA[x][y] = (int)(Math.random()*100);
matrixB[x][y] = (int)(Math.random()*100);
matrixC[x][y] = matrixA[x][y] + matrixB[x][y];
}}

System.out.println("matrixA");
System.out.println("-------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixA[x][y] +"\t");
}
System.out.println();
}
System.out.println();
System.out.println("matrixB");
System.out.println("-------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixB[x][y] +"\t");
}System.out.println(" ");}

System.out.println();
System.out.println("matrix A + B");
System.out.println("------------");
for(int x=0; x<baris; x++){
for(int y=0; y<kolom; y++){
System.out.print(matrixC[x][y] +"\t");
} System.out.println();
}}}

Nilai dan grade pada JAVA

ketikkan codingan dibawah ini pada notepad anda, kemuadian sinpan file sesuai kelas pada program java anda. untuk mengcompile ketikkan javac (nama kelas java anda).java pada cmd (commandprompt), kemudian untuk merunningnya ketikkan java (nama kelas java anda). Perlu anda ingat nama kelas pada java harus sama pada saat anda menyimpan file tersebut, karena pada saat running program akan di panggil untuk di eksekusi.



import javax.swing.*;
import java.io.*;

class nilai1 {
public static void main (String[]args) {
String grade;
double total;
String Ket;
String nama = JOptionPane.showInputDialog("Masukan Nama Anda: ");
String NPM = JOptionPane.showInputDialog("Masukkan NPM Anda : ");
int NiKeh = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Kehadiran : "));
String Midtest = JOptionPane.showInputDialog("Masukan Nilai UTS: ");
double x = Integer.parseInt(Midtest);
String uas = JOptionPane.showInputDialog("Masukan Nilai UAS: ");
double y = Integer.parseInt(uas);


total = (0.1 * NiKeh) + (0.4 * x) + (0.5 * y);

if ((total>=86) && (total<=100)) {
grade = "A";
}
else if ((total>=76) && (total<=85)) {
grade = "B";
}
else if ((total>=66) && (total<=75)) {
grade = "C";
}
else if ((total>=56) && (total<=65)){
grade = "D";
}
else {
grade = "E";
}
if ((grade == "A" || grade == "B" ) || (grade == "A" || grade == "C"))
{
Ket = "LULUS";
}
else
Ket = "TIDAK LULUS";

JOptionPane.showMessageDialog(null,
"NPM           : "+NPM+
"\nNAMA Mahasiswa      : "+nama+
"\nNilai akhir         : "+total+
"\ngrade       : "+grade+
"\nKet               : " +Ket,
"HASIL", JOptionPane.INFORMATION_MESSAGE);

}
}