Senin, 09 Oktober 2017

PRAKTEK 4 SEMESTER 3

1
public class SuperClass{
                public SuperClass(){
                                System.out.println("Ini konstruktor milik SuperClass");
                }
                public void methodSuperClass(){
                                System.out.println("Ini method milik SuperClass");
                }
}

2
public class SubClass extends SuperClass{
                public SubClass(){
                                super();
                }
                public void methodSubClass(){
                                super.methodSuperClass();
                }
}

3
public class DemoInheritance{
                public static void main(String[] args){
                                SubClass sc=new SubClass();
                                sc.methodSubClass();
                                }
                                }
4
public class Persegi{
                private double panjang;                                //atribut yang di sembunyikan
                private double lebar;                      //atribut yang di sembunyikan
                private double tinggi;                     //atribut yang di sembunyikan
               
                public Persegi(){
                                panjang=0;
                                lebar=0;
                }
                private double luas(double p,double l){
                                return p*l;
                }
                public void setPanjang(double panjang1){
                                this.panjang=panjang1;
                }
                public void setLebar(double lebar1){
                                this.lebar=lebar1;
                }
                public double getPanjang(){
                                return panjang;
                }
                public double getLebar(){
                                return lebar;
                }
                public double getLuas(){
                                return luas(panjang, lebar);
                }
}

5
public class MainPersegi{
                public static void main(String[] args){
                                Persegi pg=new Persegi();
                                pg.setPanjang(10);
                                pg.setLebar(20);
                               
                                System.out.println("Panjang : "+pg.getPanjang());
                                System.out.println("Lebar : "+pg.getLebar());
                                System.out.println("Luas : "+pg.getLuas());
                                }
                                }

6
abstract class Karyawan{
                int gaji;
                Karyawan(){
                                gaji=1000000;
                                }
                                int getGaji(){
                                                return gaji;
                                                }
                                                abstract void naikGaji();
                                                }
7
class Manager extends Karyawan{
                Manager(){
                                gaji +=2000000;
                                }
                                void naikGaji(){gaji+=3000000; }
                                }

8
class Receptionist extends Karyawan{
                Receptionist(){
                                gaji+=1000000;
                                }
                                void naikGaji(){gaji+=10000000; }
                                }

9
class Polymorphisme{
                public static void main(String[] args){
                                Karyawan K[]=new Karyawan[3];

                                K[0]=new Manager();
                                K[1]=new Receptionist();
                                K[2]=new Manager();

                                for (int i=0;i<3;i++)
                                                System.out.println("Gaji karyawan "+i+" adalah "+K[i].getGaji());

                                for(int i=0;i<3;i++)
                                                K[i].naikGaji();

                                for(int i=0;i<3;i++)
                                                System.out.println("Gaji sekarang "+i+" adalah"+K[i].getGaji());}

                                }

Laporan Praktikum 3 Semester 3


MEMBUAT SUATU SCRIPT PROGRAM JAVA

1. Membuat script program Java dengan nama "Disket.java"

- Ketik script berikut:

public class Disket (
      int panjang = 10;
      final int LEBAR = 10;
      String warna = "kuning";
      String bentuk = "segiempat";
      String data = "";
   
      void simpanData(String data1) {
            this.data += data1;
      }
   
      void hapusData() {
            data = "";
      }
   
      String bacaData() {
            return data;
      }
 }

-  Save dengan nama "Disket" dengan type java (*java)
-  Compile script tadi dengan menekan tombol Ctrl+1,
   jika masih ada yang error harap periksa kembali script yang telah    anda ketik.
-  Run script dengan menekan tombol Ctrl+2



2. Membuat script program Java dengan nama "MainDisket.java"

-  Ketik script berikut :

class Disket (
    public static void main(String [] args){
     Disket disk1 = new Disket ();
     System.out.println("Panjang disket = "+ disk1.panjang);
     System.out.println("Lebar disket = "+ disk1.LEBAR);
     System.out.println("Warna disket = "+ disk1.warna);
     System.out.println("Bentuk disket = "+ disk1.bentuk);
    
     System.out.println("data mula-mula = "+disk1.bacaData());
     disk1.simpanData("Belajar java");
     System.out.println("Setelah SimpanData = "+"+disk1.bacaData());
     disk1.hapusData());
     System.out.println("Setelah hapusData = "+"+disk1.bacaData());
    }
}

-  Save dengan nama "MainDisket" dengan type java (*java)
-  Compile script dengan menekan Ctrl+1,
   jika masih ada yang error harap periksa kembali script yang sudah    anda ketik.
-  Run script dengan menekan tombol Ctrl+2