Posts

Showing posts from October, 2024

Strategy Design pattern in java

               A design pattern that selects the algorithm’s behavior dynamically. Basically,this is a behavioral design pattern. It follows the runtime instruction to choose the algorithm. So it is more flexible. How to implement the strategy design pattern? It includes the three major components. Strategy Interface: It is the basic for all implementation. Concrete Strategies: It implements the interface with specific functionality. Context class: this is thee one which allows the strategy to choose at runtime. A shopping Application is developed here. Strategy interface: PayBillStrategy CreditCardPay ,PayPalPay : concrete strategy Billpay() is used to pay the bill. It is overridden for two strategy. Context class: Shopping Cart setPayBillStrategy() : Select the strategy at runtime. Checkout() : used to send cart to bill payment. Shop App : It is the class with main function. It creates the objects and call the contex...

Decorator Design Pattern in java

       It is a design pattern allows the user to add a function at run time. But it doesn’t change the behavior of other objects in the same class. Basically, it is a structured design pattern. Features: ·        Reusable ·        Flexible ·        Extensible   Components of decorator pattern: ·        Component interface ·        Concrete component ·        Decorator ·        Concrete decorator   Component interface:               A foundation code which has an abstract method. This is the basic for decorator and concrete components. Concrete component:               This is the class which develo...

Observer Design Pattern in java:

               This is the third design pattern which deals with behavioral design.It has a subject and observers. Subject is the object and observers are the dependents. When a change occurs it reflects the observers. Let us write a java program to implement this design pattern. Steps: ·        Create an interface observer with a method updateIt with a string msg. ·        Write a code for subject class with observer array and four member functions. One for insertObserver, removing observer,notify the observer and display the state information. ·        Implement a concreteObserver to implement observer. ·        Override the updateIt() function. ·        Finally, create the main() function for creating objects and function calling. #Java Program to implement Observer Design Pattern ...

Design patterns in java (Part1)

       Java offers solutions for software design. These design patterns provides great solutions in efficient manner. These are easy to maintain. Java’s design patterns are listed below. 1.        Singleton pattern 2.        Factory pattern 3.        Observer pattern 4.        Decorator pattern 5.        Strategy pattern Each and every pattern is detailed explained as follows. 1.Singleton pattern:               Single instance is created with the global scope. The instance can be visible for entire project. Steps: ·        First,create a singleton class. Develop a singleton instance code as private,static and final type. ·        Just return the instance from a public member f...

JMM(Java Memory Model)

              Java is an object-oriented programming language. Any java program is compiled and executed to get the desired output. It follows an architecture called JVM(Java virtual Machine). Java code is compiled by java compiler(javac). Compiled code is executed, and it gives the byte code. During this process JMM takes the control over to the memory. It involves the threads. Thread is a lightweight process. Multithreading deals with multiple threads running at same time. Why JMM?               JMM deals with interactions between threads and memory. The memory can be classified into two types. ·        Main memory – It’s the place where execution takes place. ·        Working memory- It stores the details of variables of the thread. Features of JMM: Volatile variables: Read and write operations on thread is v...

How to implement Generic function in restricted mode?

              As the earlier post implements the generic function in java. This program restricts the data type as numbers. This uses <T extends Number> for restriction. Steps: ·        Include the built in package “java.util.Scanner”. ·        Create a class with main function. A generic function printIT() with number restriction is created. ·        The input is given by the user. The first number is integer and the second number is float. ·        If it mismatches, it throws the exception. Otherwise,it prints the number. #Program import java.util.Scanner; public class sampleGr {     public static <T extends Number> void printIt(T value) {         System.out.println("The value is: " + value);     }     public static vo...

How to implement Generics in java?

              “Generics” is a special feature in java. It creates a generalized data member, classes and members. This can be used for any datatypes. So, it gives you the compile time type safety. Eg: class gene <G> { G info; public G getInfo(){ return info; } } Where, G is a parameter. The user replaces this with any datatypes (Primitive datatypes like int,String or user defined datatype). Note: Different datatypes can be used without rewriting the class. Java program to implement generic class: Generic class: sampleGene Generic data member : T Input method: input() Output method: display() Generic Objects : intsampleGene , strsampleGene //Java Program public class sampleGene<T> {     private T t;     public void display(T t) {         this.t = t;     }     public T input() {  ...

Binary searching of array elements in java using functions:

               Binary searching is efficient one among the searching method. It finds the mid item. Using the mid item, divide the array elements into two halves. Checks each half and repeat the process for all the elements. Java program to illustrate binary searching using functions:               This program gives you way to implement binary search in java. Here are the steps. ·        Include the built in files ·        Create a public class with main function. ·        Inputs: number of elements in the array, array elements, the data is searched. ·        Logic: ·        Two variable h(high),l(low) where l=0, h =length of the array -1. ·        Check whether l is less than h ,continue the...

Linear search in java

     Searching is a way of finding an element in an array. This can be done by many ways. Basically, two methods are used. ·        Linear search ·        Binary search Linear search in java:               It is a sequential search which finds a data by looking the elements one by one until the data is found. Steps to follow to write the java program: ·        Import a built in package java.util.Scanner. ·        Create a class with main function. ·        Add the input statements to get the input. ·        First, read the number of elements   for array. Next, get the array elements. ·        Finally, get the data to be searched in the array. ·        Set ...

Java Programs to illustrate Array Operations

             Array is a collection of similar data items. Genarally,operations on array includes finding a maximum number in array, reversing the array elements, sorting and searching. This blog includes two java programs to illustrate array operations. 1.Java Program to find biggest number in the array:               This program includes the below steps. ·        Include the built in package java.util.Scanner. ·        A public class MaxElement is created with main() function. ·        Read the number of elements in an array as input. ·        Using a loop(for loop), get all the elements to array. ·        Set a variable max to first element of array. ·        Check each element with max value to find ...

Arrays in java

“A collection of similar data items that can be stored in continuous memory” – An array. It has the index starts from 0 to n. Syntax: Datatype Array_variable[ index_value]; Eg: int a[10]; Sample Java program to create and display the array elements: ·        Create a class “arrayEg” and write the main() function inside it. ·        Declare the variable a[] as integer and assign some values. ·        Using a for loop, print the value. class arrayEg {   public static void main(String args[])   {   int a[]={11,12,13,14,15,16,17,18,19,20};   System.out.println("The array elements are:");   for(int i=0;i<10;i++)   {    System.out.println(a[i]);   }   } } Compile and run the program. C:\raji\blog>javac arrayEg.java C:\raji\blog>java arrayEg The array elements are: 11 12 13 14 15 16 1...