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


mini_k
September 23rd, 1999, 01:52 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



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

unicman
September 25th, 1999, 02:14 AM
I have seen the problem in ur mail also, and in this discussion forum also. But, I haven't answered that question, since its ur assignment. And I think if someone else than u does it, u will not understand what should be done.

If u go thro' Program steps, u should be able to create a program in minutes! The program is very simple.

I don't know how much u know Java and Object Oriented Programming. I am assuming, u don't know much about OOP.

I am going to explain u how to go about the problem, if u r not interested then u don't have to read the rest of the post.

1. Object Oriented programming:

In Object Oriented Programming (like Java), u visualize a 'dog' as a object. Now whatever a 'dog' can do is in this object. Now a 'Dog Class' defines the working of "dog" object. So obviously if u want to have a dog object, u will have to create 'Dog' class.

2. How to create a class:

In java u create a class like...


class Dog
{
Dog( String n, int a, int s)
{
// This method is called as a costructor.
// All the constructors must have same
// name as that of a class.
// (thats why name of this method
// is 'Dog'.
// Normally u initialize all the data in
// this method, (like u have set 'name = n' etc)
}

String getName()
{
// This is a normal method for this
// class (and for the object).
// As u can see in the real code of this
// class, it returns name of the dog
}

void showDog()
{
// This is also a normal method for this
// class (and for the object).
// As u can see in the real code of this
// class, it displays all the details of a dog
// This method does not return anything.
// Just in case u don't know 'void' means nothing.
}
}




3. How to create a object (i.e. how to create a "dog"):

For creating a "dog" object (as ur assignment says) in Java, u need to write...


Dog aDog = new Dog( "Fido", 10, 4 );




Here....
- 'aDog' is a variable. It means it can hold a dog object
- new is a operator with which you can create a dog object
- so using the above statement I have create a dog object with name 'Fido'

Now for calling any method in 'Dog' class (e.g. 'getName'), u must have a dog object (unless that method is 'static').
U have got a dog object (using the above statement) in 'aDog'.

4. How to call a method of a object

Suppose u want to know the name of the dog which u have got in 'aDog' variable. For this u will call...


String dogName = aDog.getName();




Using this statement u will get the name of the dog in 'dogName' variable.
If u display the name using...
System.out.println( "Dog name " + dogName );

U will get output as...
Dog name Fido

Like this if u want to show details of a dog u will call,

aDog.showDog();




Remember that this method displays the dog details itself.

Like this using 'aDog' u can call any method of class 'Dog'.

5. How to call a static method:

Calling a static method is a little bit different. U can call a static method (e.g. 'dogyears') as...


p1.dogyears( aDog );




Here...
- as u can see 'p1' is name of the class. It means u don't need to create object of class 'p1'.
- so u can directly call this 'static' method of class 'p1' as shown above

6. What to write in 'dogyears' method:

In this method u want to 'display' dogyears of dog 'd'.
So u can get the age using...

int age = d.getAge();




And the name using...

String name=d.getName();




So the method can be written as...


public static void dogyears(Dog d)
{
String name = d.getName();
int age = d.getAge();
int dogYears = age * 7;

System.out.println( name + " is " + dogYears + " in dog years." );
}




I think u should try to code in 'main' method so that u will get the hang of the concept.

- UnicMan
http://members.tripod.com/unicman