Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class uppgift2 {
- public static void main(String[] args) {
- Village by = new Village();
- System.out.println(by.countsick());
- while (by.countsick() > 0) {
- by.dayPassesAll();
- System.out.println(by.countsick());
- }
- }
- public static class Village {
- final int SIZE = 1000;
- ArrayList<Person> population = new ArrayList<>();
- Person sjukperson = new Person();
- public Village() {
- for (int i = 0; i < SIZE; i++) {
- Person personen = new Person();
- population.add(personen);
- if (personen.sick == true){
- sjukperson = personen;
- }
- }
- }
- int countsick() {
- int amount = 0;
- for (int i = 0; i < SIZE; i++) {
- if (population.get(i).sick == true) {
- amount++;
- }
- }
- return amount;
- }
- void dayPassesAll() {
- for (int i = 0; i < SIZE; i++) {
- if (population.get(i).sick == true) {
- sjukperson.dayPasses();
- population.set(i, new Person());
- }
- }
- }
- }
- public static class Person {
- boolean sick;
- final double INIT_SICK_PROB = 0.2;
- final double GET_WELL_PROB = 0.2;
- public Person() {
- if (Math.random() <= INIT_SICK_PROB) {
- sick = true;
- }
- }
- void dayPasses() {
- if (Math.random() <= GET_WELL_PROB) {
- sick = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement