Posts

Matrix

Reading and Printing Matrix import java.util.Scanner; public class MatrixInput {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         // Reading number of rows and columns         System.out.println("Enter the number of rows:");           int rows = scanner.nextInt();           System.out.println("Enter the number of columns:");          int columns = scanner.nextInt();         // Initializing the matrix         int[][] matrix = new int[rows][columns];         // Reading the matrix elements         System.out.println("Enter the elements of the matrix:");         for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {                 matrix[i][j] = scanner.nextInt();             }         }         // Displaying the matrix         System.out.println("The matrix is:");         for (int i = 0; i < rows; i++) {             for (int j = 0; j < columns; j++) {      

Lab Exercise Java -1

1.Read a string and print the words in Sorted order. 2.Insert a string at a particular position in another string Example:Input:Model College  string to insert:Engineering pos:5 Output:Model Engineering College 3.Remove Duplicates Description: Given a string, remove the duplicate characters in it. Example: Input: "aabbcc" Output: "abc" 4.Reverse Words in a String Description: Given an input string s, reverse the order of the words. Example: Input: "the sky is blue" Output: "blue is sky the" 5.Find the largest digit in a number also print all its positions Input:3586708 largest digit is 8 occurs at position 3,8 6.Convert the given decimal number into binary Input;13 Output;1101 7.Find the mean,median and mode of set of data 8.Given two sorted arrays A and B with unique elements. create a new array C in sorted order by merging A and B A=[5,8,9,10] B=[3,6,7] C=[3,5,6,7,8,9,10] 9.A saddle point in a matrix is an element that is the smallest in its ro

Classes and objects-Employee class

Let's break down the concepts of classes, objects, and constructors in Java with an example. Classes in Java A class in Java is a blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit. A class can contain fields (variables) and methods to describe the behavior of an object. Objects in Java An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Objects are created using the new keyword. Constructors in Java A constructor in Java is a special method that is called when an object is instantiated. Its primary purpose is to initialize the newly created object.  A constructor: Has the same name as the class. Does not have a return type. Can be overloaded to accept different numbers or types of parameters. Example Let's create a simple Employee class to illustrate these concepts. class Employee {     // Member variables     private String nam

Method Overloading in Java

Method overloading in Java is a feature that allows a class to have more than one method with the same name, provided their parameter lists (also known as method signatures) are different. This is a type of polymorphism (specifically, compile-time or static polymorphism) where multiple methods can perform similar but slightly varied tasks, differentiated by the number and type of their parameters. Key Points of Method Overloading: Same Method Name : The overloaded methods must have the same name. Different Parameter List : The parameter list must differ in at least one of the following ways: Number of parameters Type of parameters Order of parameters (if the types are different) Return Type : The return type can be different, but it does not contribute to the method signature for overloading purposes. Benefits of Method Overloading: Readability : Makes the code easier to read and understand by using the same method name for similar actions. Reusability : Encourages code reuse by allowi

Inheritance - Single Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit properties and methods from another class. This mechanism promotes code reusability and establishes a relationship between the parent class (superclass) and the child class (subclass). In single inheritance, a class inherits from only one superclass. This is the most basic form of inheritance. Here's a simple Java program to demonstrate single inheritance: // Superclass class Animal {     // Method in the superclass     void eat() {         System.out.println("This animal eats food.");     } } // Subclass class Dog extends Animal {     // Method in the subclass     void bark() {         System.out.println("The dog barks.");     } } // Main class to test the inheritance public class Main {     public static void main(String[] args) {         // Create an object of the subclass         Dog myDog = new Dog();                  // Call the methods of the superclass an

Hierarchical Inheritance

Hierarchical inheritance in Java is a type of inheritance where multiple subclasses inherit from a single superclass. This type of inheritance allows a single class to serve as a base class for multiple derived classes, promoting code reuse and consistency across related classes. Example of Hierarchical Inheritance in Java Let's create an example with a superclass called Animal and three subclasses: Dog, Cat, and Bird. Each subclass will inherit from the Animal superclass and add specific behavior. // Superclass class Animal {     String name;     // Constructor     Animal(String name) {         this.name = name;     }     // Method to display the name     void display() {         System.out.println("Animal Name: " + name);     }     // Method to make a sound (to be overridden by subclasses)     void sound() {         System.out.println("Animal makes a sound");     } } // Subclass Dog class Dog extends Animal {     // Constructor     Dog(String name) {         s

Multilevel Inheritance

Multilevel Inheritance Multilevel inheritance is a type of inheritance where a class is derived from another class, which in turn is derived from another class. This forms a hierarchy of inheritance where each class inherits properties and behaviors from its superclass. Example: class Animal {     void eat() {         System.out.println("This animal eats food.");     } } class Dog extends Animal {     void bark() {         System.out.println("The dog barks.");     } } class Puppy extends Dog {     void weep() {         System.out.println("The puppy weeps.");     } } public class Main {     public static void main(String[] args) {         Puppy puppy = new Puppy();         puppy.eat();         puppy.bark();         puppy.weep();     } } Program to Try // Base class class Shape {     double length;     double width;     // Default constructor     Shape() {         length = 0;         width = 0;     }     // Parameterized constructor     Shape(double length,