Skip to main content

Posts

Showing posts from December, 2024

Assignment4

 Polymorphism  Write a program to demonstrate runtime polymorphism in Java. Can you implement method overloading in Java? Write a sample program. Explain why static methods cannot be overridden using a program. Scenario: Different payment methods (CreditCard, PayPal) implement the same interface for processing payments. Scenario: Using polymorphism with a list of different shapes. Abstraction Write a program to demonstrate abstraction using abstract classes. Can you achieve 100% abstraction in Java? Write a program to demonstrate. Write a program to show multiple inheritance using interfaces. Scenario: Using an abstract class for common employee behavior. Scenario: Different notification methods (Email, SMS).

Assignment 3

Assignment 3   Encapsulation TOPIC Questions 1.Write a Java program that encapsulates the Employee class with the following private attributes: id (integer) name (String) salary (double) Provide getter and setter methods for each attribute. Include the following constraints: The id must be positive. The salary must be greater than 3000. If not, set it to a default value of 3000. Write a TestEmployee class to: Create an Employee object. Set the values for the attributes using setter methods. Display the values using getter methods. 2. Bank Account Management Problem: Create a BankAccount class with the following private attributes: accountNumber (String) accountHolderName (String) balance (double) Implement: Getter and setter methods for each attribute. Validation:The accountNumber must be a 10-digit number. The balance cannot be negative. A method deposit(double amount) to add money to the account. Ensure the amount is positive. A method withdraw(double amount) to deduct money...

Inheritance in Java

  What is inheritance in oops? Inheritance in OOP =  When a class derives from another class . The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword. Inheritance is an object-oriented programming concept in which one class acquires the properties and behavior of another class. It represents a parent-child relationship between two classes. This parent-child relationship is also known as an IS-A relationship. Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object Inheritance is a technique of modelling real-world relationships, and OOP is all about real-world objects. Here's an example:  a car, a bus, and a bicycle all fall under the umbrella term "vehicle." That is, they have inherited the attributes of the vehicle class, implying that they are all u...

Encapsulation in Java

 Encapsulation in Java is a fundamental concept of object-oriented programming that combines data and methods into a single unit and restricts access to some of the object's components to ensure better control and security. Here's a simple explanation: Key Features of Encapsulation: Data Hiding : Encapsulation allows you to hide the internal details (like variables) of an object from the outside world. Controlled Access : It provides controlled access to the data through methods (getters and setters). Improved Security : Sensitive data can be kept safe by making it private and only allowing modification through specific methods. How It Works: Private Variables : Declare the instance variables of a class as private , so they are not directly accessible outside the class. Public Methods : Provide public getter and setter methods to read or update the values of these variables. Example: java Copy code public class Person { // Private variables private String name; ...

Java Assignment 2

 Java Assignment #2 1.Write a Java program to print the multiplication table of 5 (from 5 * 1 = 5 to 5 * 10 = 50) using both a for loop and a while loop. 2.Write a Java program to simulate a traffic light using a switch case statement. The program should allow the user to select one of three lights: red, yellow, or green. Based on the user's choice, display an appropriate message indicating the traffic action (e.g., "Stop", "Ready", or "Go") in the selected color. 3.Write a Java program to reverse the string "QAShastra Online Training" using the charAt() method of the String class. The program should iterate through the string and construct the reversed version character by character. 4. Write a Java method that takes an integer array as a parameter, calculates the sum of all the integers in the array, and returns the sum. 5. Write a Java method that takes a String array as a parameter. The method should: Calculate and print the length of eac...

Generative AI + Selenium Course Content

  Generative AI + Selenium  Course Content    1. Introduction to Gen AI,                     Artificial Intelligence (AI),                     Machine Learning(ML) & Deep Learning   2. Explore ChartGPT : Feature and Capabilities  3. Understand Key Terminologies      What are LLM Models      Usage of Local/ Cloud LLM Models     Prompt Engineering      Embeddings      Fine Tuning  4. Explanation Selenium Page Factory Framework  5. Selenium UI TestNG(Selenium Page Factory Framework) Testing with Gen AI  6.Selenium UI Cucumber(Selenium Page Factory Framework) Testing with Gen AI       Generating cucumber Feature, Scenarios, GIVE, WHEN THEN and Example 7.What is GitHub copilot and Some Code fixes Practices   8.Integration to IDE and usages  ...

Control Statements Java

 Control Statements Java

Java Constructor

 Java Constructor a constructor is a special method that is used to initialize objects when they are created.  It shares the same name as the class and does not have a return type, not even void Purpose: Constructors are used to set the initial state of an object by assigning values to its member variables. Automatic Invocation: Constructors are automatically called when an object of the class is created using the new keyword. Default Constructor: If you don't define any constructors in your class, Java provides a default constructor with no arguments that initializes the object's member variables to their default values. Parameterized Constructor: You can create constructors that take parameters to allow for customized object initialization. Rules for Creating Java Constructor There are following rules for defining a constructor: Constructor name must be the same as its class name. A Constructor must have no explicit return type. A Java constructor cannot be abstract, static,...

Java Methods

 Java Methods What is Java Method? A method is a block of code that performs a specific task   A method in Java is a set of instructions that can be called for execution using the method name  A Java method can take in data or parameters and return a value - both parameters and return values are optional.  Methods can be public, private or protected. Method is a set of statements to perform an operation,methods are also known as procedures or functions A Java function is a block of code designed to perform a specific task, encapsulated within a class or interface Naming a Method In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun. After the first word, the first letter of each word should be capitalized. Rules to Name a Method: While defining a method, remember that the method name must be a verb and start with a lowercase letter. If the me...

Java Assignment_01

 Java Assignment_01   Q1.Write a Java Program using different(suitable dataType) for given variable name ( age, temperature,grade) , assigned value and print value  using print statement ? Q2.Write a Java Program Swap two number without using a Third variable using Java Operator and Print values using print statement ? Q3.Write a Java Program SI = (Principal × Rate × Time) / 100 program calculates simple interest using the formula (using variables and operator) and print results  

Rules for Static Variables in Java

  Rules for Static Variables in Java : Class-Level Association : Static variables are shared among all instances of a class. They are stored in the class area of memory, not in the heap. Only one copy of a static variable exists, regardless of the number of instances of the class. java Copy code class Example { static int count = 0 ; } Declaration : Declared with the static keyword, usually outside of methods, constructors, or blocks. Typically used for constants or shared properties among all objects. java Copy code class Example { static int sharedVar; } Access : Can be accessed directly using the class name (preferred): java Copy code Example.sharedVar = 10 ; Alternatively, can be accessed via an object instance, but it's discouraged for clarity: java Copy code Example obj = new Example (); obj.sharedVar = 10 ; // Works, but not recommended Initialization : Static variables are initialized only once, at the time of class loading. Default values are provi...

Rules for Instance Variables in Java

  Rules for Instance Variables in Java : Declaration and Scope : Declared inside a class but outside of methods, constructors, or blocks . The scope of instance variables is throughout the class, except in static methods. java Copy code class Example { int instanceVar; // Instance variable } Default Values : Instance variables are automatically initialized with default values if not explicitly initialized: int , long , short , byte → 0 float , double → 0.0 char → '\u0000' boolean → false Object references → null java Copy code class Example { int num; boolean flag; String name; public void display () { System.out.println(num); // Outputs 0 System.out.println(flag); // Outputs false System.out.println(name); // Outputs null } } Access Modifiers : Can have any access modifier ( public , private , protected , or default). Use private to ensure encapsulation and provide access through getter and setter methods. jav...