Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2 OwlPopulation Class Next, we'll need to create an Object type to model a population of owls. For this milestone, modify the given OwlPopulation class.
2 OwlPopulation Class Next, we'll need to create an Object type to model a population of owls. For this milestone, modify the given OwlPopulation class. This class will have the following variables: private String fileName and private Owl [] data. The OwlPopulation constructor should take in a filename and call the following method. public boolean populateData() this will readin the CSV file and construct the Owl objects to put into the OwlPopulation's data attribute. In order to read in the CSV file, we will again utilize the Scanner class. The populateData method is partially filled out for you. It already reads in a file line by line. You may modify this code to use in your OwlPopulation class. Hint: A CSV file (comma-separated-value file), as its name suggests, separates each value with a comma. In this file, each line contains the (name, age, weight] attributes for an individual owl. The following methods may be useful to you in parsing the file: split(String delimiter) is called on a String. It splits the string into an array of substrings delimited by the argument passed in. Integer.parseInt(String num) and Double.ParseDouble(String num) both take in a string object and return an int or javadouble version of the String input. Milestone 2: Show OwlPopulation being constructed correctly. Print the number of owls in the population to the console. 3 Taking Statistics on an OwlPopulation Make the following methods to collect statistics on the OwlPopulation: public double average Age () calculate and return the average age of all the Owls in the population. Note: This function returns a double, while an Owl's age attribute is of type int. public Owl getYoungest() returns the youngest Owl in the Owl Population. If no owl is found, return null. public Owl getHeaviest() returns the heaviest Owl in the OwlPopulation. If no owl is found, return null. CSCI 1933 LAB 4 4. MERGING OWLPOPULATIONS public String toString() this will return a summary of the population. The summary should include the name and age of the youngest Owl, the name and age of the heaviest Owl, and the average age of the owls in the population. The format of the summary is up to you. Milestone 3: Make a main method in your OwlPopulation class and demonstrate your statistic collection working - construct Owl Population objects from the two files supplied (owlPopulation1.csv and owlPopulation2.csv) and call toString() on each. Reflection: What are the time complexities of averageAge ), getYoungest(), and getHeaviest()? 4 Merging OwlPopulations Write the method in the OwlPopulation class public void merge (Owl Population other) which takes in an OwlPopulation and merges it with the population it is called on. Take care to not include "duplicate owls" in your population. Hint: The equals() method written for the first milestone may be useful in implementing the helper containsOwl (Owl other) function. Milestone 4: In your main method, merge two Owl Populations and print the new statistics of the merged population to the console. Where do the original populations go? Where is the "merged" population stored? Reflection: Assuming that the size of the data set is n (the total number of owls in the two databases combined), try to quantify the time complexity of your merge() algorithm in terms of n. You may wish to approach this by comparing to the three sort methods given in lecture which are all n-complerity. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Owl Population { private String fileName; private Owl [] data; public void populate Data() throws FileNotFoundException { File f = new File(fileName); Scanner scanner = new Scanner (f); int numLines = 0; while (scanner.hasNextLine()) { numLines++; Strings = scanner.nextLine(); scanner.close(); //data is is allocated the exact data = new Owl [numLines]; amount of space it needs scanner = new Scanner (f); // TODO: Populate the data with owls constructed from the lines of the file given public Owl Population (String fileName) throws FileNotFoundException{ // TODO: Populate the class variables in Owl Population public double averageAge () { // TODO: implement return -1; public Owl get Youngest() { // TODO: implement return null; public Owl get Heaviest() { // TODO: implement return null; public String toString() { // TODO: implement return null; public boolean containsOwl (Owl other) { // TODO: method you can implement as a helper function to your merge method return false; public void merge (Owl Population other) { // TODO: a brief overview of what you can do to implement this method is given below: //1) determine (and store) the distinct owls in the other population. 1/2) make a new data array to hold the correct number of owls for the merged population 1/3) copy over the distinct owls from each population to the data array 1/4) set the new data array to "this" data (where is the merged population? what happens to the original populations?) public int popSize() { return data.length; public static void main(String[] args) { try { //The following should run when you are complete. Feel free to comment out as you see fit while you work. Owl Population popl = new Owl Population ("owl Populationi.csv"); System.out.println(popl); System.out.println(popl.popSize()); Owl Population pop2 = new Owl Population ("owl Population2.csv"); System.out.println(pop2); System.out.println(pop2.popSize()); popi.merge (pop2); System.out.println(popl); System.out.println(popl.popSize()); catch (FileNotFoundException f) { System.out.println("File not found."); owlPopulation Owl57 57 6.7 Owl79 79 8.9 Owl88 1.8 Owl62 62 7.2 Ow166 66 7.6 Ow177 77 8.7 Owl22 22 3.2 Owl25 25 3.5 Ow189 89 9.9 Ow165 65 7.5 Ow158 Owl36 36 4.6 Owl32 32 4.2 Ow115 15 Owl43 43 5.3 Owl68 68 7.8 Owl44 Ow123 23 Owl34 34 Owl54 54 6.4 Owl46 46 5.6 Ow121 21 3.1 Owl19 19 2.9 Owl42 42 Owl18 18 2.8 Ow116 16 2.6 Owl88 88 9.8 Ow148 48 5.8 Owl53 53 6.3 Owl24 24 Ow186 86 9.6 Ow156 56 6.6 Owl82 82 9.2 Owl26 26 3.6 Owl17 17 2.7 owlPopulation2 Vanessa Who 29 46.36 | 7 63.54 41 94.04 Goldy Owl34 Owls Phoebe Ow162 Dan Dasher Rachel Nate 25 58.99 7.2 44 26.14 41.42 10 18.84 49 87.94 9.47 50.96 15 23.12 23 3.3 89 9.9 Rufus Aragog Nagini Owl23 Owl89 Ross Donner Owl57 Owl42 Rudolph 79.71 Dancer Owl54 Owl15 Crookshanks Lily 14.74 22 39.47 Owl32 Jenny 30.38 23.15 Comet Chandler 4.09 Ow165 Pidwidgeon Buckbeak Owl 65 7.5 | 8 24.43 7 96.87 38 7.31 50 26.14 66 7.6 62.43 70.22 Joey Owl66 Chuck Vixen Owl58 Owl36 36 4.6 Owl46 Cupid Scabbers Owl68 12 12.15 52.93 68 7.8 2 OwlPopulation Class Next, we'll need to create an Object type to model a population of owls. For this milestone, modify the given OwlPopulation class. This class will have the following variables: private String fileName and private Owl [] data. The OwlPopulation constructor should take in a filename and call the following method. public boolean populateData() this will readin the CSV file and construct the Owl objects to put into the OwlPopulation's data attribute. In order to read in the CSV file, we will again utilize the Scanner class. The populateData method is partially filled out for you. It already reads in a file line by line. You may modify this code to use in your OwlPopulation class. Hint: A CSV file (comma-separated-value file), as its name suggests, separates each value with a comma. In this file, each line contains the (name, age, weight] attributes for an individual owl. The following methods may be useful to you in parsing the file: split(String delimiter) is called on a String. It splits the string into an array of substrings delimited by the argument passed in. Integer.parseInt(String num) and Double.ParseDouble(String num) both take in a string object and return an int or javadouble version of the String input. Milestone 2: Show OwlPopulation being constructed correctly. Print the number of owls in the population to the console. 3 Taking Statistics on an OwlPopulation Make the following methods to collect statistics on the OwlPopulation: public double average Age () calculate and return the average age of all the Owls in the population. Note: This function returns a double, while an Owl's age attribute is of type int. public Owl getYoungest() returns the youngest Owl in the Owl Population. If no owl is found, return null. public Owl getHeaviest() returns the heaviest Owl in the OwlPopulation. If no owl is found, return null. CSCI 1933 LAB 4 4. MERGING OWLPOPULATIONS public String toString() this will return a summary of the population. The summary should include the name and age of the youngest Owl, the name and age of the heaviest Owl, and the average age of the owls in the population. The format of the summary is up to you. Milestone 3: Make a main method in your OwlPopulation class and demonstrate your statistic collection working - construct Owl Population objects from the two files supplied (owlPopulation1.csv and owlPopulation2.csv) and call toString() on each. Reflection: What are the time complexities of averageAge ), getYoungest(), and getHeaviest()? 4 Merging OwlPopulations Write the method in the OwlPopulation class public void merge (Owl Population other) which takes in an OwlPopulation and merges it with the population it is called on. Take care to not include "duplicate owls" in your population. Hint: The equals() method written for the first milestone may be useful in implementing the helper containsOwl (Owl other) function. Milestone 4: In your main method, merge two Owl Populations and print the new statistics of the merged population to the console. Where do the original populations go? Where is the "merged" population stored? Reflection: Assuming that the size of the data set is n (the total number of owls in the two databases combined), try to quantify the time complexity of your merge() algorithm in terms of n. You may wish to approach this by comparing to the three sort methods given in lecture which are all n-complerity. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Owl Population { private String fileName; private Owl [] data; public void populate Data() throws FileNotFoundException { File f = new File(fileName); Scanner scanner = new Scanner (f); int numLines = 0; while (scanner.hasNextLine()) { numLines++; Strings = scanner.nextLine(); scanner.close(); //data is is allocated the exact data = new Owl [numLines]; amount of space it needs scanner = new Scanner (f); // TODO: Populate the data with owls constructed from the lines of the file given public Owl Population (String fileName) throws FileNotFoundException{ // TODO: Populate the class variables in Owl Population public double averageAge () { // TODO: implement return -1; public Owl get Youngest() { // TODO: implement return null; public Owl get Heaviest() { // TODO: implement return null; public String toString() { // TODO: implement return null; public boolean containsOwl (Owl other) { // TODO: method you can implement as a helper function to your merge method return false; public void merge (Owl Population other) { // TODO: a brief overview of what you can do to implement this method is given below: //1) determine (and store) the distinct owls in the other population. 1/2) make a new data array to hold the correct number of owls for the merged population 1/3) copy over the distinct owls from each population to the data array 1/4) set the new data array to "this" data (where is the merged population? what happens to the original populations?) public int popSize() { return data.length; public static void main(String[] args) { try { //The following should run when you are complete. Feel free to comment out as you see fit while you work. Owl Population popl = new Owl Population ("owl Populationi.csv"); System.out.println(popl); System.out.println(popl.popSize()); Owl Population pop2 = new Owl Population ("owl Population2.csv"); System.out.println(pop2); System.out.println(pop2.popSize()); popi.merge (pop2); System.out.println(popl); System.out.println(popl.popSize()); catch (FileNotFoundException f) { System.out.println("File not found."); owlPopulation Owl57 57 6.7 Owl79 79 8.9 Owl88 1.8 Owl62 62 7.2 Ow166 66 7.6 Ow177 77 8.7 Owl22 22 3.2 Owl25 25 3.5 Ow189 89 9.9 Ow165 65 7.5 Ow158 Owl36 36 4.6 Owl32 32 4.2 Ow115 15 Owl43 43 5.3 Owl68 68 7.8 Owl44 Ow123 23 Owl34 34 Owl54 54 6.4 Owl46 46 5.6 Ow121 21 3.1 Owl19 19 2.9 Owl42 42 Owl18 18 2.8 Ow116 16 2.6 Owl88 88 9.8 Ow148 48 5.8 Owl53 53 6.3 Owl24 24 Ow186 86 9.6 Ow156 56 6.6 Owl82 82 9.2 Owl26 26 3.6 Owl17 17 2.7 owlPopulation2 Vanessa Who 29 46.36 | 7 63.54 41 94.04 Goldy Owl34 Owls Phoebe Ow162 Dan Dasher Rachel Nate 25 58.99 7.2 44 26.14 41.42 10 18.84 49 87.94 9.47 50.96 15 23.12 23 3.3 89 9.9 Rufus Aragog Nagini Owl23 Owl89 Ross Donner Owl57 Owl42 Rudolph 79.71 Dancer Owl54 Owl15 Crookshanks Lily 14.74 22 39.47 Owl32 Jenny 30.38 23.15 Comet Chandler 4.09 Ow165 Pidwidgeon Buckbeak Owl 65 7.5 | 8 24.43 7 96.87 38 7.31 50 26.14 66 7.6 62.43 70.22 Joey Owl66 Chuck Vixen Owl58 Owl36 36 4.6 Owl46 Cupid Scabbers Owl68 12 12.15 52.93 68 7.8
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started