Commit e397927

mo <mo.khan@gmail.com>
2019-07-13 19:31:34
complete question 5
1 parent feb61ff
src/Q5/Citizen.java
@@ -1,30 +1,43 @@
 package Q5;
 
+import java.util.*;
+
 public class Citizen {
   private int citizenId;
   private int educationalQualification;
-  private int id;
   public static final int DOCTORATE = 4;
   public static final int HIGH_SCHOOL = 1;
   public static final int POSTGRADUATE = 3;
   public static final int UNDERGRADUATE = 2;
+  private static int id = 0;
+
+  public Citizen(int citizenId) {
+    this(citizenId, Citizen.generateEducationalQualification());
+  }
 
-  public Citizen(int id, int qualification) {
-    this.id = id;
+  public Citizen(int citizenId, int qualification) {
+    this.citizenId = citizenId;
     this.educationalQualification = qualification;
   }
 
+  public int getEducationalQualification() {
+    return this.educationalQualification;
+  }
+
   public static int generateEducationalQualification() {
-    return 0;
+    return new Random().nextInt(4) + 1;
   }
 
   public static int generateId() {
-    return 0;
+    id++;
+    return id;
   }
 
   public static String convert(int i) {
-    return "";
+    return String.valueOf(i);
   }
 
-  public static void resetId() {}
+  public static void resetId() {
+    id = 0;
+  }
 }
src/Q5/ComputeIntellect.java
@@ -4,10 +4,43 @@ import java.io.*;
 import java.util.*;
 
 public class ComputeIntellect {
-  private int doctorate;
-  private int highschool;
-  private int postgraduate;
-  private int undergraduate;
+  private int doctorate = 0;
+  private int highschool = 0;
+  private int postgraduate = 0;
+  private int undergraduate = 0;
 
-  public void distributionOfQualification(Citizen[] citizens) {}
+  public int getDoctorate() {
+    return this.doctorate;
+  }
+
+  public int getHighschool() {
+    return this.highschool;
+  }
+
+  public int getPostgraduate() {
+    return this.postgraduate;
+  }
+
+  public int getUndergraduate() {
+    return this.undergraduate;
+  }
+
+  public void distributionOfQualification(Citizen[] citizens) {
+    for (Citizen citizen : citizens) {
+      switch (citizen.getEducationalQualification()) {
+        case Citizen.DOCTORATE:
+          this.doctorate++;
+          break;
+        case Citizen.POSTGRADUATE:
+          this.postgraduate++;
+          break;
+        case Citizen.UNDERGRADUATE:
+          this.undergraduate++;
+          break;
+        case Citizen.HIGH_SCHOOL:
+          this.highschool++;
+          break;
+      }
+    }
+  }
 }
src/Q5/README.md
@@ -8,27 +8,27 @@ Student ID: 3431709
 ```text
 Create three classes: `Village`, `Citizen`, and `ComputeIntellect`.
 
-The Village class has an instance variable called `numberOfCitizens` and
-an array that holds a maximum of 100 Citizen objects.
+The `Village` class has an instance variable called `numberOfCitizens` and
+an array that holds a maximum of 100 `Citizen` objects.
 
 The Citizen class has `citizenId` and `educationalQualification` as instance variables.
 
-The ComputeIntellect class has a `distributionOfQualification()` method.
+The `ComputeIntellect` class has a `distributionOfQualification()` method.
 
 Create 100 Citizen objects using citizenId for the range [1 to 100].
 
 Randomly generate the educational qualification in the range [1 to 4], where
-1 = high school,
-2 = undergraduate,
-3 = postgraduate,
-and 4 = doctorate.
+* 1 = high school,
+* 2 = undergraduate,
+* 3 = postgraduate,
+* 4 = doctorate.
 
 Store these 100 objects in a Village object using an array – any array of your choice.
-The `distributionOfQualification()` method loops through the 100 objects and counts the
-number of citizens corresponding to each of the four educational qualifications.
+The `distributionOfQualification()` method loops through the 100 objects and counts
+the number of citizens corresponding to each of the four educational qualifications.
 ```
 
-2. Description of the Code:
-3. Errors and Warnings:
-4. Sample Input and Output:
-5. Discussion:
+1. Description of the Code:
+1. Errors and Warnings:
+1. Sample Input and Output:
+1. Discussion:
src/Q5/Village.java
@@ -1,17 +1,54 @@
 package Q5;
 
+import java.io.*;
+import java.util.*;
+
 public class Village {
-  private Citizen[] citizens;
-  // private int pointer;
+  private List<Citizen> citizens;
   private int numberOfCitizens;
 
-  public Village() {}
+  public Village() {
+    this(new ArrayList<Citizen>());
+  }
+
+  public Village(List<Citizen> citizens) {
+    this.citizens = citizens;
+  }
 
   public int getNumberOfCitizens() {
-    return this.numberOfCitizens;
+    return this.citizens.size();
+  }
+
+  public void addCitizen(int qualification) {
+    this.citizens.add(new Citizen(Citizen.generateId(), qualification));
   }
 
-  public void addCitizen(int qualification) {}
+  public void addCitizen() {
+    this.addCitizen(Citizen.generateEducationalQualification());
+  }
+
+  public Citizen[] getCitizens() {
+    return this.citizens.toArray(new Citizen[this.citizens.size()]);
+  }
 
-  public void addCitizen() {}
+  public static void main(String[] args) {
+    Scanner in = new Scanner(System.in);
+    System.out.println("Welcome to the village");
+    Village village = new Village();
+    for (int i = 0; i < 100; i++) {
+      village.addCitizen();
+    }
+
+    ComputeIntellect intellect = new ComputeIntellect();
+    intellect.distributionOfQualification(village.getCitizens());
+
+    System.out.println(String.format("The village has %d citizens", village.getNumberOfCitizens()));
+    System.out.println(String.format("%d citizens have a doctorate", intellect.getDoctorate()));
+    System.out.println(
+        String.format("%d citizens have a post graduate degree", intellect.getPostgraduate()));
+    System.out.println(
+        String.format("%d citizens have a under graduate degree", intellect.getUndergraduate()));
+    System.out.println(
+        String.format("%d citizens have a high school diploma", intellect.getHighschool()));
+  }
 }
src/App.java
@@ -26,6 +26,9 @@ public class App {
         case 4:
           Q4.RandomSumGame.main(args);
           break;
+        case 5:
+          Q5.Village.main(args);
+          break;
         default:
           System.out.println("Bye");
           System.exit(0);
Rakefile
@@ -27,6 +27,6 @@ task(doc: [:pdf]) { sh 'mvn javadoc:javadoc' }
 task publish: [:clean, :test, :doc, :repackage]
 task(:build) { sh "mvn package" }
 task(run: :build) { run_cli }
-1.upto(4) { |n| task("run#{n}": :build) { run_cli(n) } }
+1.upto(10) { |n| task("run#{n}": :build) { run_cli(n) } }
 
 task default: [:publish]