Java Prashanayak

rhn123

Well-known member
  • Jul 20, 2012
    759
    37
    48
    mehemai machanla dog kiyala class ekak thiyenawa doglist kiyala thawa class ekak thiyenawa. dog class eke constructor ekak thiyenawa dog objects walata name ekakui age ekakui assign karana.dogList eke array ekak hadanwa dog type (dog[] theList = new dog[5]). mata one hadana dog objects array eke store karanna. doglist eke thiyenne method ekak dogShow(dog d) kiyala dog type object ekak parameter ekak widiyata thiyena.
    ob kiyana thawa class ekaka main method eka thiyenawa.

    ******* dog class eka***********
    public class dog
    {
    String name;
    int age;
    public dog(String n, int a)
    {

    na=n;
    ag=a;
    }

    }

    *******dogList class eka********

    public class dogList {


    private dog [] theList= new dog[5];
    int i=0;



    public void dogShow(dog d)
    {


    if(i<theList.length)
    {

    theList=d;
    System.out.println("Dog added at the index of : "+i+"ballage nama " +"ballage wayasa "); mata menna methana ewele hadana Dog object eke namai age ekai danna widiyak na. eka thamai prashne
    i++;

    }
    else
    System.out.println("Its full");


    }
    }

    ***********main method************

    public class Ob {


    public static void main(String[] args) {
    dogList li= new dogList();
    dog dg1= new dog("tommy",19);
    dog dg2= new dog("browny",12);
    dog dg3= new dog("jacky",14);
    dog dg4= new dog("packy",21);
    dog dg5= new dog("packy",21);
    dog dg6= new dog("packy",21);


    li.dogShow(dg1);
    li.dogShow(dg2);
    li.dogShow(dg3);
    li.dogShow(dg4);
    li.dogShow(dg5);
    li.dogShow(dg6);





    }
    }
    mata ballage namai wayasai dog class eken ganna baha. dog class extend karahama eke thiyena construtor ekath ekka case ekak thiyenawa. super (name,age) kiyala constructor ekak dogList eke dunnama error ekak enawa "
    Cannot reference name,age before supertype constructor has been called"

    name, age static dennath baha ethakota hama dog object ekatama ekama nama wayasai pennanne. kohomada meka karaganne.
     

    rhn123

    Well-known member
  • Jul 20, 2012
    759
    37
    48
    ammo monawada ban mae..

    mata therenne an bn karana widiya. ada dawasama okata iwarai. epa wela thiyenne. manasikawa watenawa bn. me wage deyakwath baruwa me field ekenma thawa jobuth hoyanawa. epa wela thiyenne
     

    juanis

    Member
    Sep 10, 2010
    26
    2
    0
    Use of setters and getters is one way.

    You are declaring name and age as protected. So if the dogList class and dog class are in the same java package you can just access the name and age using,

    d.name
    d.age

    However please check your dog class. Your name variable is declared as 'name', but you assign 'n' to 'na'. aren't you getting syntax erros? Same for 'age' as well.
     
    • Like
    Reactions: rhn123

    rhn123

    Well-known member
  • Jul 20, 2012
    759
    37
    48
    Use of setters and getters is one way.

    You are declaring name and age as protected. So if the dogList class and dog class are in the same java package you can just access the name and age using,

    d.name
    d.age

    However please check your dog class. Your name variable is declared as 'name', but you assign 'n' to 'na'. aren't you getting syntax erros? Same for 'age' as well.

    Thanks machan. yeah thats a mistake i have done. but i changed the code more than 10 times. i think that must be the reason. i used setters and getters and worked fine thanks for the help.
     

    rhn123

    Well-known member
  • Jul 20, 2012
    759
    37
    48
    post_old.gif

    counter dr
    user_offline.gif

    Senior Member
    reputation_pos.gif
    reputation_pos.gif
    reputation_pos.gif
    reputation_pos.gif
    reputation_pos.gif
    reputation_highpos.gif

    Posts: 1,711
    Join Date: Dec 2011



    Yesterday, 11:30 PM

    Bump

    (#10)
    post_old.gif

    Kalaa
    user_offline.gif

    Member
    reputation_pos.gif

    Posts: 54
    Join Date: Nov 2009
    Location: නල්ලමෙලේ



    Yesterday, 11:39 PM

    jump


    thanks machanla