Question: Objectives: More familiar with PHP Syntax Use object-oriented code to structure concepts Read from API Transform raw data into objects you can interact with Interact

Objectives:

  • More familiar with PHP Syntax
  • Use object-oriented code to structure concepts
  • Read from API
  • Transform raw data into objects you can interact with
  • Interact with a collection of objects for meaningful reporting
  • Get more familiar with PHP syntax and built-in functions
  • Use Illuminate\Support library (Collection, Arr, etc.) to powerfully interact with our data
    • Looking through the available methods may very likely make this assignment implementation a ton easier. :)
  • Organize classes into their own files

Coding Standards and Clean Code

  • Meaningful variable, function, namespace, class, method names - spell them out and don't abbreviate. (For example, $firstName instead of $fn. $StudentProfile instead of $StProf.)
  • Put classes into their own files
  • Use logical indentation for code blocks (e.g., PHP formatter)

Description:

Important links:

  • Read about data youll be consuming from https://data.wa.gov/Demographics/WAOFM-Census-Population-and-Housing-2000-and-2010/tx5i-i2ja (Links to an external site.)
  • The specific data API endpoint youll be consuming from (from your code) is here: https://data.wa.gov/resource/ktqk-i4iw.json (Links to an external site.)

Write a command-line/terminal application that reads in city census data from listed API endpoint (see above). Read data in, convert data into City objects. The only json fields to be captured and stored in an object are: place_name and total_population_2010.

Then print a report:

  • 10 highest populated cities (ordered from high to low)
  • 10 lowest populated cities (ordered from low to high)
  • A random city

Example output (city names are fake. For sake of space I don't display all 10):

Population report for 2010 ========================== 10 highest populated cities: - St. Andrews (234234) Imrryr (200000) New Yark (199343) Las Veeegus (159034) Racoon City (139023) ... 10 lowest populated cities: - Tanelorn (4) Wally Wally (367) Boring (2893) Springfield (3200) Grand Coulee (3456) ... Random City: Mountain View (56345)

Implementation:

  • Class named CensusAPI with a static function called fetchCities. The function, when called, returns parsed JSON data that is usable in your PHP code.
    • API calling code
 $client = new GuzzleHttp\Client(); $res = $client->request('GET', 'http://data.wa.gov/resource/ktqk-i4iw.json'); $result = json_decode($res->getBody()->getContents(), true);
    • You need to first install the Guzzle Http client in your project to use it.
      composer require guzzlehttp/guzzle
  • Class named: City that has 2 properties for city name and population. Also, implement a toString method that formats a string in the format: (). See example output for real values in this format.
  • Class named CityAnalyzer that is initialized/constructed with collection data returned from CensusAPI::fetchCities. This class should have methods that are responsible for:
    • Mapping the json collections data into PHP array of city objects
    • Find and print the 10 highest populated cities
    • Find and print the 10 lowest populated cities
    • Find and return a random city
      • returns a string (generated by the city, see Example output)

Youll need a program to then use these classes to fulfill requirements of the assignment.

Your main program implementation may partially look like this:

$citiesJson = CensusAPI::fetchCities(); $analyzer = new CityAnalyzer($citiesJson); ... $analyzer->printHighestPopulatedCities(); $analyzer->printLowestPopulatedCities(); echo "Random City: {$analyzer->randomCity()}"; ...

Tip: Make sure you have require "vendor/autoload.php"; at the top of your files that use guzzle and/or illuminate libs.

To Turn In

Submit to Canvas a zipped file named with your last name, followed by first initial of first name followed by cscd378hw4 (ex: BreshearsCcscd378hw4.zip). The zip file should contain all the .php files and a text file of instructions on how to set up and run the PHP code. (I expect that the grader will be running your code from within VS Code like we have been demonstrating the last few lectures, but could do so from the command line.)

Include your name and a description of the assignment/class at the top of all your .php files in comments. Do this for any assignment you submit in this class! You will lose points if you do not do this.

Extra Credit

Add the total_population_2000 json field from the input API to your classes (as needed). Print the highest and lowest 10 cities sorted by the percentage of change between 2000 and 2010, as well as a random city. For each city printed include the name, the percentage change (to 1 rounded decimal place), the 2000 and 2010 populations. Include column labels and line up data values neatly. For example:

Top 10 Cities by Population Change ================================== Name Pct Change 2000 pop 2010 pop ------------------- ---------- ---------- ---------- Imrryr 100.0% 1000000 2000000 Boring 45.6% 1987 2893

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!