Contoh Program Java sederhana Untuk Belajar
1. Menghitung Luas dan Keliling Lingkaran
import java.io.*;
public class menghitungLuasdanKeliling
{
public static void main (String args[])
{
try
{
int phi=22/7;
System.out.print(“jari2=”);
BufferedReader input1= new BufferedReader(new InputStreamReader(System.in));
String jari2=input1.readLine();
int r=Integer.parseInt(jari2);
int Luas=phi*r*r;
int Keliling=2*phi*r;
System.out.print(“Luas=”+Luas+”\n”+”Keliling=”+Keliling);
}
catch(IOException e)
{
}
}
}
Hasil Eksekusi Luas dan Keliling
2. Konversi Fahrenheit ke Celcius dan Reamur
import javax.swing.*;
public class konversiFahrenheit
{
public static void main (String args[])
{
String input=JOptionPane.showInputDialog(“masukkan fahrenheit”);
int TF=Integer.parseInt(input);
System.out.println(“Fahrenheit=”+TF);
int TC=5*(TF-32)/9;
int TR=TC*4/5;
System.out.println(“Celcius=”+TC+”\n”+”Reamur=”+TR);
System.exit(0);
}
}
Hasil Eksekusi Konversi
TUGAS PRAKTIKUM 2
1. Hari-hari dalam satu minggu
public class hari
{
public static void main (String args[])
{
String []data ={“Senin”,”Selasa”,”Rabu”,”Kamis”,”Jumat”,”Sabtu”,”Minggu”};
for(int i=0;i<data.length;i++)
{
System.out.println(“Hari “+data[i]);
}
}
}
Hasil Eksekusi Hari
2. Menentukan Bilangan Prima (0-1000)
public class prima
{
int[] bil=new int[1000];
public int[]inputdata()
{
for (int i=0; i<bil.length;i++)
{
bil[i]=i;
}
return bil;
}
public int[]cekprima()
{
int cek=0;
int[] data=new int[bil.length];
for (int i=0;i<bil.length;i++)
{
data [2]=2;
for (int j=2;j<bil[i];j++)
{
cek=(bil [i])%j;
if(cek==0)
break;
}
if (cek!=0)
data[i]=bil[i];
}
return data;
}
public void cetakprima(int[]cetak)
{
System.out.print (“Bilangan prima dari 0-1000:\n”);
for (int i=0; i<cetak.length; i++)
{
if (cetak[i]>0)
{
System.out.print (cetak[i]+” “);
}
}
System.out.println();
}
public static void main(String []args)
{
prima p =new prima();
p.inputdata();
p.cekprima();
p.cetakprima(p.cekprima());
}
}
Hasil Eksekusi Bilangan Prima (0-1000)
3. Menentukan bilangan prima/bukan dengan menginputkan angka dari Keyboard
import java.io.*;
public class primaViaKeyboard
{
public static void main(String[] args)
{
BufferedReader dataIn = new BufferedReader(new
InputStreamReader(System.in));
int bil,pembagi = 2;
String chek = “”;
String numStr = “”;
System.out.print(“Masukkan bilangan : “);
try
{
numStr = dataIn.readLine();
}
catch( IOException e )
{
System.out.println(“Error!”);
}
bil = Integer.parseInt(numStr);
do
{
if (bil % pembagi ==0 || bil<=2)
{
chek = “BUKAN bilangan PRIMA”;
break;
}
if (pembagi>= bil/2 )
{
chek = “bilangan PRIMA”;
break;
}
pembagi++;
}
while (pembagi<=bil/2);
System.out.println(bil + ” adalah” +chek);
}
}
Hasil Eksekusi bilangan prima/bukan dengan menginputkan angka dari Keyboard
4. Mencari rata-rata dari n bilangan
import javax.swing.*;
public class ratarata
{
public static void main (String args[])
{
System.out.println(“Data Yang Di Masukkan Melalui Keyboard:”);
String input1=JOptionPane.showInputDialog(“data 1: “);
int d1=Integer.parseInt(input1);
System.out.println(“data1: “+d1);
String input2=JOptionPane.showInputDialog(“data 2: “);
int d2=Integer.parseInt(input2);
System.out.println(“data2: “+d2);
String input3=JOptionPane.showInputDialog(“data 3: “);
int d3=Integer.parseInt(input3);
System.out.println(“data3: “+d3);
String input4=JOptionPane.showInputDialog(“data 4: “);
int d4=Integer.parseInt(input4);
System.out.println(“data4: “+d4);
String input5=JOptionPane.showInputDialog(“data 5: “);
int d5=Integer.parseInt(input5);
System.out.println(“data5: “+d5);
int ratarata = (d1+d2+d3+d4+d5)/5;
System.out.println(“nilai rata-rata=”+ratarata);
System.exit(0);
}
}
Hasil Eksekusi Mencari rata-rata dari n bilangan
TUGAS PRAKTIKUM 3
1. Penjumlahan Matrik dengan menginputkan data dari keyboard
import javax.swing.*;
public class penjumlahanmatrik
{
public static void main (String args[])
{
System.out.println(“Data Yang Di Masukkan Melalui Keyboard:”);
String input1=JOptionPane.showInputDialog(“data 1: “);
int m1=Integer.parseInt(input1);
System.out.print(” “+m1);
String input2=JOptionPane.showInputDialog(“data 2: “);
int m2=Integer.parseInt(input2);
System.out.print(” “+m2);
String input3=JOptionPane.showInputDialog(“data 3: “);
int m3=Integer.parseInt(input3);
System.out.print(” “+m3);
String input4=JOptionPane.showInputDialog(“data 4: “);
int m4=Integer.parseInt(input4);
System.out.print(” “+m4);
String input5=JOptionPane.showInputDialog(“data 5: “);
int m5=Integer.parseInt(input5);
System.out.print(” “+m5);
String input6=JOptionPane.showInputDialog(“data 6: “);
int m6=Integer.parseInt(input6);
System.out.println(” “+m6);
String input7=JOptionPane.showInputDialog(“data 7: “);
int m7=Integer.parseInt(input7);
System.out.print(” “+m7);
String input8=JOptionPane.showInputDialog(“data 8: “);
int m8=Integer.parseInt(input8);
System.out.print(” “+m8);
String input9=JOptionPane.showInputDialog(“data 9: “);
int m9=Integer.parseInt(input9);
System.out.print(” “+m9);
String input10=JOptionPane.showInputDialog(“data 10: “);
int m10=Integer.parseInt(input10);
System.out.print(” “+m10);
String input11=JOptionPane.showInputDialog(“data 11: “);
int m11=Integer.parseInt(input11);
System.out.print(” “+m11);
String input12=JOptionPane.showInputDialog(“data 12: “);
int m12=Integer.parseInt(input12);
System.out.println(” “+m12);
int[][]matrik1={{m1,m2,m3},{m4,m5,m6}};
int[][]matrik2={{m7,m8,m9},{m10,m11,m12}};
int[][]jumlah=new int[matrik1.length][matrik1[0].length];
System.out.println(“data matrik1:”);
for(int i=0;i<matrik1.length;i++)
{
for(int j=0;j<matrik1[0].length;j++)
{
System.out.print( matrik1[i][j]+” “);
}
System.out.println();
}
System.out.println(“data matrik2:”);
for(int i=0;i<matrik2.length;i++)
{
for(int j=0;j<matrik2[0].length;j++)
{
System.out.print( matrik2[i][j]+” “);
}
System.out.println();
}
for (int i=0;i<jumlah.length;i++)
{
for (int j=0;j<jumlah[0].length;j++)
{
jumlah[i][j]=matrik1[i][j]+matrik2[i][j];
}
}
System.out.println(“hasil penjumlahan:”);
for (int i=0;i<matrik2.length;i++)
{
for (int j=0;j<matrik2[0].length;j++)
{
System.out.print(jumlah[i][j]+” “);
}
System.out.println();
}
System.exit(0);
}
}
Hasil Eksekusi Penjumlahan Matrik dengan menginputkan data dari keyboard
Thanks for reading & sharing Kamar Pekick
0 komentar:
Post a Comment