Advertisement
16120

Constructor

Feb 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package oop;
  2.  
  3. public class oopfile {
  4.  
  5.     public static void main(String[] args) throws Exception {
  6.     Kifli oopfile = new Kifli("Kifli", 120, "chocolate", 60);
  7.     oopfile.printInfo();
  8.     }
  9.  
  10. }
  11.  
  12. package oop;
  13.  
  14. public class Kifli {
  15.     String type;
  16.     double volume;
  17.     String filling;
  18.     double quantity;
  19.  
  20. public Kifli(String type, double volume, String filling, double quantity) {
  21.     this.type = type;
  22.     this.volume = volume;
  23.     this.filling = filling;
  24.     this.quantity = quantity;
  25. }
  26.    
  27.     void printInfo() {
  28.         System.out.println("sugar product: " + this.type);
  29.         System.out.println("volume: " + this.volume);
  30.         System.out.println("filling: " + this.filling);
  31.         System.out.printf("quantity: %.2f\n", this.quantity);
  32.     }
  33.  
  34.     void put(double number) throws Exception {
  35.         if ((number + this.quantity) > this.volume) {
  36.             throw new Exception();
  37.         } else {
  38.             this.quantity += number;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement