Posts

Java networking Examples: Client and server implementation in TCP

 TCP (Transmission Control Protocol) is a transport layer protocol which deals with end to end transmission. It makes a reliable connection. Let us create a client server program using TCP. Program implementation: Server: The implementation starts with including the built in packages. The steps are given below. A public class is created with main() function. A ServerSocket object is created with the port 12345. Clientsocket object is derived from Socket. The object calls the accept() function to accept the clientsocket. ‘getInetAddress()’ is used to get the internet Address. ‘getOutputStream()’ is used to get the output stream from the socket. ‘PrintWriter’ object writer1 gets the outputStream as a input. Print the output as response from server. Close() the socket. Server Program: import java.net.*; import java.io.*; public class TCPServerEg {     public static void main(String[] args) {         ...

Java Networking Examples: Simple Client Server application

               A set of computers and other communication devices connected together to share data, communication and services. Clients and server are two important keywords in networking. Client -It is the one which requests the service. Server – It responds the clients according to its request. Let us create a simple client and server communication using sockets. Program implementation: Client implementation:               The client program implementation starts from including the built-in packages. Other steps are as follows. ·        Create a class with main() function. ·        An object is created for socket in the localhost in port 12345. ·        For input object creation, use BufferedReader class. ·        PrintWriter is used to develop...

Java Reflection

       A feature which allows the program to analyse the program at run time. You can modify the structure and behaviour of the program dynamically. Features: Creating objects: A new object can be created at runtime. The object is instantiated using the constructor. Accessing Class information: It uses java.lang package to get the class information. It also gives the details about member variables, methods, constructors and modifiers. Manipulating Fields:   A class ‘Field’ is used to get and set the field values. Even you can modify the private value. Dynamic method invocation: ‘Method’ class is used to invoke the methods on objects. This handles the methods without the name also. Inspect annotations:               It analyses the classes, methods, fields and other members. This is used to build the frameworks and libraries. Create the java program to implement the java refl...

Java Stream API Example

 Do you need a java feature which process a sequence of elements? If your answer is yes…. Java stream API is used. A collection of data is involved for performing functional-style operations. Features: Some of the features in the Java Stream API are listed below… Stream: It has a pipeline which process the sequential elements. It processes from source to destination. Intermediate operations: when you want to transform a stream into another stream, some of the functions are used. Eg: distinct(),filter(),limit(),map(),sorted(),skip() Terminal operations: when these operations executed, some results are produced. Eg: anyMatch(),allMatch(),collect(),count(), forEach(),reduce(). Let us implement a java program to illustrate java Stream API. Program implementation: ·        First,include the builtin packages. ·        Create a class with main() function. ·        A list c_na...

Java Program for freshers: Check if a given number is a palindrome or not

What is palindrome?               A number or a phrase that is same when you read from backward or forward. It ignores spaces, capital letters and punctuation. Eg:12321, level. Logic: ·        Read the input from the user. ·        First, reverse the digits of the given number. ·        Check the original number and reverse number. If both are equal,then the number is palindrome. ·        Otherwise, it is not a palindrome number. Program implementation: To implement the concept, follow the steps given below. ·        First, include the java package java.util.Scanner. ·        Create a class LetusCheckPalindrome with main() function. ·        An object is created for Scanner. Using the object,call ...

Java Programs for freshers: Find second largest number in the array

               In this series of programming, let us create a java program to find the largest number in the array. Logic: Get the number of elements in the array. Read the array elements from the user. Find the largest value and the minimum value in the array. Create a for loop for checking all elements. Now, check the element is greater than largest value, then assign the element as largest value. Else, if the element is greater than second largest and not equal to largest, assign it as second largest. If the second largest is equal to minimum value, then print there is no second largest number. Otherwise, print the second largest number. Program implementation: Include the built in package. Create a public class with main() function. Using scanner object,read the number of elements and elements from the user at run time. Use the above logic to implement the program. Program: import java.util.Scanner; public cla...

Java Program for freshers: To reverse a String

               String is a collection of characters. String operations includes concatenation, counting the number of characters, deletion of a character and so on. let us create a reversal of string program as follows. Logic: ·        Read the input from the user. ·        Create an empty string. ·        Using a for loop, repeat the process. ·        Assign the ‘i’ value to string length-1. Until i>0 , split the character at location ‘i’. ·        Add to the empty string. ·        Finally, print the value. Program implementation: ·        Import the built in module java.util.Scanner. ·        Create a public class with main() function. ·        An object is cr...