Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class uppgift2 {
  5.     public static void main(String[] args) {
  6.         Village by = new Village();
  7.         System.out.println(by.countsick());
  8.         while (by.countsick() > 0) {
  9.             by.dayPassesAll();
  10.             System.out.println(by.countsick());
  11.         }
  12.     }
  13.     public static class Village {
  14.         final int SIZE = 1000;
  15.         ArrayList<Person> population = new ArrayList<>();
  16.         Person sjukperson = new Person();
  17.         public Village() {
  18.             for (int i = 0; i < SIZE; i++) {
  19.                 Person personen = new Person();
  20.                 population.add(personen);
  21.                 if (personen.sick == true){
  22.                     sjukperson = personen;
  23.                 }
  24.             }
  25.         }
  26.         int countsick() {
  27.             int amount = 0;
  28.             for (int i = 0; i < SIZE; i++) {
  29.                 if (population.get(i).sick == true) {
  30.                     amount++;
  31.                 }
  32.             }
  33.             return amount;
  34.         }
  35.         void dayPassesAll() {
  36.             for (int i = 0; i < SIZE; i++) {
  37.                 if (population.get(i).sick == true) {
  38.                     sjukperson.dayPasses();
  39.                     population.set(i, new Person());
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     public static class Person {
  45.         boolean sick;
  46.         final double INIT_SICK_PROB = 0.2;
  47.         final double GET_WELL_PROB = 0.2;
  48.         public Person() {
  49.             if (Math.random() <= INIT_SICK_PROB) {
  50.                 sick = true;
  51.             }
  52.         }
  53.         void dayPasses() {
  54.             if (Math.random() <= GET_WELL_PROB) {
  55.                 sick = false;
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement