Created
February 19, 2018 13:49
-
-
Save alfianyusufabdullah/4390a3f67374ec3352009e89631d181a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package kalkulator; | |
| public class Kalkulator { | |
| private static Kalkulator instance; | |
| public static final int KALKULATOR_PENAMBAHAN = 0; | |
| public static final int KALKULATOR_PERKALIAN = 1; | |
| public static final int KALKULATOR_PEMBAGIAN = 2; | |
| public static final int KALKULATOR_PENGURANGAN = 3; | |
| private int Operator; | |
| private Kalkulator() { | |
| } | |
| public static Kalkulator newInstance() { | |
| if (instance == null) { | |
| instance = new Kalkulator(); | |
| } | |
| return instance; | |
| } | |
| public Kalkulator setOperator(int Operator) { | |
| instance.Operator = Operator; | |
| return instance; | |
| } | |
| public double hitung(double nilaiA, double nilaiB) { | |
| switch (instance.Operator) { | |
| case KALKULATOR_PEMBAGIAN: | |
| return nilaiA / nilaiB; | |
| case KALKULATOR_PENAMBAHAN: | |
| return nilaiA + nilaiB; | |
| case KALKULATOR_PENGURANGAN: | |
| return nilaiA - nilaiB; | |
| case KALKULATOR_PERKALIAN: | |
| return nilaiA * nilaiB; | |
| default: | |
| return 0; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment