July 28, 2016

Method overriding example in java

Method overriding example in java


Description

In method overriding, the sub class and super class contains the same function with type and signature.
The function in the subclass overrides the funtion in the super class at the run time.This is a runtime polymorphism. The class must be in Is-A relation ship ie, Clerk is a emloyee , sweeper is a employee. Note: static methods cannot be overriden, because static methods are bound with class, we class static method using class name and not by the objects, so we cannot override public static void main().

Source code

/**
 *
 */
package oops;

/**
 * @author java programs
 *
 */
public class Employees {

int name;
int age;
int salary;

int getSalary(){
return 15000;
}

}





/**
 * 
 */
package oops;

/**
 * @author java programs
 *
 */
public class Clerk extends Employees {
int getSalary(){
return 20000;
}

}


/**
 * 
 */
package oops;

/**
 * @author java programs
 *
 */
public class HumanResource extends Employees {
int getSalary(){
return 35000;
}

}


package oops;

public class Sweeper extends Employees {
 int getSalary(){
return 5000;
 }
}

/**
 * 
 */
package oops;

/**
 * @author java programs
 *
 */
public class MethodOverriding {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employees employee = new Employees();
System.out.println("This is employee class : "+employee.getSalary());
employee = new Clerk();
System.out.println("This is clerk class : "+employee.getSalary());
employee = new HumanResource();
System.out.println("This is human resource class : "+employee.getSalary());
employee = new Sweeper();
System.out.println("This is sweeper class : "+employee.getSalary());
System.out.println(" Method overriding example in java");

}

}


Output



Method overriding example in java

Similar posts


http://javabelazy.blogspot.in/

July 02, 2016

Number to word convertor in java

How to convert a numeric value into English words in java



Source code


import java.util.Scanner;

/**
 * Number to word convertor
 */

/**
 * @author consfedkozhikode@gmail.com
 *
 */
public class NumberToWord {
   
    private String[] ones = {"","one","two","three","four","five","six","seven","eight","nine","ten"};
    private String[] twenties = {"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
    private String[] tens = {"","ten","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
    private String[] hundreds = {"ones","tens","hundred","thousand","thousand","lakhs","lakhs","crore","crore"};
    private String word = "";
    private String tempWord = null;
    private int lastDigit = 0;
    /**
     * @param args
     */
    public static void main(String[] args) {
        NumberToWord numberToWord = new NumberToWord();
        int number = 0;
        Scanner input = new Scanner(System.in);
        System.out.println(" Enter the number : ");
        number = input.nextInt();
        int output = numberToWord.convertToWord(number,0);
    }

    private int convertToWord(int number,int count) {
                int quotient = number/10;
                int reminder = number % 10;
                getTheWord(reminder,count);
                if(quotient<=0){
                    word = tempWord + word;
                    System.out.println(" output : "+word);
                    return 0;
                }else
                {
                    return convertToWord(quotient,count+1);
                }
    }

    private void getTheWord(int reminder,int placeValue) {
        String newWord = null;
        switch (placeValue) {
       
        case 0:
            newWord = " "+ones[reminder];
            break;
        case 1:
            if(reminder==1){
                word = word; //skip
                int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
                newWord = " "+twenties[number];
            }else{
                word = tempWord + word;
                newWord = " "+tens[reminder];
            }
            break;
        case 2:
            word = tempWord + word; // adding tens
            newWord = " "+ones[reminder]+" "+hundreds[placeValue]+" and";
            break;
        case 3:
            word = tempWord + word; //adding hundred
            newWord = " "+ones[reminder]+" "+hundreds[placeValue];
            break;
        case 4:
            if(reminder==1){
                word = word;
                int number = Integer.parseInt(String.valueOf(reminder+""+lastDigit));
                newWord = ""+twenties[number]+" "+hundreds[placeValue]+"";
            }else{
                word = tempWord + word;
                newWord = " "+tens[reminder]+"";
            }
            break;

        default:
            System.out.println(" This is default statement, it won't print ");
            break;
        }
        lastDigit = reminder;
        tempWord = newWord;
    }

}


Output

img for number to word covertor output
Number to english word in java

Description


Number to word converter java program converts numeric value into words,  currently the program will convert up to thousands. The same logic is applied for lakhs and crores so you can extend the project, only thing you have to do is to increase case statement in switch (repeating case 3 and case 4).
       In this program I pass the user inputted number to recursive function convertToWord(), A recursive function is a function that call itself. convertToWord() function will find the quotient and reminder of the number in each calls, the reminder is send getTheWord() function where a switch case will convert the number into corresponding words.

Most useful code

Java reference ( Buy a copy )

Similar posts


Finding HCF and LCM

Finding Factorial using recursion

Fibonacci series

Binary search in java

Palindrome in java

Geometric mean using java 

Prime number in java

Print triangle in java

Find missing number in java




author : +belazy


Ever tried to develop a tic toc toe game, you can try our code, multiplayer and with computer the whole source code will be available soon, stay update




http://javabelazy.blogspot.in/

Facebook comments