CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: java

  1. #1
    Join Date
    Sep 1999
    Posts
    4

    java

    hello. I need some help with a little program I was trying to write. myBook does not give such good examples. It is a program that should print out a dogs name age and speak. It should also print out a fetching line which I am still having a little but of problems with.
    I appreciate all e-mail back to me and any response to this..
    thanks
    karen
    [email protected]


    public class p1{

    Public static void main(String[]args)
    {
    //what should i be writing here???
    }

    Public static void dogyears(Dog d)
    {
    //(what should i be writing here???
    //It says a static method to access a dog object and display the Dog's age in "dogyears" means human //age *7 )

    }
    class Dog

    {
    private String name;
    private int age;
    private int speed;

    Dog()
    {}

    Dog( String n, int a, int s)
    {
    name = n;
    age = a;
    speed = s;
    }

    String getName()
    {
    return name;
    }
    int getAge()
    {
    return age;
    }

    int getSpeed()
    {
    return speed;
    }

    void showDog()
    {
    System.out.println( getName()
    + " is "
    + getAge()
    + " years old and runs "
    + getSpeed()
    + " feet per second.");
    }

    void fetch( int feet)
    {
    System.out.println( getName()
    + " retrieved a ball thrown "
    + feet
    + " feet in "
    + 2 * ( feet / getSpeed() )
    + " seconds.");
    }

    void speak()
    {
    String [] msg = { "Woff Woff", "Ruff Ruff", "Arf Arf", "Grrr"};

    int index= (int)(Math.random()*msg.length);
    System.out.println(name + " says \""+msg[index]+"\".\n");
    }
    }




    thanks again


  2. #2
    Join Date
    Jul 1999
    Posts
    5

    Re: java

    In main you should create an instance of Dog and than invoke its methods.

    Public static void main(String[]args) {

    Dog dog = Dog("name",10/*age*/,10/*speed*/);
    dog.showDog();
    dog.fetch(2);
    dog.speak();
    }

    Public static void dogyears(Dog d) {

    int age = d.getAge();
    System.out.println("..."+age);
    }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured