Showing posts with label java interface. Show all posts
Showing posts with label java interface. Show all posts

April 21, 2020

Making Tic Tac Toe using abstract and interface in Java Full source code


What we did here is, using the OOPs concepts abstract and interface we change the face of the project
Help us to do simple modification in future so as to add new games using the same matrix.
You have to go through the previous tutorials to fully understand the concepts.


Change in Tic Tac Toe class

public class TicTacToe implements IGame

Changes in Main class


import com.cfed.matrix.tictactoe.IGame;

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */

/**
 * @author konzernites
 * @since 1.0
 *
 */
public class Main extends AbstractGame {

private static final long serialVersionUID = 8683452581122892182L;

/**
* @param args
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException {

Main mainClass = new Main();
mainClass.execute();

}

private void execute() throws IllegalAccessException {
Matrix<Character> matrix = new Matrix<>(3, 3);

System.out.println(" **** INSTRUCTIONS **** ");
System.out.println(" User has to enter row and column ");
System.out.println(" Postion starts from (0,0) to (2,2) ");

IGame game = new TicTacToe(matrix);
TicTacToe.isTwoPlayer = true;
playGame(game);

System.out.println(" ***** GAME OVER ***** ");
System.out.println(" Developed by consumerfed I T Section ");

}

}

AbstractGame.java


/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */

import com.cfed.matrix.tictactoe.IGame;

/**
 *
 */

/**
 * @author Konzernites
 *
 */
public abstract class AbstractGame {

public void playGame(IGame game) throws IllegalAccessException {
game.startGame();
}

}


IGame Interface in java


/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright (c) 1997-2017 Javabelazy and/or its affiliates. All rights reserved.
 *
 */
/**
 * 
 */
package com.cfed.matrix.tictactoe;

/**
 * @author jwalian walla bag
 *
 */
public interface IGame {
public void startGame() throws IllegalAccessException;

}





March 23, 2016

Interface in java

Java Interface with example code

      A Java interface is similar like class, where contains only unimplemented methods. The class which implements that interface should define that method. This way the java achieve polymorphism. In other words, java doesn't support multiple inheritance directly but can be achieved it indirectly through Interface concept.



package interfaces;
public class Main {

   
         
    public static void main(String[] args) {
     
          shape circleshape=new circle();
       
             circleshape.Draw();
           
             shape trish=new triangle();
             trish.Draw();
           
    }
}

 interface shape
 {
     public   String baseclass="shape";
   
     public void Draw();
   
   
 }
 class circle implements shape
 {

    public void Draw() {
        System.out.println("Drawing Circle here");
    }
 
 
   
 }

An interface is a group of methods with empty bodies ( nothing will be defined in that methods), Here shape is an Interface that contains method Draw(), which is an unimplemented method

package interfaces;

public class triangle implements shape {

@Override
public void Draw() {
// TODO Auto-generated method stub
System.out.println(" ni");

}

}

What will happens if two a class implements two interfaces having a similar method in both?

I called the function System.out.println(new InterfaceTest().hello());

we dont know which interface is working

package com.tutorial.interfaces;
/**
 *
 */

/**
 * @author belazy
 *
 */
public class InterfaceTest implements InterfaceOne, InterfaceTwo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//new InterfaceTest();
System.out.println(new InterfaceTest().hello());

}

@Override
public String hello() {
// TODO Auto-generated method stub
return "test";
}

}


+Belazy L


http://javabelazy.blogspot.in/

September 19, 2013

How to reduce coupling in java

 

How to reduce coupling in java

 Description : This examples shows how the concept of interface in java helps to reduce the coupling between classes/ decoupling the classes, Inversion of control, Removing dependency injection

uml diagram example in java- How to reduce coupling
UML Diagram

The concept is very simple. We are just removing the dependencies of class in another class. Let us check how will do this. By doing this project maintenance will be very easy since there is no dependencies. so please try this code

Copy and paste this source code :
This program shows interface example in java

EmployeeInterface.java
/**
* FileName : EmployeeInterface.java
* Author : belazy1987atgmailcomcom
* Date : September 19 1987
* Description : Interface of the project
*/
package com.nick.Interface;
/**
* @author Oscar
* Implementing Employee Interface to types of employees in java
*/
public interface EmployeeInterface {
/**
* basic salary of the employee
*/
int BASICS = 5000;
/**
* house rent allowance for an employee
*/
int HR = 3000;
/**
* Traveling allowance for an employee
*/
int TA = 2000;
/**
*
*/
int DA = 4000;
/**
* @return int
* abstract method for getting the salary
*/
public int getSalary();
/**
*
* @return String
* abstract method for finding the desingation of employee
*/
public String getDesignation();
}
HumanResourceManager.java
/**
* FileName : HumanResourceManager.java
* Author : belazy1987@gmail.com
* Date : September 29 1987
* Description : Represents the details of Human Resource Manager
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class HumanResourceManager implements EmployeeInterface {
/**
* @return string
* Defines the desingation of the employee
*/
@Override
public String getDesignation() {
String designation = ” HumanResourceManager”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr)
*/
@Override
public int getSalary() {
int salary = BASICS + HR;
return salary;
}
}
BusinessAnalyst.java
/**
* FileName : BusinessAnalyst.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : MainClass of project
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class BusinessAnalyst implements EmployeeInterface{
/**
* @return string
* defines the designation of the employee
*/
@Override
public String getDesignation() {
String designation = “BusinessAnalyst”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr + da)
*/
@Override
public int getSalary() {
int salary = BASICS + HR + DA ;
return salary;
}
}
MarketingStaff.java
/**
* FileName : MarketingStaff.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : Represent the details of marketting staff
*/
package com.nick.Interface;
/**
* @author Oscar
*
*/
public class MarketingStaff implements EmployeeInterface {
/**
* @return string
* defines the designation of the employee
*/
@Override
public String getDesignation() {
String designation = “Marketing”;
return designation;
}
/**
* @return int
* calculate the salary of the employee (basic + hr + ta)
*/
@Override
public int getSalary() {
int salary = BASICS + HR + TA;
return salary;
}
}
MainClass.java
/**
* FileName : MainClass.java
* Author : menijesh@gmail.com
* Date : September 29 1987
* Description : MainClass of project
* Main Idea of the project is to describe how
* interface works and how to reduce coupling
*/
package com.nick.Interface;
/**
* @author belazy1987@gmail.com
* Working with interface in java
*/
public class MainClass {
/**
* @param cmdArg
*/
public static void main(String[] computerScience) {
EmployeeInterface employeeInterface = null;
employeeInterface = new HumanResourceManager();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
employeeInterface = new BusinessAnalyst();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
employeeInterface = new MarketingStaff();
System.out.println(“Designation :”+employeeInterface.getDesignation() +” and Salary :”+employeeInterface.getSalary());
}
}

The Above program is an example for interface in java. Why java doesnt support multiple inheritence?

This example shows how to reduce couple or enable loose coupling through concept of interface in java. 

 



October 14, 2012

Phone Number validation in java

Regular expression phone number validation in java

This example code is for phone number validation in java using regular expression and for internet protocol address validation in java


/**
 *
 * @author belazy
 * @version validates1.0
 */
public class Validator implements ValidaterInterface {


/**
 *
 * @see deeps
 * @param
 * @return boolean
 * Description : phone number validator
 * Date : Sep 19, 2007
 * Coded by : belazy
 */
public boolean phoneNumberValidator(String phoneNumber) {
        boolean isValid = false;
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

/**
 *
 * @see deeps
 * @param
 * @return boolean
 * Description : ip v4 validator
 * Date : Sep 19, 2007
 * Coded by : belazy
 */
public boolean ipaddressV4Validator(String ip) {
        String[] parts = ip.split("\\.");
        if (parts.length < 5) {
            for (String s : parts) {
                int i = Integer.parseInt(s);
                if (i < 0 || i > 255) {
                    return false;
                }
            }
        } else {
            return false;
        }
        return true;
    }


}



To trace the caller details in your java apps you can use true caller api

True caller API Documentation 


http://belazy.blog.com/

Facebook comments