Code
String prefixString = "http://chart.apis.google.com/chart?cht=p3&chd=t:"; String sizeString = "&chs=300x100&chl="; String firstName = "Lauren"; String lastName = "Silverman"; String middleName = "Rose"; int nCharsInFirstName = firstName.length(); int nCharsInLastName = lastName.length(); int nCharsInMiddleName = middleName.length(); String finalURL = prefixString + nCharsInFirstName + "," + nCharsInMiddleName + "," + nCharsInLastName + sizeString + firstName + "|" + middleName + "|" + lastName; println(finalURL);
0406 - String Assignment: Automatic String Assembly
Statement:This assignment is easy but has very specific instructions which introduce a little bit of new syntax. Please read carefully.
The Google Chart API is an interesting web service that automatically generates a variety of chart graphics from simple URLs. For example, copy the following URLs into a browser window, and notice how they automatically produce pie-charts:
- http://chart.apis.google.com/chart?cht=p3&chd=t:6,10&chs=300x100&chl=George|Washington
- http://chart.apis.google.com/chart?cht=p3&chd=t:8,6,10&chs=300x100&chl=Carnegie|Mellon|University
- The URL begins with a fixed "prefix" which is always the same:
http://chart.apis.google.com/chart?cht=p3&chd=t: - The prefix is then followed by a series of integers separated by commas. These numbers represent the proportions of each of the elements of the pie chart.
- Next comes some text which defines the size of the requested image:
&chs=300x100&chl= - Finally, the URL ends with a series of name-labels. These labels are used to label the different sections of the pie-chart, and they correspond respectively to the integers described above. In the URL, these text labels are separated by the "pipe" (vertical bar) character: |
Here's the assignment: For this assignment, you will automatically generate a pie-chart image using the Google Chart API, which represents the relative proportions of your given and family names. For example, suppose your name was George Washington. Your first name is 6 letters long, and your family name is 10 letters long. Then you would generate the URL for a pie chart in which the "George" occupied 6/16ths of the pie, while the "Washington" occupied 10/16ths of the pie. Here are the requirements:
- Create a String called prefixString which contains the first part of the URL, as described in Item #1 above. Create another String called sizeString which contains the part that specifies the size of the desired image, as described in Item #3 above.
- Create Strings called firstName and lastName which contain the components of your name. If you like, you can also create Strings to store your middle name(s).
- You are asked to automatically calculate the number of characters in each of your names. (You will lose points if you simply hard-code "magic" numbers representing this information.) In order to determine the number of characters in a String, you will use the String.length() method. This function returns an integer representing the number of characters in a given String. Note that the syntax for this method uses the "dot" operator, which we have not talked about before, and which may therefore appear unfamiliar to you. Test out the following code on one of your own Strings to make sure you understand how to use it:
int nCharsInMyString = myString.length();
- Your final URL should be constructed by adding together the different components in the correct order:
the prefixString; the automatically-calculated lengths of your first and last names (separated by commas); the sizeString; and your first and last names (separated by pipe characters). - The actual deliverable for this assignment will not be an applet. Instead, you will upload the .PNG image which is produced by the Google Chart API. (You can drag this image right out of the browser to your Desktop.) In the courseware, make sure you upload this PNG image as the "Medium", and make sure to select "Image" as the Medium Type (instead of "Java").
- Don't forget to include your code, too, which demonstrates how you assembled the URL.
string hide statement