Skip to main content

Posts

Showing posts with the label Lab Experiments

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();   ...

Experiment No: 1 - Study of Java Runtime Environment (JRE).

  Experiment No: 1   Aim: Study   of Java run time environment (JRE) Theory: Java is a programming language and a platform. Platform Any hardware or software environment in which a program runs, known as a platform. Since Java has its own Runtime Environment (JRE) and API, it is called platform. The history of Java is a fascinating journey that began in the early 1990s and continues to shape the modern software development landscape. Here's an overview of the key milestones and events in the history of Java: Origins (Early 1990s): Java's story begins with a team of engineers at Sun Microsystems, led by James Gosling, Mike Sheridan, and Patrick Naughton. Their goal was to create a programming language that could be used to develop software for consumer electronic devices, such as set-top boxes and interactive television. Green Project (1991): The project, initially known as the "Green Project," aimed to develop a language that could addr...

Java Code for Matrix Operations (Addition, Subtraction & Multiplication)

  Copy import java.util.Scanner; public class MatrixOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input matrix A System.out.println("Enter the number of rows and columns of matrix A:"); int rowsA = scanner.nextInt(); int colsA = scanner.nextInt(); int[][] matrixA = new int[rowsA][colsA]; System.out.println("Enter the elements of matrix A:"); for (int i = 0; i

Second Largest number in the Array

  Copy import java.util.Scanner; public class SecondLargestInArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] arr = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println("There is no second largest element."); } else { System.out.println("The second largest element in the array is: " + secondLargest); } scanner.close(); } }

Basic Java Programs

  Copy public class BasicJavaProgram { public static void main(String[] args) { // Variables int num1 = 10; int num2 = 5; // Arithmetic Operators int sum = num1 + num2; int difference = num1 - num2; int product = num1 * num2; int quotient = num1 / num2; int remainder = num1 % num2; // Comparison Operators boolean isEqual = num1 == num2; boolean isNotEqual = num1 != num2; boolean isGreater = num1 > num2; boolean isLess = num1 = num2; boolean isLessOrEqual = num1 0) && (num2 0) || (num2 num2) { System.out.println("num1 is greater than num2"); } else if (num1

Basic Java program for implementation operators, variables, control flow statements

 public class BasicJavaProgram {     public static void main(String[] args) {         // Variables         int num1 = 10;         int num2 = 5;         // Arithmetic Operators         int sum = num1 + num2;         int difference = num1 - num2;         int product = num1 * num2;         int quotient = num1 / num2;         int remainder = num1 % num2;         // Comparison Operators         boolean isEqual = num1 == num2;         boolean isNotEqual = num1 != num2;         boolean isGreater = num1 > num2;         boolean isLess = num1 < num2;         boolean isGreaterOrEqual = num1 >= num2;         boolean isLessOrEqual = num1 <= num2;   ...

The concepts of Class, Method, Attribute and object in Java:

The analogy of a baker and a cake to explain the concepts of Class, Method, and Attribute in Java: 1. Class:    - Explanation : A class is like a recipe book. It contains the instructions (methods) and ingredients (attributes) needed to create something.     - Analogy: Imagine a baker's recipe book. Each recipe in the book represents a class. Each recipe contains a list of ingredients (attributes) and instructions on how to combine them (methods) to create a specific dish (object).    - Example:      public class Cake {          // Attributes          private String flavor;          private int layers;          // Constructor          public Cake(String flavor, int layers) {              this.flavor = flavor;              this.layers = layers;  ...

Reading character arrays and integer arrays in Java

To read character arrays and integer arrays in Java, you can use the `Scanner` class or the `BufferedReader` class. Here's how to do it using each approach: 1. Using Scanner class: import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         // Reading a character array         System.out.print("Enter a string: ");         String str = scanner.nextLine();         char[] charArray = str.toCharArray();         // Reading an integer array         System.out.print("Enter the size of the integer array: ");         int size = scanner.nextInt();         int[] intArray = new int[size];         System.out.println("Enter " + size + " integers separated by spaces:");         for (int i...

An example of adding two numbers in Java using the `Scanner` class

An example of adding two numbers in Java using the `Scanner` class, along with an analogy for each keyword: import java.util.Scanner; // Importing Scanner class public class Addition {     public static void main(String[] args) {         // Creating a Scanner object named 'scanner' to read input from the console         Scanner scanner = new Scanner(System.in);                  // Prompting the user to enter the first number         System.out.print("Enter the first number: ");                  // Using the 'nextInt()' method of the Scanner class to read an integer input from the user         int num1 = scanner.nextInt();                  // Prompting the user to enter the second number         System.out.print("Enter the second number: "); ...

Procedure to Run the program with different class name and file name in java.

If your Java file is named `hw.java` and the public class inside it is named `HelloWorld`, you can still compile and run it. Here's how you can do it: 1. Compile the Java File:    javac hw.java    This will generate a `HelloWorld.class` file. 2. Run the Program:    To run the compiled Java program, use the `java` command followed by the public class name `HelloWorld`:    java HelloWorld This approach works because Java uses the public class name to determine the entry point for execution, not the filename. So even though the filename (`hw.java`) doesn't match the public class name (`HelloWorld`), you can still run the program using the public class name. OR   you can directly run the program with the following command                 java hw.java  It will compile and run the program in one hit. 

Hello World program in Java along with an explanation of each keyword

  Here's a "Hello World" program in Java along with an explanation of each keyword: public class HelloWorld {     public static void main(String[] args) {         System.out.println("Hello, world!");     } } Let's break down the code: public:  This is an access modifier that specifies that the class HelloWorld is accessible by any other class. In analogy, think of it as a door with a sign saying "Open to everyone". Anyone can access the class from anywhere in the program. class:  This keyword is used to declare a class in Java. In our example, we have a class named HelloWorld. Classes in Java are like blueprints or templates for objects. You can think of a class as a recipe for baking a cake, and objects as the actual cakes created from that recipe. HelloWorld:  This is the name of our class. It follows the rules for naming identifiers in Java. It's the convention to start class names with an uppercase letter and use camel case...

The Hello World Program with Detailed Explanation of Java Program

The "Hello World" program is a simple program that prints the message "Hello, World!" to the console. Let's go through the program and explain each keyword in detail: public class HelloWorld {     // 'public' is an access modifier. It indicates that the class is accessible from anywhere.       public static void main(String[] args) {         // 'public', 'static', and 'void' are modifiers for the main method.         // 'public' makes the method accessible from anywhere.         // 'static' indicates that the method belongs to the class and not to an instance of the class.         // 'void' means that the method does not return any value.         System.out.println("Hello, World!");         // 'System' is a class in the java.lang package.         // 'out' is a static member of the System class, representing the standard ou...

Study of Java Runtime Environment (JRE).

The Java Runtime Environment (JRE) is a crucial component of the Java platform, providing the necessary runtime support for executing Java applications. Here's an overview of the key aspects and components of the Java Runtime Environment: Components of Java Runtime Environment (JRE): 1. Java Virtual Machine (JVM):    - Definition:  The JVM is a virtualized execution engine that interprets or compiles Java bytecode into native machine code for the host system.    - Responsibilities:      - Execution of Java applications and applets.      - Memory management, including garbage collection.      - Handling exceptions and providing a secure execution environment. 2. Java Class Libraries:    - Definition:  A comprehensive set of pre-compiled classes and methods that provide fundamental functionalities to Java applications.    - Responsibilities:      - Includes core libraries (e.g., j...