Click to See Complete Forum and Search --> : java


mini_k
September 22nd, 1999, 06:27 PM
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
kginsberg@hotmail.com


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

chorv
September 28th, 1999, 10:24 AM
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);
}