Advertisement
ivana_andreevska

calculator zadaca

Aug 10th, 2022
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. prvo pravime interfejs za olesnuvanje na metodite / kalkulaciite
  2. package AV4;
  3.  
  4. public interface Strategy {
  5.  
  6.     double calculate(double num1, double num2);
  7. }
  8.  potoa pravime nova klasa koja go implementira toj interfejs
  9. package AV4;
  10.  
  11. public class Addition implements Strategy{
  12.     @Override
  13.     public double calculate(double num1, double num2) {
  14.         return num1+num2;
  15.     }
  16. }
  17. i na kraj vo originalnata calculator klasa
  18. public class Calculator {
  19.     private double rezultat;
  20.     private Strategy strategy;
  21.  
  22.     public Calculator(double rezultat) {
  23.         this.rezultat = 0.0;
  24.     }
  25.  
  26.     public String execute(char operation, double value) {
  27.         if (operation == '+') {
  28.             strategy= new Addition();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement