<< Chapter < Page Chapter >> Page >

Complete program listings

Listing 8 - The program named Java4640a.

/*File Java4640a.java Copyright 1998, R.G.Baldwin Revised 01/05/14 This program exercises four of the constructors andsix of the methods of the URL class. The program also illustrates the use of the URLEncoderclass to convert a string containing spaces and other such characters into UTF-8 format.**********************************************************/ import java.net.*;import java.io.*; class Java4640a{public static void main(String[] args){Java4640a obj = new Java4640a(); try{System.out.println( "Use simple string constructor for host URL");obj.display(new URL("http://www.austincc.edu"));System.out.println("Use simple string constructor " + "for host plus file");obj.display(new URL( "http://www.austincc.edu/baldwin"));System.out.println("Use strings for protocol, host, and file"); obj.display(new URL("http","www.austincc.edu","/baldwin"));System.out.println("Use strings for protocol " + "host, and file\n and int for port");obj.display(new URL( "http","www.austincc.edu",80,"/baldwin"));System.out.println("Construct absolute URL from " +"host URL and relative URL"); URL baseURL = new URL("http://www.austincc.edu/baldwin/hello.html"); obj.display(new URL(baseURL,"/baldwin/Index.html"));System.out.println("Now use URLEncoder to create " +"UTF-8 encoded String"); System.out.println("http://space .tilde~.plus+.com");System.out.println(URLEncoder.encode( "http://space .tilde~.plus+.com","UTF-8"));}catch(MalformedURLException | UnsupportedEncodingException e){e.printStackTrace(); }//end catch}//end main //-----------------------------------------------------//void display(URL url){//method to display parts of URLSystem.out.print(url.getProtocol() + " "); System.out.print(url.getHost() + " ");System.out.print(url.getPort() + " "); System.out.print(url.getFile() + " ");System.out.println(url.getRef());//Now display entire URL as a string. System.out.println(url.toString());System.out.println(); }//end display}//end class Java4640a
Listing 9 - The program named Java4640d.
/*File Java4640d.java Copyright 1998, R.G.Baldwin Revised 01/06/14 Illustrates connecting to a URL and reading a file fromthat URL as an input stream. Computer must be online for this program to run properly.Otherwise, it will throw an exception of type UnknownHostException.**********************************************************/ import java.net.*;import java.io.*; class Java4640d{public static void main(String[] args){String dataLine; try{//Get a URL object URL url = new URL("http://www.austincc.edu/baldwin/page1.html");//Open a connection to this URL and return an // input stream for reading from the connection.BufferedReader htmlPage = new BufferedReader(new InputStreamReader(url.openStream()));//Read and display file one line at a time. while((dataLine = htmlPage.readLine()) != null){System.out.println(dataLine); }//end while loop}//end try catch(Exception e){e.printStackTrace(); }//end catch}//end main }//end class Java4640d

-end-

Questions & Answers

1. Discuss the processes involved during exchange of fluids between intra and extracellular space.
Mustapha Reply
what are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is biology
Inenevwo
what is sexual reproductive system
James
what is biology
Opeyemi
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask