Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: I need ur help :|

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need ur help :|

    hi all ..

    plzzzz...I am novice in Java and I have some queries about some programs that my teacher give them to us.. so I need ur help ..

    Program 1:

    // prints the following:
        //
        // Missy: Meowww!
        // Garfield: Meowww!
        // Lassie: Arf! Arf!
    //
     
     
    interface Animal
    {
        String getName();
        String talk();
    }
     
    abstract class AnimalBase implements Animal
    {
    private final String name;
     
    protected AnimalBase(String name)
        {
            this.name = name;
        }
     
    public String getName()
        {
    return name;
        }
    }
     
    class Cat extends AnimalBase
    {
    public Cat(String name)
        {
    super(name);
        }
     
    public String talk()
        {
    return "Meowww!";
        }
    }
     
    class Dog extends AnimalBase
    {
    public Dog(String name)
        {
    super(name);
        }
     
    public String talk()
        {
    return "Arf! Arf!";
        }
    }
     
    public class TestAnimals
    {
     
    public static void main(String[] args)
        {
    Animal[] animals = 
            {
    new Cat("Missy"),
    new Cat("Mr. Mistoffelees"),
    new Dog("Lassie")
            };
     
    for (Animal a : animals)
            {
    System.out.println(a.getName() + ": " + a.talk());
            }
        }
    }



    private final String name;
    here..why she use " final "?

    protected AnimalBase(String name)
        {
            this.name = name;
        }

    What is the benefit of " this. " ?

    and also why she use "protected" and what is the benefit of it ?

    for (Animal a : animals)

    why she use this "for" and what is the benefit of it ?

    thanx a lot ..and plz I want the answers at nearest time

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by Noni View Post
    thanx a lot ..and plz I want the answers at nearest time
    I was going to type up explanations to your questions, but it would probably take too long for you, so I won't.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need ur help :|

    Not a problem , I want answers today or tomorrow , so help me if u can plz

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by Noni View Post
    Not a problem , I want answers today or tomorrow , so help me if u can plz
    I was being facetious. You should read the link in my signature on asking smart questions. Basically, if you want people to take your question seriously, you should use meaningful post titles (we know you need help, that's why you're here, and generic post titles usually just get ignored), you should use proper grammar and spelling (plz isn't a word), and you shouldn't mention the urgency of your question or your time (otherwise people will assume you think your time is more valuable than the time of others here).

    Also, many of your questions are better answered by a quick google search. Tell us what you find, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I need ur help :|

    The following pages, quickly found using a google search, should answer most of your questions.
    Undertanding Variables
    Using the this keyword
    Controlling Member Access
    The For Statement

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by KevinWorkman View Post
    I was being facetious. You should read the link in my signature on asking smart questions. Basically, if you want people to take your question seriously, you should use meaningful post titles (we know you need help, that's why you're here, and generic post titles usually just get ignored), you should use proper grammar and spelling (plz isn't a word), and you shouldn't mention the urgency of your question or your time (otherwise people will assume you think your time is more valuable than the time of others here).

    Also, many of your questions are better answered by a quick google search. Tell us what you find, and we'll go from there.
    Yes, your talk is true. I am realy sory. thanks for your alert to me

    and I looked for my questions a lot in google but I did not find what I want exactly

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by Noni View Post
    and I looked for my questions a lot in google but I did not find what I want exactly
    What did you find? What are you still confused about? Did you take a look at the links copeg gave you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by copeg View Post
    The following pages, quickly found using a google search, should answer most of your questions.
    Undertanding Variables
    Using the this keyword
    Controlling Member Access
    The For Statement
    Thanxxxx a lot

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need ur help :|

    Quote Originally Posted by KevinWorkman View Post
    What did you find? What are you still confused about? Did you take a look at the links copeg gave you?
    yes , but I don't understand this link

    The For Statement
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    for (Animal a : animals)
    this part in my subject

    why she use this "for" and what is the benefit of it ?

    I don't understand from the link

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I need ur help :|

    The link refers to that syntax as an enhanced for loop. Again, google is your friend.

    There really isn't a "benefit" other than not needing to keep track of indexes or use them to access the Objects in the array. It's a bit of "syntactic sugar" that gives people a shortcut to using indexes or an Iterator (although, the enhanced for is using an Iterator under the hood).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I need ur help :|

    If you're unsure after reading the link about Java for loops, do a google search on "for each loops".

  12. #12
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need ur help :|

    the "for" loop works like this:
    for(at what point loop starts(u have to define a variable beginning) use semicolion to separate it, where the loop ends (u have to define a variable (the same which first time)) |semicolion| and here u have to give a formula how that variable will reach the end point from the beginning)
    but this is very simple sample. Google this and u will find out that this is very useful when u r blanking it.

    example (try it and watch what will happen).

    for(int i=0; i <10; i++){
    System.out.println(i); }
    Last edited by Toggo; January 7th, 2011 at 09:13 PM.