Bikin program Remote AC pake BlueJ?! Sederhana dulu!
Pada minggu ini, saya diberikan tugas untuk memrogram sebuah Remote AC dengan menggunakan BlueJ. Program ini cukup sederhana, dengan fiturnya adalah sebagai berikut.
Tampilan
Menu Awal
Mengganti Temperatur
Mengganti Mode
Mengganti Kecepatan Fan
Tampilan setelah adanya penggantian
Mematikan AC
Source Code
RemoteAC
Class sebagai pengendali fungsi-fungsi pada class IntMain. /**
* Remote AC
* @author Indira Nursyamsina Hazimi
* 05111740000082
* PBO-A
*/
public class RemoteAC
{
public int temperature;
public int mode;
public int fanspeed;
public RemoteAC(int initialTemp)//inisialisasi
{
temperature=initialTemp;
mode=1;
fanspeed=1;
}
public void acMode(int currentMode)//mode awal
{
if(currentMode==1)
{
System.out.println("Current Mode: Automatic");
}
else if(currentMode==2)
{
System.out.println("Current Mode: Cold");
}
else if(currentMode==3)
{
System.out.println("Current Mode: Dry");
}
}
public void chooseMode(int currentMode)//ganti mode
{
acMode(currentMode);
System.out.println("Change mode to:");
System.out.println("1. Automatic");
System.out.println("2. Cold");
System.out.println("3. Dry");
}
public int changeMode(int newMode)//mode baru
{
if(newMode == 1)
{
mode=newMode;
System.out.println("New Mode : Automatic");
}
else if(newMode == 2)
{
mode=newMode;
System.out.println("New Mode : Cold");
}
else if(newMode == 3)
{
mode=newMode;
System.out.println("New Mode : Dry");
}
return(mode);
}
public void temps(int currentTemp)//ubah temperatur
{
System.out.println("Current Temperature:"+currentTemp);
System.out.println("1. Up");
System.out.println("2. Down");
}
public int temperatureUp(int up)//menaikkan temperatur
{
up++;
return(up);
}
public int temperatureDown(int down)//menurunkan temperatur
{
down--;
return(down);
}
public void fSpeed(int currentSpeed)//fan speed awal
{
if(currentSpeed==1)
{
System.out.println("Currrent Speed: 1");
}
else if(currentSpeed==2)
{
System.out.println("Currrent Speed: 2");
}
else if(currentSpeed==3)
{
System.out.println("Currrent Speed: 3");
}
else if(currentSpeed==4)
{
System.out.println("Currrent Speed: Automatic");
}
}
public void fspeedMode(int speed)//ganti fan speed
{
fSpeed(speed);
System.out.println("Change Fan Speed to:");
System.out.println("1. Fan Speed 1");
System.out.println("2. Fan Speed 2");
System.out.println("3. Fan Speed 3");
System.out.println("4. Fan Speed Automatic");
}
public int acFS(int newSpeed)//fan speed baru
{
if(newSpeed==4)
{
mode=newSpeed;
System.out.println("New Fan Speed: Automatic");
}
else
{
mode=newSpeed;
System.out.println("New Fan Speed: "+newSpeed);
}
return(mode);
}
}
IntMain
Class sebagai pengeksekusi program. /**
* Remote AC
* @author Indira Nursyamsina Hazimi
* 05111740000082
* PBO-A
*/
import java.util.Scanner;
public class IntMain
{
public static void main()
{
System.out.println(".............................");
System.out.println("======AC Remote Control======");
System.out.println(".............................");
System.out.println("Enter Temperature :");
Scanner scan= new Scanner(System.in);
System.out.println(".............................");
int initialtemp;
int temp,modevalue,fan;
int tempTemp,smntr,modeAC,smntr1,FanSpd,count;
int menu;
boolean status=false;
initialtemp=scan.nextInt();
RemoteAC remAC=new RemoteAC(initialtemp);
modeAC=remAC.mode;
FanSpd=remAC.fanspeed;
while(status!=true)
{
System.out.println(".............................");
System.out.println("======AC Remote Control======");
System.out.println(".............................");
System.out.println("Temperature : "+initialtemp+" °C");
remAC.acMode(modeAC);
remAC.fSpeed(FanSpd);
System.out.println(".............................");
System.out.println(".............................");
System.out.println("Menu");
System.out.println("1. Temperature");
System.out.println("2. Mode");
System.out.println("3. Fan Speed");
System.out.println("4. Turn Off AC");
System.out.println(".............................");
menu=scan.nextInt();
System.out.print('\u000C');//clear screen
if(menu==1)
{
System.out.println(".............................");
System.out.println("======AC Remote Control======");
System.out.println(".............................");
System.out.println("Temperature : "+initialtemp+ " °C");
remAC.acMode(modeAC);
remAC.fSpeed(FanSpd);
System.out.println(".............................");
System.out.println(".............................");
remAC.temps(initialtemp);
System.out.println(".............................");
temp=scan.nextInt();
System.out.print('\u000C');
if(temp==1)
{
tempTemp=remAC.temperatureUp(initialtemp);
initialtemp=tempTemp;
}
else if(temp==2)
{
tempTemp=remAC.temperatureDown(initialtemp);
initialtemp=tempTemp;
}
System.out.print('\u000C');
}
else if(menu==2)
{
System.out.println(".............................");
System.out.println("======AC Remote Control======");
System.out.println(".............................");
System.out.println("Temperature : "+initialtemp+" °C");
remAC.acMode(modeAC);
remAC.fSpeed(FanSpd);
System.out.println(".............................");
System.out.println(".............................");
remAC.chooseMode(modeAC);
System.out.println(".............................");
modevalue = scan.nextInt();
smntr=remAC.changeMode(modevalue);
modeAC = smntr;
System.out.print('\u000C');
}
else if(menu==3)
{
System.out.println(".............................");
System.out.println("======AC Remote Control======");
System.out.println(".............................");
System.out.println("Temperature : "+initialtemp+" °C");
remAC.acMode(modeAC);
remAC.fSpeed(FanSpd);
System.out.println(".............................");
System.out.println(".............................");
remAC.fspeedMode(FanSpd);
System.out.println(".............................");
fan=scan.nextInt();
smntr1=remAC.acFS(fan);
FanSpd=smntr1;
System.out.print('\u000C');
}
else if(menu==4)
{
System.out.print('\u000C');
System.out.println(".............................");
System.out.println("======Turning the AC Off=====");
System.out.println(".............................");
System.out.println(".......Have a nice day.......");
status = true;
}
}
}
}
Komentar
Posting Komentar