In the New Java Class window, type lab1 as the name of the Package (do not use t
ID: 3662454 • Letter: I
Question
In the New Java Class window, type lab1 as the name of the Package (do not use the default, which normally blank). For the class name itself, type HTTP_Request in the textbox next to the word ‘Name:’.
5. In the new file, HTTP_Request.java, add the following instance variable protected String URL; inside the { }.
6. Create the following class methods: • Empty constructor which initializes the URL string variable (created above) to an empty string. • Constructor which takes a String as the initial URL. • Get/Set methods for the variable called getURL() and setURL().
7. Still in HTTP_Request.java, write a public static void main(String args[]) method instantiates two new HTTP_Request objects. One HTTP_Request object should be initialized to “www.google.ca”. The other HTTP_Request should be initialized to “www.bing.com”.
8. After creating both objects, use System.out.println() to print the value using the getURL() method for each object. The output should look like: “obj1’s URL is: www.google.com“; “obj2’s URL is: www.bing.com“;
9. Create a 3rd HTTP_Request object called temp. Use temp to swap obj1 and obj2, i.e. temp = obj1; //Now temp and obj1 refer to the same object. obj1 = obj2; // obj1 now refers to obj2. obj2 = temp; // obj2 refers to temp
10. Now print out obj1 and obj2 again using getObj() to show that obj1 and obj2 have been swapped. The output should look like: “obj1’s URL is: www.bing.com“; “obj2’s URL is: www.google.com“;
Explanation / Answer
//HTTP_Request.java
/*The java program HTTP_Request that has methods to set
* and get url's. The class has instance variable
* string URL.
* */
package lab1;
public class HTTP_Request
{
//instance variable, URL
private String URL;
//Default constructor that sets the URL to empty
public HTTP_Request()
{
URL="";
}
/*User defined constructor that sets the URL
to user set value */
public HTTP_Request(String URL)
{
//Call setURL method
setURL(URL);
}
//Set method that sets name of the url
public void setURL(String URL)
{
this.URL=URL;
}
//Get method that returns the name of the url
public String getURL()
{
return URL;
}
//main method
public static void main(String[] args)
{
//Create two instances of the class HTTP_Request
HTTP_Request obj1=new HTTP_Request("www.google.com");
HTTP_Request obj2=new HTTP_Request("www.bing.com");
//print url of obj1 and obj2
System.out.println("Obj1's URL is: "+obj1.getURL());
System.out.println("Obj2's URL is: "+obj2.getURL());
//swap obj1 and obj2 using temp object of HTTP_Request
HTTP_Request temp;
temp=obj1;
obj1=obj2;
obj2=temp;
//print new URL values of obj1 and obj2
System.out.println("Obj1's URL is: "+obj1.getURL());
System.out.println("Obj2's URL is: "+obj2.getURL());
}
}//end of the class HTTP_Request
------------------------------------------------------------------------------------------------------------------
Sample output:
Obj1's URL is: www.google.com
Obj2's URL is: www.bing.com
Obj1's URL is: www.bing.com
Obj2's URL is: www.google.com