Advertisement
Guest User

KRS-OOP

a guest
Apr 1st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. package mahasiswa;
  2.  
  3. class mhs {
  4.     private int npm;
  5.     private String nama;
  6.  
  7.     public mhs() {
  8.         npm = 0;
  9.         nama = "";
  10.     }
  11.  
  12.     public mhs(int n, String nm) {
  13.         npm = n;
  14.         nama = nm;
  15.         System.out.println("Constructor Mahasiswa: " + npm + " " + nama);
  16.     }
  17.  
  18.     public String cetak() {
  19.         return npm + " " + nama;
  20.     }
  21. }
  22.  
  23. class matakuliah {
  24.     private String kode;
  25.     private String nama_mk;
  26.     public static int jumlahmk;
  27.  
  28.     public matakuliah() {
  29.         kode = "";
  30.         nama_mk = "";
  31.     }
  32.  
  33.     public matakuliah(String k, String nm) {
  34.         kode = k;
  35.         nama_mk = nm;
  36.         System.out.println("Constructor Mata Kuliah: " + kode + " " + nama_mk);
  37.         jumlahmk++;
  38.     }
  39.  
  40.     public String cetak() {
  41.         return nama_mk;
  42.     }
  43.  
  44.     public String getcode() {
  45.         return kode;
  46.     }
  47. }
  48.  
  49. class krs {
  50.     private mhs peserta;
  51.     private matakuliah mk;
  52.     public static int jumlahkrs;
  53.  
  54.     public krs(mhs m, matakuliah k) {
  55.         peserta = new mhs();
  56.         peserta = m;
  57.         mk = new matakuliah();
  58.         mk = k;
  59.         jumlahkrs++;
  60.     }
  61.  
  62.     public String getkode() {
  63.         return mk.getcode();
  64.     }
  65.  
  66.     public String cetak() {
  67.         return peserta.cetak();
  68.     }
  69. }
  70.  
  71. public class Mahasiswa {
  72.  
  73.     /**
  74.      * @param args
  75.      *            the command line arguments
  76.      */
  77.     public static void main(String[] args) {
  78.         // TODO code application logic here
  79.         mhs si1 = new mhs(123, "budi");
  80.         mhs si2 = new mhs(456, "amin");
  81.         mhs si3 = new mhs(789, "dedi");
  82.         matakuliah mk[] = new matakuliah[2];
  83.         mk[0] = new matakuliah("1", "BP");
  84.         mk[1] = new matakuliah("2", "PTI");
  85.         krs krsku[] = new krs[5];
  86.         System.out.println("\nProgram Mahasiswa Kuliah");
  87.         krsku[0] = new krs(si1, mk[0]); // mhs si mengambil matkul mk
  88.         krsku[1] = new krs(si1, mk[1]);
  89.         krsku[2] = new krs(si2, mk[0]);
  90.         krsku[3] = new krs(si3, mk[0]);
  91.         krsku[4] = new krs(si3, mk[1]);
  92.         // menampilkan berdasarkan kode mata kuliahnya
  93.         System.out.println("Jumlah KRS: " + krs.jumlahkrs + " record");
  94.         System.out.println("Jumlah mata kuliah: " + mk[1].jumlahmk + "\n");
  95.  
  96.         for (int i = 0; i < mk[1].jumlahmk; i++) {
  97.             System.out.println("Nama MK: " + mk[i].cetak());
  98.             for (int j = 0; j < krsku[4].jumlahkrs; j++) {
  99.                 if ((krsku[j].getkode().compareTo(Integer.toString(i + 1))) == 0)
  100.                     System.out.println(krsku[j].cetak());
  101.             }
  102.             System.out.println();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement