Contoh Program Java

  • Uploaded by: Eddy Purwoko
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Contoh Program Java as PDF for free.

More details

  • Words: 1,122
  • Pages: 16
Tugas JAVA Nama : Eddy Purwoko NPM : 2008 435 00 845 Kelas III - V ( Sore ) ================================================================ Tabel Bulan dan Tabel Data yg berjumlah 1000 :

INPUT : import java.util.Random; /** * * @author Nama : Eddy Purwoko * @author NPM : 2008 435 00 845 */

class p1{ public static void main(String[] args){ int x[][] = new int[8][12]; int jkolom[] = new int[12]; String bulan[]={"jan","Feb","mar","Apr","Mei","jun","Jul","Ags","Sep", "Okt","Nov","Des"}; String Umur[]={"s.d 18","19","20","21","22","23","24","25+"}; Random r1 = new Random( ); Random r2 = new Random( ); int bln, umur; for(int i=0; i<1000; i++){

bln = r1.nextInt(12); umur = r2.nextInt(8); x[umur][bln]+=1; } int jumlah=0; System.out.print("\t"); for(int baris=0;baris<12;baris++) System.out.print(bulan[baris]+"\t"); System.out.println("\n----------------------------------------------------------"+ "------------------------------------------"); for(int baris=0;baris<8;baris++){ System.out.print(Umur[baris]+"\t"); for(int kolom=0;kolom<12;kolom++) { System.out.print(x[baris][kolom]+"\t"); jumlah+=x[baris][kolom]; jkolom[kolom]+=x[baris][kolom]; } System.out.println(); } System.out.print("\t"); for (int kolom=0; kolom<12; kolom++) System.out.print(jkolom[kolom]+"\t"); System.out.println("\n\n ---> jumlah data : "+jumlah);

System.out.print("\t");

for (int kolom=0; kolom<12; kolom++) System.out.print(jkolom[kolom]+"\t"); System.out.println("\n\n ---> jumlah data : "+jumlah);

int imin, imax, jmin, jmax,i,j; int max = x[0][0]; jmax=jmin=imax=imin=0; int min = x[0][0]; for (i=0;i<8;i++) for (j=0;j<12;j++) {if (x[i][j] > max) {max = x [i][j]; imax=i;jmax=j;} if (x[i][j] <min) {min = x [i][j];imin=i;jmin=j;} } System.out.println("max = " + max+" i= "+imax+" j= "+jmax); } }

OUTPUT :

jan

Feb

mar

Apr

Mei

jun

Jul

Ags

Sep

Okt

Nov

Des

---------------------------------------------------------------------------------------------------s.d 18 7

12

11

10

15

6

12

8

10

10

11

12

19

13

9

10

10

9

15

19

12

11

8

18

13

20

5

14

13

6

10

12

11

14

15

11

19

6

21

5

8

7

8

11

9

12

10

9

16

14

9

22

9

8

8

8

13

11

7

7

10

10

14

10

23

10

2

5

13

14

13

11

5

11

10

6

10

24

11

8

11

10

10

10

11

9

9

12

8

17

25+

13

9

7

15

12

8

10

14

6

8

12

10

73

70

72

80

94

84

93

79

81

85

102

87

80

94

84

93

79

81

85

102

87

---> jumlah data : 1000 73

70

72

---> jumlah data : 1000 max = 19 i= 1 j= 6

Nilai Rata-2

INPUT :

class r1 /** * * @author Nama : Eddy Purwoko * @author NPM : 2008 435 00 845 */

{ public static void main (String [] args ) { int [] x = {11,12,13,10,9}; int jumlah = 0 ; for ( int i = 0; i<x.length; i++) jumlah = jumlah +x[i]; double rata2 = ( double ) jumlah /x.length; for(int i=0; i<x.length; i++){ System.out.println(x[i]+" "+rata2+" = "+(x[i]-rata2)); System.out.printf("x %d rata2 %5.2f \n", x[i],rata2);} } } OUTPUT :

11 11.0 = 0.0 x 11 rata2 11.00 12 11.0 = 1.0

x 12 rata2 11.00 13 11.0 = 2.0 x 13 rata2 11.00 10 11.0 = -1.0 x 10 rata2 11.00 9 11.0 = -2.0 x 9 rata2 11.00

Penentuan Tanggal

INPUT : import java.util.Date; /** *

* @author Nama : Eddy Purwoko * @author NPM : 2008 435 00 845 */ class Coba1 {} public class Tanggal {public static void main(String []args) { Date waktuIni = new Date();

System.out.println("" +waktuIni); } } OUTPUT : Tanggal Hari Ini : Sun Oct 18 21:29:33 ICT 2009

INPUT :

import java.util.Date; /**

* * @author Nama : Eddy Purwoko * @author NPM : 2008 435 00 845 */ public class HelloDate1{

public HelloDate1() { }

public static void main(String[] args) { System.out.println("Tanggal berapa Sekarang : " + new Date()); }

}

OUTPUT :

Tanggal berapa Sekarang : Sun Oct 18 21:28:26 ICT 2009

“”” Program Buat Dialog Message “”” Input : importjavax.swing.JOptionPane; /* Membuat dialog box */ public class Welcome { public static void main(String args[]) {JOptionPane.showMessageDialog(null

, "Welcome\nto\nJava Programming!"); System.exit(0);}}

package ratarata; import java.io.*; public class RataRata { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double jumlah = 0; double bilangan = 0; int n = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String strbilangan = null;

System.out.print("Masukkan bilangan pertama : "); try { strbilangan = br.readLine(); } catch (IOException ioe) { System.out.println("Kesalahan IO, program berhenti"); System.exit(1); } // mengubah input menjadi double agar bisa diproses lebih lanjut bilangan = Double.parseDouble(strbilangan); while (bilangan != 0) { jumlah += bilangan; n++;

// sama dengan : jumlah = jumlah + bilangan // sama dengan : n = n+1

// tanya user input System.out.print("Masukkan bilangan berikutnya (atau 0 untuk mengakhiri) : "); try { strbilangan = br.readLine(); } catch (IOException ioe) { System.out.println("Kesalahan IO, program berhenti"); System.exit(1); } // mengubah input menjadi double agar bisa diproses lebih lanjut bilangan = Double.parseDouble(strbilangan); } // hitung rata-rata double ratarata = jumlah/n; // cetak hasilnya ke layar if (n == 0) { System.out.println("Data kosong, rata-rata tidak bisa dihitung"); } else { System.out.println("Anda memasukkan " + n + " data"); System.out.println("Rata-rata bilangan yang dimasukkan adalah " + ratarata); } } }

import java.io.*; public class MainSuit { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // cetak aturan main dan input dari user System.out.println("Permainan suit"); System.out.println("=============="); System.out.println("Masukkan salah satu dari 3 kemungkinan :"); System.out.println("J untuk Jempol");

System.out.println("T untuk Telunjuk"); System.out.println("K untuk Kelingking"); System.out.println(""); // baris kosong // variabel untuk mengambil input dari user BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = null; // Kita gunakan pernyataan while hingga user betul // memasukkan salah satu dari J, T, atau K boolean inputvalid = false; int suitKomputer = 0; int suitUser = 0; // selama input tidak valid, jalankan perulangan ini while (!inputvalid) { System.out.print("Masukkan suit Anda "); try { input = br.readLine(); } catch (IOException ioe) { System.out.println("Kesalahan IO, program berhenti"); System.exit(1); }

if (input.length() > 0) { switch (input.charAt(0)) { case 'j' : case 'J' : suitUser = 0; inputvalid = true; break; case 't' : case 'T' : suitUser = 1; inputvalid = true; break; case 'k' : case 'K' : suitUser = 2; inputvalid = true; } } } suitKomputer = (int)(Math.random()*3);

break;

if (suitKomputer == 3) { suitKomputer = 2; }

switch (suitKomputer) { // jika komputer Jempol case 0: // cetak suit Komputer System.out.println("Suit komputer = Jempol"); switch (suitUser) { // jika user Jempol case 0: System.out.println("Seri"); break; // jika user Telunjuk case 1: System.out.println("Anda kalah"); break; // jika user Kelingking case 2: System.out.println("Anda menang"); break; } break; // dari switch(suitKomputer) // jika komputer Telunjuk case 1: // cetak suit Komputer System.out.println("Suit komputer = Telunjuk"); switch (suitUser) { // jika user Jempol case 0: System.out.println("Anda menang"); break; // jika user Telunjuk case 1: System.out.println("Seri"); break; // jika user Kelingking case 2: System.out.println("Anda kalah"); break; } Break; case 2:

System.out.println("Suit komputer = Kelingking"); switch (suitUser) { case 0: System.out.println("Anda kalah"); break; case 1: System.out.println("Anda menang"); break; case 2: System.out.println("Seri"); break; } break; } } }

Related Documents


More Documents from ""

E-commerce Dan Cbis
June 2020 18
Modul-iv
June 2020 21
Vb2005 Database Sql
June 2020 20
Gerbang Logika.pdf
June 2020 22