Advertisement
KhanQ

wedding clink count

Jan 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package wedding;
  2.  
  3. public class Wedding {
  4.  
  5.     private static final int PERSON_COUNT = 14;
  6.     private static final String CLINK_COUNT_MESSAGE = "You hear the %d persons %d times clinking their glasses.";
  7.     private static final String ILLEGAL_PERSON_COUNT_MESSAGE = "Person count smaller 0 not allowed.";
  8.  
  9.     private final int personCount;
  10.  
  11.     public static void main(String[] args) {
  12.         Wedding wedding = new Wedding(PERSON_COUNT);
  13.  
  14.         int clinkCount = wedding.countClinkCount();
  15.         System.out.printf(CLINK_COUNT_MESSAGE, wedding.getPersonCount(), clinkCount);
  16.     }
  17.  
  18.     public Wedding(int personCount) {
  19.         if (personCount < 0) {
  20.             throw new IllegalArgumentException(ILLEGAL_PERSON_COUNT_MESSAGE);
  21.         }
  22.  
  23.         this.personCount = personCount;
  24.     }
  25.  
  26.     public int getPersonCount() {
  27.         return personCount;
  28.     }
  29.  
  30.     public int countClinkCount() {
  31.         int clinkCount = 0;
  32.         for (int i = 0; i < personCount; i++) {
  33.             for (int j = i + 1; j < personCount; j++) {
  34.                 clinkCount++;
  35.             }
  36.         }
  37.  
  38.         return clinkCount;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement