Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1.  
  2. public class Ital {
  3.  
  4.     protected double mennyiség;
  5.     protected boolean alkoholmentes;
  6.  
  7.     public Ital(double mennyiség, boolean alkoholmentes) {
  8.         this.mennyiség = mennyiség;
  9.         this.alkoholmentes = alkoholmentes;
  10.         System.out.println("Új ital: " + this);
  11.     }
  12.  
  13.     public Ital(double mennyiség) {
  14.         this(mennyiség, false);
  15.     }
  16.  
  17.     public Ital kever(Ital i) {
  18.         boolean alkMent = alkoholmentes && i.alkoholmentes;
  19.         System.out.println("I + I: " + this + " + " + i);
  20.         return new Ital(mennyiség + i.mennyiség, alkMent);
  21.     }
  22.  
  23. }
  24.  
  25. class AlkoholMentes extends Ital {
  26.    
  27.     public AlkoholMentes(double mennyiség) {
  28.         super(mennyiség, true);
  29.         System.out.println("Új alkoholmentes ital: " + this);
  30.     }
  31.  
  32.     @Override
  33.     public Ital kever(Ital i) {
  34.         System.out.println("M + I: " + this + " + " + i);
  35.         return new Ital(mennyiség + i.mennyiség, i.alkoholmentes);
  36.     }
  37.     public Ital kever(AlkoholMentes a) {
  38.         System.out.println("M + M: " + this + " + " + a);
  39.         return new AlkoholMentes(mennyiség + a.mennyiség);
  40.     }
  41.  
  42. }
  43.  
  44.  
  45. public class ItalFeladat {
  46.  
  47.     public static void main(String[] args) {
  48.         Ital ii = new Ital(0.1);
  49.         Ital mi = new AlkoholMentes(0.3);
  50.         AlkoholMentes mm = new AlkoholMentes(0.5);
  51.  
  52.         System.out.println(mi.kever(mm));
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement