Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, you\'ll write a Java class that has an instance variable tha

ID: 3562839 • Letter: I

Question

In this assignment, you'll write a Java class that has an instance variable that is a String reference, a constructor method, a set of overloaded methods, and a class static method.

Write a Java class called MyUrl that represents a URL (Uniform Resource Locator) with optional URL encoded parameters attached. It should have a private String instance variable representing the URL, and have the following public methods:

public MyUrl(String url) - this constructor initializes the base URL to the url parameter value. It 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 mUrl instance variable to http://www.amazon.com  Note that a constructor has no return type, not even 'void'.

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. Use Integer.toString(ivalue) to convert the integer to a string representation.

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. Use Double.toString(dvalue) to convert the double to a string representation.

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. 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

In order to determine whether to use a "?" or "&" when appending an argument, you'll either need to look at the current URL value to see if it already contains a "?", or use a counter instance variable that contains the number of arguments appended to the object's URL.

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%27Reilly

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.java file next.

Explanation / Answer

please rate - thanks

any questions ask

good I still had the old code

SAMPLE RUN


----jGRASP exec: java MyUrlDemo


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')...
publixsher
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:
URL with appended arguments is:
http://www.amazon.com?id=123&author=Jim+Campbell&publixsher=O%27Reilly


Enter URL site (or 'exit')...
www.boston.com
Url value read was: www.boston.com
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
URL with appended arguments is:
http://www.boston.com


Enter URL site (or 'exit')...
http://www.google.com
Url value read was: http://www.google.com
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
URL with appended arguments is:
http://www.google.com


Enter URL site (or 'exit')...
www.barnesandnoble.com
Url value read was: www.barnesandnoble.com
Enter URL argument name (or 'done')...
publisher
Enter type of argument value (string, integer, double)...
string
Enter a string value
Addison & Wesley
Enter URL argument name (or 'done')...
title
Enter type of argument value (string, integer, double)...
string
Enter a string value
Does e=mc2?
Enter URL argument name (or 'done')...
price
Enter type of argument value (string, integer, double)...
double
Enter a double value
19.95
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
URL with appended arguments is:
http://www.barnesandnoble.com?publisher=Addison+%26+Wesley&title=Does+e%3dmc2%3f&price=19.95


Enter URL site (or 'exit')...
http://www.amazon.com
Url value read was: http://www.amazon.com
Enter URL argument name (or 'done')...
published
Enter type of argument value (string, integer, double)...
string
Enter a string value
6/2010
Enter URL argument name (or 'done')...
title
Enter type of argument value (string, integer, double)...
string
Enter a string value
History of M & M's
Enter URL argument name (or 'done')...
SKU#
Enter type of argument value (string, integer, double)...
integer
Enter an integer value
12345
Enter URL argument name (or 'done')...
done
URL with appended arguments is:
URL with appended arguments is:
http://www.amazon.com?published=6%2f2010&title=History+of+M+%26+M%27s&SKU#=12345


Enter URL site (or 'exit')...
exit

Done - press enter key to end program
Done

----jGRASP: operation complete.

--------------------------------------------------------------------

CODE

public class MyUrl
{private String mUrl;
public MyUrl(String url)
{if(url.indexOf("http://")==-1)   //has http?
       mUrl="http://"+url;          //no add it
   else
       mUrl=url;
    }
public void addArgument(String name, String value)
{if(mUrl.indexOf("?")==-1)                       //? added yet
     mUrl=mUrl+"?"+name+"="+urlEncode(value);    //no add it
else
      mUrl=mUrl+"&"+name+"="+urlEncode(value);  
}
public void addArgument(String name, int ivalue)
{if(mUrl.indexOf("?")==-1)
     mUrl=mUrl+"?"+name+"="+urlEncode(Integer.toString(ivalue));
else
     mUrl=mUrl+"&"+name+"="+urlEncode(Integer.toString(ivalue));
}
public void addArgument(String name, double dvalue)
{if(mUrl.indexOf("?")==-1)
     mUrl=mUrl+"?"+name+"="+urlEncode(Double.toString(dvalue));
else
      mUrl=mUrl+"&"+name+"="+urlEncode(Double.toString(dvalue));  
}
public String toString()
{ return "URL with appended arguments is: "+mUrl+" ";
}
public static String urlEncode(String text)
{String encoded="";
for(int i=0;i<text.length();i++)                     //for each character
    {if(text.charAt(i)==' ')                       //blanks change to +
         encoded=encoded+"+";
     else if(keep(text.charAt(i)))            //check if character is to stay
         encoded=encoded+text.charAt(i);
     else
         encoded=encoded+"%"+Integer.toHexString(text.charAt(i));      //if not convert
     
    }
return encoded;
}
public static boolean keep(char c)                 //check which characters are to be unchanged
{if(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9'||c=='_'||c=='-'||c=='.'||c=='*')
      return true;
     else
        return false;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote