Quick Index
Example - City Hemisphere Info


In this problem we print out hemisphere info for a City.

Given:
CityCountryLatitudeLongitude
AccraGhana5.6037-0.1870

Instructions:
  • Construct a City object from the given data
  • Print out a nice display string of the city
  • Print out whether or not (true/false) the is in the northern hemisphere

Hint - There is a common method that should yield a nice display string


We first find the method menu for City. We then peruse the menu and decide on:

  • Call static method to construct
  • Call method to render nice display string
  • Call method that tells us if city is in northern hemisphere
var city;

city = City.
1
fromNameCountryLatLong( "Accra", "Ghana", 5.6037, -0.1870); println(city.
2
toString()); println("Northern Hemisphere: " + city.
3
isInNorthernHemisphere());


Example - Roofing Company Familiarization


In this problem the roofing company is simply getting familiar with the project.

Given:

LabelDimension 1Dimension 2
Gymnasium Roof400200
Auditorium Roof300310

Instructions:
  • Construct objects using the three attributes for each roof
  • Print out each roof as a nice display string

Hint - The roofs are rectangular


We first browse the method menus and choose the Rectangle menu as it applies to our problem.

We then select these off the menu (and apply to two objects):

  • Call static method to construct
  • Call method to set given label
  • Call method to render nice display string

We then repeat the steps for the second object.
let gym, auditorium;


gym = Rectangle.
1
fromWidthHeight(400, 200); gym.
2
setLabel('Gymnasium Roof');
3
println(gym.toString()); auditorium = Rectangle.fromWidthHeight(300, 310); auditorium.setLabel('Auditorium Roof'); println(auditorium.toString());


Example - Print Cities


Instructions:
  • Construct a list of City objects
  • Print out nice display string of each item in the list

Hint - You may call a method that generates the list for you (if one is available).


We first find the method menu for City and choose to call these methods:

  • Call static method to construct list of cities
  • Iterate and, for each city, call method to render nice display string
let cities = City.
1
newSampleSet1(); for (let nextCity of cities) println(nextCity.
2
toString());


Example - Distance to Meteor


In this problem we determine if we need to think about extra safety depending on the proximity of a predicted meteor landing.

Given:
Location
Description
LatitudeLongitude
NHCC45.1-93.4
Meteor Landing45.3-93.2

Instructions:
  • A meteor is expected to land near NHCC
  • A student has data structures class on campus on the day of the expected meteor
  • If the predicted landing is within 20 miles of the college, advise the student to wear a hard hat
  • Print out if a student should wear a hard hat


We first find the method menu for GeoLocation to model the given (lat, long) data. We also find the menu for ToolLib which provides a very helpful utility function.

  • Call static method to construct a GeoLocation
  • Call static method to construct a GeoLocation
  • Call static utility latLongDistFromTo to compute distance
  • Check distance vs safety threshold

Lastly, we print out the appropriate message.

//d for distance
let meteor, nhcc, d, safetyThreshold;


meteor = GeoLocation.
1
fromLatLong(45.1, -93.4); nhcc = GeoLocation.
2
fromLatLong(45.3, -93.2); d = ToolLib.
3
latLongDistFromTo(meteor, nhcc); println('Distance to Meteor (miles): ' + d); safetyThreshold = 20; if (
4
d <= safetyThreshold) println('Student should wear hard hat'); else println('Student does not need hard hat');


Conclusion


These problems were non-trivial. Lat-Long calculations are notoriously complex.

However, by searching for helpful method menus, and choosing methods to call, we avoided reinvention, and simplified our solutions.