Question: Part 1 . URL Parsing ( 1 0 Marks ) Web URL s are made of many parts. Take the following url for example: https:

Part 1. URL Parsing(10 Marks)
Web URLs are made of many parts. Take the following url for example:
https://acs.uwinnipeg.ca/search?hello=world&x=50
We have the following parts:
1. Scheme: https
2. subdomain: acs
3. domain: uwinnipeg
4. top-level domain: ca
5. subdirectory (or route): search
6. query parameters: everything following the ?
The query parameters are variables in the url that follow
a ?. These parameters are values that get passed to a webpage, like we would pass variables to a Java method. The parameter name and value are always separated by a =, while different
parameters are separated by &s. For the sample url we have two parameters: hello = world,
and x =50.
make the following changes in the java file given below :
a.(3 Marks) Complete the parseParams() method. This method will accept a url (String) as
input, and the parse out all of the parameters from the url. The parameters should be
returned as a 2D array of Strings. Where the array holds the pairs or parameter names and
values. Use the split() method from the String class to answer this question.
eg: Our example url would return {{hello,world},{x,50}}
b.(1 Mark) URLs will often need to encode many special characters in the query params. (If
youve ever noticed urls with things like hello%20world, this is why.) Use the decode
method from the URLDecoder class to decode your urls in the parseParams method.
c.(1 Mark) A url should contain at most one unencoded ?. Include validation in your
parseParams() method to check that the url argument is valid.
d.(3 Marks) Complete the buildQuery() method. This method will take a 2D array of Strings
that represent query params. This array is the same format as the output of part a. Your
method will return the rebuilt query part of the url. You must use StringBuilder to rebuild
your result.
For example {{hello,world},{x,50}} would return ?hello=world&x=50
e.(2 Marks) As mentioned, urls often need to encode special characters in their query
parameters. Use the URLEncoder class to ensure that all of your values are encoded while
building the query again.
Java file:
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
public class Lab03
{
public static void main(String[] args){
// A list of various urls
String[] completeUrls ={
"https://github.com/search?q=machine+learning&type=repositories&l=Python",
"https://github.com/search?q=stringbuilder&type=discussions",
"https://github.com/search?q=date+parser+language%3AJavaScript+created%3A%3E2024-01-21&type=repositories",
"https://github.com/",
"https://www.google.com/search?q=java+docs+stringbuilder",
"https://www.uwinnipeg.ca/search-results.html?q=applied+computer+science",
"https://www.bestbuy.ca/en-ca/search?search=cool+computer+stuff&sort=highestRated",
"https://docs.oracle.com/search/?q=stringbuilder&pg=1&size=10&product=en%2Fjava%2Fjavase%2F21&category=en%2Fjava&showfirstpage=true&cType=WM178623&lang=en",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"https://www.google.com/search?q=shouldthrowexception?toomanyquestionmarks",
};
System.out.println("Parsing params...");
for(String url : completeUrls){
System.out.println(url);
// This try-catch block will stop our code from crashing if an IllegalArgumentException occurs
// We'll learn more about these try-catch blocks later in the course :)
try{
// Parse the URL to get the query parameters
String[][] params = parseParams(url);
System.out.println("\tParams: "+ Arrays.deepToString(params));
// Using the query params, rebuild the query from the URL
String rebuiltQuery = buildQuery(params);
System.out.println("\tRebuilt: "+ rebuiltQuery);
}catch(IllegalArgumentException e){
// If illegal argument exception occurs, print it out
System.out.println("\tException: "+ e);
}
System.out.print("
");
}
}
public static String[][] parseParams(String url){
// Hint ? is a special character!
// Your code goes here
}
public static String buildQuery(String[][] params){
// Your code goes here
// Use StringBuilder
}
}

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!