Wednesday, November 10, 2021

Java: Data Analysis

Data Analysis (Sharks)

Computer Science assignment, 2017.   You can find the project code by clicking the Github icon. ->GitHub



Introduction


Given a text file with data representing various features and aspects of sharks, read the file, write some functions to produce the required output and save the results in a text file. These were the requirements given for the various tasks.

Each line in a text file was represented as a shark, and each feature was separated by a colon (:). The given tasks were split into three. 

The first task asked for numeric analytics, like the largest and smallest sharks, as well as counts of words and strings. The second one played on a grouping principle, grouping all the sharks by region. Finally, the third task was to implement a search feature through the Latin name of the shark, which was one of the features in the given data. All these tasks are detailed below.

Code and Analysis


The reading in and writing to of the data was standard across all the tasks. To further simplify the analysis, a Java class, Shark, was created. This class contains a blank constructor and one that requires the following features:
  • Common Name
  • Latin Name
  • Maximum Length of Growth in cm
  • Maximum Swimming Depth in m
  • Maximum Young
  • Global Presence Indicator
  • Oceanic Regions
It also contains getter and setter methods and a toString method - all basic elements of a class.

On initial composition of the main file, a function required to open up the file and parse the shark data was created. The line is split by the colon delimiter and the sharks are added to an array.

Task 1

a) The three largest sharks by length - This required sorting the array by 'Maximum Length of Growth in cm', in descending order and printing the first three names.

b) The three smallest sharks by length - As with (a), sorting the array in ascending order would suffice. And print out the first three.

*Alternatively, run a sort in ascending or descending order, and print out the respective three required for (a) and (b).

c) The total number of letters (excluding spaces) of all Latin names -. A simple counter variable along with a for loop that runs across all 'Latin names' works. For each shark, 'strip' off the space and add the length of the string to the counter variable.

d) The total number of unique words contained within all Latin names with an even number of letters - Adding each name (or part of the name) to a set should remove all duplicates. With a for loop, count the number of letters in each word. If even, add one to a counter variable.

e) The total number of unique words contained within all Latin names with an odd number of letters - The same implementation as (d), except you count the odd, instead of the even.

** Alternatively, you can do a count of even or odd, and subtracting the count from the size of the set will give you the other result. Words in the set can only either have an odd or even number of letters.

Task 2

a) For each unique region, print the common names of the sharks - 
Start off by finding all the unique regions. You can either have these in a set (the easier way as sets don't allow duplicates), or filtered in an array (I used array as this proved useful for other parts of this task). 

As the results have to be printed in alphabetical order, sort the array.

Run a for loop (or a bunch of them). For each region, check against each shark's oceanic regions. If it exists, add the Common Name to a colon-delimited string. Split the string and sort the array in alphabetical order. Print the results for each region. 

Task 3

a) Prompt the user to enter a search string. Match the input string to Latin names of the sharks and print the common and Latin name (with search string in uppercase) - 

For each shark, check if the Common Name contains the search string (converting both to all lowercase). If it does, find the respective sub-string indexes and convert to uppercase. Add the common name and Latin name to an array (or two arrays). Sort the array alphabetically by Common name and print results.

If no results, print No Results Found.

0 comments:

Post a Comment




 
Loading...