Wednesday, May 14, 2014

OOP සදහා අබ්හ්යාස 01


අ‍පි oop  ‍ගැ‍න ‍විස්‍‍ත‍ර ‍ත‍ර‍මක්‍ ‍දු‍ර‍ට ‍ක‍තා ‍ක‍රා ‍යා‍ලු‍ව‍නෙ ‍ගි‍ය post  ‍එ‍කෙන්‍ ‍ඉ‍තින්‍ ‍අ‍පි ‍අ‍ද ‍හි‍තු‍ව ‍ඒ ‍ඉ‍ග‍න‍ගත්‍ ‍දේ‍වල්‍ ‍ත‍වත්‍ ‍ම‍ත‍ක ‍හි‍ටින්‍‍න ‍
අබ්‍හ්‍‍යා‍ස ‍ටි‍කක්‍ ‍ක‍රන්‍‍න ‍මො‍ක‍ද ‍පා‍ඩම්‍ ‍ම‍ත‍ක ‍හි‍ටින්‍‍නෙ ‍ඒ‍ව ‍යො‍ද‍ව‍ල practise  ‍උ‍න‍හ‍ම ‍ත‍ම‍යි.

හො‍ද‍යි ‍දැන්‍ ‍අ‍පි ‍බ‍ල‍මු ‍මේ java   code ‍එ‍ක.
මේ codes  ‍ඔ‍බ ‍ගි‍ය ‍පා‍ඩ‍මෙන්‍ ‍ඉ‍ග‍න‍ගත්‍ ‍සන්‍‍කල්‍‍ප‍යන්‍ ‍ස‍ම‍ග ‍ස‍ස‍දා ‍ව‍ට‍හා ‍ගන්‍‍න ‍එ‍ම‍ගින්‍,
 ‍ඔ‍බේ ‍දැ‍නු‍ම ‍ත‍වත්‍ ‍වැ‍ඩි ‍ව‍න‍වා ‍නි‍සැ‍ක‍යි.

‍අ‍පිපො‍ඩි  over  1 ‍කේ match  ‍එ‍කක්‍ ‍ග‍හ‍මු‍ද  Run ‍ක‍ර‍ල ‍බ‍ලන්‍‍න‍කෝ‍.
‍ම‍ත‍ක ‍ත‍බා ‍ගන්‍‍න ‍මෙ‍ම programme  ‍දෙ‍ක‍ම ‍එ‍ක‍ම folder  ‍එ‍කක්‍ ‍තු‍ල ‍ති‍බී‍ම ‍අ‍නි‍වාර්‍ය ‍වේ. ‍ඔ‍බ compile  ‍ක‍ර‍න ‍වි‍ට main   code ‍එ‍ක ‍ප‍ම‍නක්‍ compile  ‍ක‍ර run  ‍කි‍රී‍ම ප්‍ර‍මා‍න‍වත්‍ය 

Code :- 

public class cricketTeam {
    public static String team1,team2;
    private int liveScore;

    public cricketTeam(String t1,String t2) {
        team1=t1;
        team2=t2;
        liveScore=0;
       
    }
    public void get4run(){
        liveScore=liveScore+4;
    }
    public void get6run(){
        liveScore=liveScore+6;
    }
    public void get1run(){
        liveScore=liveScore+1;
    }
    public void get2run(){
        liveScore=liveScore+2;
    }

    public int playteam1(String t1){
        System.out.println("this over play "+t1);
        System.out.println("----------------------");
        java.util.Scanner x=new java.util.Scanner(System.in);
        for(int i=1;i<7;i++){
            System.out.print("Enter "+i+" ball Score :");
            int score=x.nextInt();
            switch(score){
            case 1 : get1run(); break;
            case 2 : get2run(); break;
            case 4 : get4run(); break;
            case 6 : get6run(); break;
            case 3: case 5:
                break;
            }
           
        }
        System.out.println("----------------------");
        return liveScore;
    }
    public void clearScorebord(){
        liveScore=0;
       
    }
}


Main Code : 

public class match {

    public static void main(String[] args) {
        cricketTeam o=new cricketTeam("Sri lanka","Pakistan");
        int t1score=o.playteam1("Srilanka");
        System.out.println(t1score);
        o.clearScorebord();
        int t2score=o.playteam1("Pakistan");
        System.out.println(t2score);
       
        if(t1score>t2score)
            System.out.println("Congradjulation...."+o.team1+" win the match");
        else if(t1score==t2score)
            System.out.println("oops match tied.......");
        else
            System.out.println("Congradjulation...."+o.team2+" win the match");
       
    }

}

Out put:-

‍එ‍හෙ‍නම්‍ ‍අ‍ද‍ට ‍අ‍පි ‍ගි‍යා ‍ත‍වත්‍ code  ‍ඔ‍යා‍ල‍ට ‍ඉක්‍‍ම‍න‍ට‍ම ‍ගේන්‍‍නම්‍ post  ‍පොඩ්‍‍ඩක්‍ leate  ‍මේ ‍ද‍වස්‍ ‍ව‍ල ‍අ‍පි ‍දෙන්‍‍නම ‍පොඩ්‍‍ඩක්‍ ‍වැ‍ඩ‍ව‍ල ‍හි‍ර‍වෙ‍ල ‍ඉන්‍‍නෙ. ‍මේ‍කෙ ‍නො‍තේ‍රෙ‍න ‍දෙ‍යක්‍ ‍ති‍ය‍නම්‍ comment  ‍ක‍රන්‍‍න. .

By Manoj Priyankara

No comments:

Post a Comment