Kalkulator dalam bahasa java Implementasi OOP
import java.util.Scanner;
class Calculator
{
private operasi opr;
Calculator()
{
opr = new operasi();
}
operasi getopr()
{
return opr;
}
class operasi{
operasi(){
}
int tambah(int x,int y)
{
return (x+y);
}
int kurang(int x,int y)
{
return (x-y);
}
int kali(int x,int y)
{
return (x*y);
}
int bagi(int x,int y)
{
if(y>0)
{
return (x/y);
}else
{
return -999999;
}
}
}
}
public class Main
{
public static void main(String[] args) {
int menu = 0;
int x = 0;
int y = 0;
Calculator cal = new Calculator();
System.out.println("Masukan menu");
Scanner sc = new Scanner(System.in);
try{
menu = sc.nextInt();
}catch(Exception e)
{
}
System.out.println("Masukan x");
try
{
x = sc.nextInt();
}catch(Exception e)
{
}
System.out.println("masukan y");
try
{
y = sc.nextInt();
}catch(Exception e)
{
}
switch(menu)
{
case 1:
System.out.println(cal.getopr().tambah(x,y));
break;
case 2:
System.out.println(cal.getopr().kurang(x,y));
break;
case 3:
System.out.println(cal.getopr().kali(x,y));
break;
case 4:
System.out.println(cal.getopr().bagi(x,y));
break;
}
}
}
Komentar
Posting Komentar