Download Commonly used methods of Java URL classThe java.net.URL class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Reg.No:
SNS College of Technology, Coimbatore-35.
(Autonomous)
B.E/B.Tech- Internal Assessment - I
Academic Year 2016-2017 (Even)
Sixth Semester
Computer Science and Engineering
CS316 - Advanced JAVA Programming – Answer Key
Time: 11/2 Hours
B
Maximum Marks: 50
Answer ALL Questions
PART - A (5 x 1 = 5Marks)
1.
2.
_________ is used to perform all input & output operations in Java.
c) Classes
a) Streams
b) Variables
d) Methods
_________ is the output of this program if input given is ‘abcqfghqbcd’.
class Input_Output {
public static void main(String args[]) throws IOException {
char c;
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
do {
c = (char) obj.read();
System.out.print(c);
} while(c != 'q');
}}
a) abcqfgh
c) abcq
b) abc
3.
4.
d) abcqfghq
_____ class is used to read characters and strings in Java from console.
c) BufferedStreamReader
a) BufferedReader
b) StringReader
d) InputStreamReader
_________ is a protocol for breaking and sending packets to an address across a
network
c) Socket
a) TCP/IP
b) DNS
5.
d) Proxy Server
_____ of these methods is used to know host of an URL
a) host()
c) fullHost()
d) InetAddress()
b) getHost()
1
PART - B (5 x 2 = 10 Marks)
6.
Define Byte streams.
Byte streams provide a convenient means for handling input and output
of bytes. Byte streams are used for example when reading or writing binary data.
7.
Identify the use of the class “ByteArrayInputStream” in JAVA.
ByteArrayInputStream is an implementation of an input stream that uses a byte
array as the source. This class has two constructors, each of which requires a
byte array to provide the data source:
1. ByteArrayInputStream(byte array[])
2. ByteArrayInputStream(byte array[], int start, int numbytes)
Write the use of Layout Managers in Swing.
The Layout Managers are used to place the components in a predefined manner
relative to each other. The layout manager puts the components in a queue and
arranges them one after the other rather then placing the components according to the
coordinates. This helps in achieving platform independence.
Define sockets. List out various operation Performed by socket.
Sockets is used to Communicate between Server and Client.
8.
9.
10. State the use of reading data from the Server.
The minimal set of Steps needed to retrieve data from a URL Using a
URLConnection Object are:
1. Construct a URL Object
2. Invoke the URL Object’s openConnection() to retrieve a URLConnection
object for that URL.
3. Invoke the URL Connection’s getInputStream() method.
4. Read from the input Stream using the usual Stream API
The getInputStream() returns a generic InputStream,which lets we read and parse
the data that server sends
Public InputStream getInputStream()
PART – C (14+14+7=35 Marks)
11. (a) Explain in detail about the various Input And Output Stream classes in
detail.Give an example for each Stream Class.



Stream: an object that either delivers data to its destination (screen, file, etc.)
or that takes data from a source (keyboard, file, etc.) it acts as a buffer
between the data source and destination
The stream classes are divided into two class hierarchies, based on the data
type (either characters or bytes) on which they operate i.e Character Stream
and Byte Stream
Character Streams
o Reader and Writer are the abstract superclasses for character streams
in java.io.
o Reader provides the API and partial implementation for readers (
streams that read 16-bit characters ) and Writer provides the API and
partial implementation for writers ( streams that write 16-bit
characters).
2
14

Byte Streams
o To read and write 8-bit bytes, programs should use the byte streams,
descendents of InputStream and OutputStream .
o InputStream and OutputStream provide the API and partial
implementation for input streams (streams that read 8-bit bytes) and
output streams (streams that write 8-bit bytes).
o These streams are typically used to read and write binary data such as
images and sounds.
Two of the byte stream classes, ObjectInputStream and ObjectOutputStream, are used
for object serialization
(or)
(b) Describe in detail about the Swing Components and write a Simple 14
Program Using Swings.
Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes
(JFC) – an API for providing a graphical user interface (GUI) for Java programs. Swing
was developed to provide a more sophisticated set of GUI components than the earlier
3
Abstract Window Toolkit (AWT).
12. (a) Illustrate in detail about the URL Classes,Connection Classes,Inet 14
Addresses ,Http URL Connection with an Example.
Java URL
The Java URL class represents an URL. URL is an acronym for Uniform Resource
Locator. It points to a resource on the World Wide Web. For example:
http://www.javatpoint.com/java-tutorial
A URL contains many information:
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, www.javatpoint.com is the server name.
3. Port Number: It is an optional attribute. If we write
http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port number. If port number is
not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.
Commonly used methods of Java URL classThe java.net.URL class provides
many methods. The important methods of URL class are given
below.
Method
public String getProtocol()
public String getHost()
public String getPort()
Description
it returns the protocol of the URL.
it returns the host name of the URL.
it returns the Port Number of the URL.
4
public String getFile()
public URLConnection
openConnection()
it returns the file name of the URL.
it returns the instance of URLConnection i.e.
associated with this URL.
Example of Java URL class
1. //URLDemo.java
2. import java.io.*;
3. import java.net.*;
4. public class URLDemo{
5. public static void main(String[] args){
6. try{
7. URL url=new URL("http://www.javatpoint.com/java-tutorial");
8.
9. System.out.println("Protocol: "+url.getProtocol());
10. System.out.println("Host Name: "+url.getHost());
11. System.out.println("Port Number: "+url.getPort());
12. System.out.println("File Name: "+url.getFile());
13.
14. }catch(Exception e){System.out.println(e);}
15. }
16. }
Output:
Protocol: http
Host Name: www.javatpoint.com
Port Number: -1
File Name: /java-tutorial
(or)
(b) Demonstrate in detail about Multicast Sockets and JAVA Messsaging
Services.
JMS (Java Message Service)
MS (Java Message Service) is an API that provides the facility to create, send and read
messages. It provides loosely coupled, reliable and asynchronous communication.JMS is
also known as a messaging service.
Understanding Messaging:
Messaging is a technique to communicate applications or software components.
JMS is mainly used to send and receive message from one application to another.
Requirement of JMS:
Generally, user sends message to application. But, if we want to send message from one
application to another, we need to use JMS API.
Consider a scenario, one application A is running in INDIA and another application B is
5
14
running in USA. To send message from A application to B, we need to use JMS.
Advantage of JMS
1) Asynchronous: To receive the message, client is not required to send request.
Message will arrive automatically to the client.
2) Reliable: It provides assurance that message is delivered.
Messaging Domains
There are two types of messaging domains in JMS.
1. Point-to-Point Messaging Domain
2. Publisher/Subscriber Messaging Domain
1) Point-to-Point (PTP) Messaging Domain
In PTP model, one message is delivered to one receiver only. Here, Queue is used as a
message oriented middleware (MOM).
The Queue is responsible to hold the message until receiver is ready.
In PTP model, there is no timing dependency between sender and receiver.
13. (a) Outline the mechanism of Byte Code Interpretation in detail.
Bytecode Interpretation
Java bytecode is the form of instructions that the Java Virtual Machine executes. Each
bytecode opcode is one byte in length, although some require parameters, resulting in
some multi-byte instructions. Not all of the possible 256 opcodes are used. 51 are
reserved for future use. To understand the details of the bytecode. As each byte has 256
potential values, there are 256 possible opcodes. Of these, 0x00 through 0xca, 0xfe, and
0xff are assigned values. 0xca is reserved as a breakpoint instruction for debuggers and is
not used by the language. Similarly, 0xfe and 0xff are not used by the language, and are
reserved for internal use by the virtual machine.
Instructions fall into a number of broad groups:
 Load and store (e.g. aload_0,istore) 
 Arithmetic and logic (e.g. ladd,fcmpl)
 
Type conversion (e.g. i2b,d2i) 
 Object creation and manipulation (new,putfield)
 
Operand stack management (e.g. swap,dup2) 

Control transfer (e.g. ifeq,goto) 
 Method invocation and return (e.g. invokespecial,areturn) 
Computational Model
The model of computation of Java bytecode is that of a stack-oriented
programming language. For example, assembly code for an x86 processor
6
7
might look like this:
add eax,
edx mov
ecx, eax
This code would add two values and move the result to a different location
0 iload_1
1 iload_2
2 iadd
3 istore_3
The Execution Engine decodes the bytecode and performs the actions necessary to
implement each instruction. Its interface is quite simple, consisting of a single function
that takes a Context as an argument, and executes the method whose frame is on top
of the Context's call stack.
(or)
(b) Distinguish between UDP packet and TCP packet.
7
**********************
Prepared by
Verified by
7
HoD/DEAN