Minggu, 23 September 2018

Tugas PBO Remote TV

Tugas PBO Remote TV


Pada kesempatan kali ini, saya mencoba membuat sebuah remote TV dengan menggunakan Java. Remote TV memiliki beberapa method, yakni,
1. Nyalakan Tivi
2. Tambah Channel
3. Kurangi Channel
4. Set Channel
5. Tambah Volume
6. Kurangi Volume
Apabila Tivi mati maka semua method tersebut tidak dapat digunakan, dan tidak ada channel negatif.
Contoh Output seperti berikut:


 Dari Output diatas, saya menggunakan metode clrscreen untuk tetap memberikan 1 layar tivi dan 1 remote tv, dengan code "System.out.print('\u000C');"
Berikut diagram classnya:

Melihat tivi melalui kelas lihat tv, sedangkan layar diperoleh dari kelas tv, dan interaksi didapat dari kelas remote, berikut source code nya

lihattv :


/**
 * Lihat TV disini
 *
 * @author (M. Rizaldi Huzein P.)
 * @version (9/19/2018)
 */

import java.util.Scanner;

public class lihattv
{
    public static void lihattv()
    {
        int pilih;
        Scanner scan = new Scanner(System.in);
        tv tivi = new tv();
        remote remot = new remote();
        while(true)
        {

            if(remot.getpower()) tivi.tampil(remot.getchannel(),remot.getvol());
            else tivi.mati();
            System.out.println("0. Turn on/off");
            System.out.println("1. Channel +");
            System.out.println("2. Channel -");
            System.out.println("3. Set Channel");
            System.out.println("4. Volume +");
            System.out.println("5. Volume -");
            pilih = scan.nextInt();
            switch(pilih)
            {
                case 0:
                remot.powerset();
                break;
                case 1:
                if(remot.getchannel()<20) remot.chp();
                else if(remot.getchannel()==20) remot.chset(0);
                break;
                case 2:
                if(remot.getpower()){
                if(remot.getchannel()>0) remot.chm();
                else if(remot.getchannel()==0) remot.chset(20);
                }
                break;
                case 3:
                if(remot.getpower()){
                int ch = scan.nextInt();
                if(ch>20) System.out.println("Tidak ada channel");
                else remot.chset(ch);}
                break;
                case 4:
                if(remot.getpower()){
                if(remot.getvol()<10) remot.volp();}
                break;
                case 5:
                if(remot.getpower()){
                if(remot.getvol()>0) remot.volm();}
                break;
            }
            System.out.print('\u000C');
        }
    }
}


tv :

/**
 * Objek TV yang dipakai
 *
 * @author (M. Rizaldi Huzein P.)
 * @version (9/19/2018)
 */
import java.util.Scanner;

public class tv
{
   
   public void tampil(int channel,int volume)
   {
       System.out.println("==================");
       System.out.println("Channel: "+channel);
       System.out.println("==================");
       System.out.println("");
       System.out.println("");
       System.out.println("");
       System.out.println("");
       System.out.print("Volume:");
       for(int i=0;i<volume;i=i+1){System.out.print("|");}
       System.out.println("\n==================");
   }
   public void mati()
   {
       System.out.println("==================");
       System.out.println("    NO SIGNAL");
       System.out.println("==================");
       System.out.println("");
       System.out.println("");
       System.out.println("");
       System.out.println("");
       System.out.print("");
       System.out.println("\n==================");
   }
}


remote:

/**
 * Objek Remote tv
 *
 * @author (M. Rizaldi Huzein P.)
 * @version (9/19/2018)
 */
public class remote
{
    private int channel, vol, brightness, signal;
    private boolean power, avi;
    
    public remote()
    {
        channel=0;
        vol=0;
        power=false;
        avi=true;
        brightness=50;
        signal=114;
    }
    public int getvol()
    {
        return vol;
    }
    public int getchannel()
    {
        return channel;
    }
    public boolean getpower()
    {
        return power;
    }
    public boolean getavi()
    {
        return avi;
    }
    public void volp()
    {
        vol=vol+1;
    }
    public void volm()
    {
        vol=vol-1;
    }
    public void chp()
    {
        channel=channel+1;
    }
    public void chm()
    {
        channel=channel-1;
    }
    public void chset(int ch)
    {
        channel=ch;
    }
    public void powerset()
    {
        if(!power) power=true;
        else power=false;
    }
    public void aviset()
    {
        if(!avi) avi=true;
        else avi=false;
    }
}

Berikut merupakan file BlueJ yang dapat diakses Link file

Tidak ada komentar:

Posting Komentar