Question: Description In this assignment, you'll write a Java class named MyUrl that contains the following: - a private String variable named url that will store
Description
In this assignment, you'll write a Java class named MyUrl that contains the following:
- a private String variable named url that will store the URL
- a constructor that takes one argument, a URL string
- a set of overloaded methods
- one static method.
MyUrl will represent a URL (Uniform Resource Locator) that has the option of additional URL encoded parameters attached.
The constructor does the following:
- initializes the url String variable using the constructor argument.
- adds the protocol prefix http:// on the front if not present in the url parameter..
For example, if the parameter is www.amazon.com, it sets the url variable to http://www.amazon.com
Note that a constructor has no return type, not even 'void'.
Additional Overloaded Methods:
public void addArgument(String name, String value)
This overloaded method adds a string argument to the URL of the form name=value. It URL encodes both the name and value parameters by calling the urlEncode() method on each.
public void addArgument(String name, int ivalue)
This overloaded method adds an int argument to the URL of the form name=value. It URL encodes the name parameter by calling urlEncode(). The value is the string representation of the ivalue parameter.
public void addArgument(String name, double dvalue)
This overloaded method adds a double argument to the URL of the form name=value. It URL encodes the name parameter by calling urlEncode(). The value is the string representation of the dvalue parameter.
public String toString()
This method returns the object's URL value (the base URL plus all arguments).
public static String urlEncode(String text)
This static method URL encodes its parameter String and returns the URL encoded value as the result. It is called by the addArgument methods to encode their name and value parameters before appending them to the object's URL. You can use your earlier project code to implement this method - instead of reading a line of text, the parameter string contains the text to be URL encoded, and instead of printing the encoded string, it is returned as the method result.
Arguments are added to the URL as name=value, where name and value are URL encoded. If any arguments are present in a URL, a '?' precedes the first name=value, with subsequent arguments separated by '&' characters. For example, a URL of www.amazon.com with a first argument name of id and value of 123 and a second argument name of author and value of Jim Campbell would look like:
http://www.amazon.com?id=123&author=Jim+Campbell Note: the main() method for testing this class is supplied for you at a link at the bottom
of this page. Do NOT write your own main() method for this homework.
Sample Output
Enter URL site (or 'exit')...
www.amazon.com
Url value read was: www.amazon.com Enter URL argument name (or 'done')... id Enter type of argument value (string, integer, double)... integer Enter an integer value 123
Enter URL argument name (or 'done')...
author
Enter type of argument value (string, integer, double)...
string
Enter a string value
Jim Campbell
Enter URL argument name (or 'done')...
publisher
Enter type of argument value (string, integer, double)...
string
Enter a string value
O'Reilly
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
http://www.amazon.com?id=123&author=Jim+Campbell&publisher=O%2 7Reilly
Enter URL site (or 'exit')...
exit
Test Data
Use the following test data, plus an example of your own. The example name and value arguments below should be entered as two separate inputs as shown in the sample output above.
base url: www.boston.com (no arguments)
base url: http://www.google.com (no arguments)
base url: www.barnesandnoble.com name: publisher value: Addison & Wesley name: title
value: Does e=mc2 ? name: price value: 19.95
base url: http://www.amazon.com name: published value: 6/2010 name: title
value: History of M & M's
name: SKU# value: 12345
Test Code
Use the attached MyUrlDemo.java source file to test your class.
Save this source as a separate MyUrlDemo.java file that contains just the supplied main() method test code. Compile your MyUrl.java file first, then this MyUrlDemo.javafile next.
-----------------------------------------------------------------------------------------
/** * MyUrlDemo - Tests the MyUrl class * * The MyUrl class must be compiled before this class can be compiled. */ import java.util.Scanner; public class MyUrlDemo { /** * main */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Loop, asking for a new URL to be entered. do { System.out.println(); System.out.println("Enter URL site (or 'exit')..."); String baseUrl = sc.nextLine(); if (baseUrl.equalsIgnoreCase("exit")) { break; } // Create a new MyUrl object and call its constructor MyUrl u = new MyUrl(baseUrl); System.out.println("Url value read was: " + baseUrl); // Loop, asking for argument/value input do { System.out.println("Enter URL argument name (or 'done')..."); String argName = sc.nextLine(); if (argName.equalsIgnoreCase("done")) { break; } System.out.println("Enter type of argument value (string, integer, double)..."); String argType = sc.nextLine(); if (argType.startsWith("s")) { System.out.println("Enter a string value"); String s = sc.nextLine(); u.addArgument(argName, s); } else if (argType.startsWith("i")) { System.out.println("Enter an integer value"); int i = sc.nextInt(); sc.nextLine(); u.addArgument(argName, i); } else if (argType.startsWith("d")) { System.out.println("Enter a double value"); double d = sc.nextDouble(); sc.nextLine(); u.addArgument(argName, d); } else { System.out.println("Unrecognized value type - must be (s)tring, (i)nteger, or (d)ouble"); continue; } } while (true); // Display the final url System.out.println("URL with appended arguments is:"); System.out.println(" " + u.toString()); } while (true); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
