diff options
| author | mo <mo.khan@gmail.com> | 2019-07-13 13:31:34 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2019-07-13 13:31:34 -0600 |
| commit | e397927ab6cad22c94baff80486dfa98d4f5ddfe (patch) | |
| tree | a89d4417aa58b84446c2ea30dee1d0b86223fa60 /src/Q5 | |
| parent | feb61ff33d63c753613ae540106d56bd379f877c (diff) | |
complete question 5
Diffstat (limited to 'src/Q5')
| -rw-r--r-- | src/Q5/Citizen.java | 27 | ||||
| -rw-r--r-- | src/Q5/ComputeIntellect.java | 43 | ||||
| -rw-r--r-- | src/Q5/README.md | 26 | ||||
| -rw-r--r-- | src/Q5/Village.java | 49 |
4 files changed, 114 insertions, 31 deletions
diff --git a/src/Q5/Citizen.java b/src/Q5/Citizen.java index b10e6a9..748a848 100644 --- a/src/Q5/Citizen.java +++ b/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; + } } diff --git a/src/Q5/ComputeIntellect.java b/src/Q5/ComputeIntellect.java index f195783..d8a7ee7 100644 --- a/src/Q5/ComputeIntellect.java +++ b/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; + } + } + } } diff --git a/src/Q5/README.md b/src/Q5/README.md index 452fe19..2994e5a 100644 --- a/src/Q5/README.md +++ b/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: diff --git a/src/Q5/Village.java b/src/Q5/Village.java index 897ed4a..4d82c8b 100644 --- a/src/Q5/Village.java +++ b/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())); + } } |
