Posts

Showing posts from June, 2018

QA's approach 2 Java - Creating Java Doc's in Eclipse

Image
How to Create Java Docs in Eclipse Consider the Bank Interest Rate Calculator Application & lets see how to create Java docs using Eclipse for it. Navigate to Projects Tab->Select Generate java Docs, JavaDoc Generation Window appears Provide the path of javadoc.exe using Configure under Javadoc command section Next select the project or package for which you wanted to generate Java docs., Click Finish Click on index.html

QA's approach 2 Java - Bank Interest Rate Calculator Application exhibiting RunTimePolymorphism

Image
Learn Runtime Polymorphism through Practical Example Application Bank Interest Rate Calculator Application exhibiting Runtime Polymorphism used to retrieve the Interest Rate of Bank by providing the Bank details at Runtime. Create the package structure with class names as follows Create class name with Bank package runTimePolymorphsimSimpleExmp; public class Bank { // Default interest Rate public Float getInterest() { return 6.5f; } } Create class name with Sbi extending Bank package runTimePolymorphsimSimpleExmp; public class Sbi extends Bank{ // SBI specific interest Rate public Float getInterest() { return 6.25f; } } Create class name with Hdfc extending Bank package runTimePolymorphsimSimpleExmp; public class Hdfc extends Bank{ // HDFC specific interest Rate public Float getInterest() { return 6.75f; } } Create class name with Icici extending Bank package runTimePolymorphsimSimpleExmp; public class Icici extends...

QA's approach 2 Java - Data Structures

What is a Data Structure? Its a container or a storage box which holds a collection of elements as a single unit.  Its a class grouping multiple elements into single entity. What can be done on this container? 1. New elements can be added 2. Added elements can be retrieved 3. Added elements can be deleted or replaced 4. Elements can be copied from this container to another container Some Data structures allows duplicates [Lists], allows null values [Set], stores or prints elements in ascending order, etc..

QA's approach 2 Java - Abstract Class vs Interface Theory

Abstract Class vs Interface, Differences, when to use? Why not create an object? Interface Interface is used when you want to define a contract and you don't know anything about implementation. (here it is total abstraction as you don't know anything). Interface is used in a situation, 1. When you know the contract methods but don't know anything about the implementation. 2. Your contract implementation can change in future. 3. You want to achieve dynamic polymorphism and loose coupling that is by just changing one line of code, you should be able to switch between contract implementer. 4. Since multiple inheritance is not supported in Java, you cannot inherit your class from two abstract classes. An interface is your only option in such situations. 5. If there is no default or common behavior among all the classes that are inheriting from abstract class then interface may be a better choice. Example Analogy for point 1: Redbus interface lists out all t...

QA's approach to Java - Dependency Injection

Image
Dependency injection defined with Real world analogy: Ecommerce site has dependency on Delivery service 1. Create a package DependencyInjectionExample 2. Create an interface for Courier Service with delivery method package DependencyInjectionExample; public interface CourierService { public void deliver(int Pid); } 3. Create a class for various courier services implementing delivery method of interface Courier Service DTDC service class implementing Courier Service interface package DependencyInjectionExample; public class DTDC implements CourierService{ @Override public void deliver(int Pid) { System.out.println("Product with deliverly id"+Pid+" is delivered through DTDC"); } DTDC() { } } BlueDart service class implementing Courier Service interface package DependencyInjectionExample; class BlueDart implements CourierService{ @Override public void deliver(int Pid) { // TODO Auto-generated method stub System.out.println("Product with ...

QA's approach 2 Java - Business logic vs Execution logic class

Image
Business logic vs Execution logic class Business logic classes are helper classes, which doesn't contain main method. Execution logic class is the one which contains the main method from where the actual execution begins. Example using Singleton static factory method: Create a new java file with BusinessLogic & define the business logic & save it public class BusinessLogic { static String name = null; static private BusinessLogic bl = null; private BusinessLogic(String name) { this.name = name; } //Singleton Factory Design Pattern public static BusinessLogic getObject(String name) { if (bl == null) { bl = new BusinessLogic(name); return bl; } else return bl; } } Output: Run BusinessLogic.java Use the command > Alt+Shift+X, J Create a new java file with ExecutionLogic, define the execution logic ...

QA's approach 2 Java - Java API class hierarchy

http://falkhausen.de/Java-8/java.io/index.html

QA's approach 2 Java - Abstract Class vs Interface

Image
Abstract Class vs Interface series 2: Example 1. Create package with runTimePolymorphism 2. Create interface with name as "Reviews 3. Create abstract class with name as "CompanyReviews 4. Create implementation classes with names as "GlobalDoor" & "MySurvey"  The Class hierarchy diagram will be as follows: The Package Diagram is as follows: Add the following logic to Business logic classes Reviews.java: package RunTimePolymorphism; public interface Reviews { public String getReviews(String... args); } Details.java: package RunTimePolymorphism; public class Details { int ratings = 0; Float salary = 0.0f; String pros = null; String cons = null; public int getRatings(String companyName) { switch (companyName) { case "Bally": ratings = 4; return ratings; default: ratings = 2; return ratings; } } public Float getSalary(String companyName, String designation) { switch (companyName) { case "Bally...

QA's approach 2 Java - Inheritance with Example_TBD

QA's approach 2 Java - Reflection API_TBD

https://www.javatpoint.com/java-reflection

QA's approach 2 Java - Collections Framework & Generics Introduction

Image
Why Collections & why is it called a Framework? First Question: Why Collections? Collections are used to overcome problems with variables, arrays, classes like size problem, type problem, non existence of predefined operations/methods, etc. To get a Overview of the above discussed problems, click on this link What is a Framework? Its a semi-finished re-usable application, providing some low level services, which can be customized according to our requirement.  Second Question: Why is it called a Framework? Collection API is available in util package & its called a Framework for the reason that, java programmers were not restricted to just utilize the existing classes making them final but was allowed to expand on top of the exiting classes inheriting them. Collections framework gives a clear example of when to use Interface & abstract class Classification of Collection Framework: In Collection Framework all the classes are classified into 3 cat...

QA's approach 2 Java - Arrays & ArrayLists_TBD

Arrays: Useful methods Arrays class java.util.Arrays.asList(a); java.util.Arrays.binarySearch(a, key); java.util.Arrays.fill(a, val); java.util.Arrays.fill(a, fromIndex, toIndex, val); java.util.Arrays.sort(a); java.util.Arrays.toString(a); java.util.Arrays.copyOf(original, newLength); java.util.Arrays.equals(a, a2); Basic Example to learn how to Declare, Initialize & Iterate over the elements in an array package dataStructureExmpls; public class ArrayBasicExample { public static void main(String[] args) { // Declare an array int[] number; // provide memory for an array number = new int[5]; //Array of reference type Student[] s=new Student[5]; int[ ][ ] arr = new int[3][ ]; arr[0] = new int[3]; arr[1] = new int[5]; arr[2] = new int[4]; // memory allocation for an array // int[] num=new int[10]; // Initializing the array at the time of declaration int age[] = { 10, 20, 30, 40 }; int[][] n = ...

QA's approach 2 Java - Problems & Solutions to Information storage in Java

Image
4 ways to store information in java: 1. Using Variables Variable is a container to hold only one value. When to use? When we want to store only one value. Limitations: Can store only one value if we require to store 120 values, we need 120 variables which will be placed at dis- continuous memory locations, so we have below 2 problems. Problem 1: Reading problem: Data is stored at some random location, hence requires more time for reading it. Problem2: Sending problem: If we take individual primitive variable, we cannot pass multiple values beyond the parameter we defined.                                      Cannot even return multiple values as the return type is primitive, to overcome this 2 & 3 have come into picture. 2. Using Class objects When to use?  When we store multiple, fixed no of values of  different types 3. Using a...

QA's approach 2 Java - Structure of a Java program

Image

QA's approach 2 Java - Variable in Java

Image
Variables Variable is the basic unit in a java program.It is a container to hold only one value. where in the data is stored in Temporary memory, hence it hold the values within the life cycle of that variable or onto program termination. The Java programming language defines the following kinds of variables: Instance Variables (Non-Static Fields) - 1. Objects/instances of class, store their individual states in "non-static fields", that is, fields declared without the static keyword.  2. Need to be initialized explicitly  3. Provided memory on object creation in oldgen/newGen area's in Heap memory.   4. Lifetime of the variable is upto that particular object or instance of class only. 5. Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing a...

QA's approach 2 Java- Engineerig drawing analogy of Class vs Object

Class vs Object, an Engineering drawing Anology Class is like an Engineering drawing Assume class to be a Engineering drawing of vechile with various parts & features In order to use a vehicle, we need to have an actual vehicle, so we need to Build it. Inorder top use various features of vechile we need to Build an actual Vehicle Similarly in order to use the members of class we need to build an object. Using Brakes of vehicle is similar to Using methods of the Created object. Reuse Just as a car’s engineering drawings can be reused many times to build many cars, you can reuse a class many times to build many objects. Reuse of existing classes when building new classes and programs saves time and effort. Reuse also helps you build more reliable and effective systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning. State - Behavior and identity of an object State: represents data (valu...

QA's approach 2 Java - Constructor

Constructor: Constructors have the same name as the class itself. Secondly, they do not specify a return type, not even void. Purpose and function Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object, Limitations of Setters & Getters: Assume if we have 100 instance variables, we need to have 200 setters & getters totally to instantiate all the 100 variables, i.e., one each setter & getter for one instance variable. 1 variable = 1 setter + 1 getter Solution: Setter ->problem can be overcome by Constructor Getter -> can be overcome by toString method Properties of a constructor 1. Constructor will be called automatically when the object is created. 2. Constructor name must be similar to name of the class. 3. Constructor should not return any value even void also. Because basic aim is to place the value in the object. (if we write the return type for the constructor then that construct...