Question: In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2.

In a project named 'DogApplication', create a class called 'Dog'

1. declare two instance variables in 'Dog' :

name (String type)

age (int type)

2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables

3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with any values of your choice for name and age.

4. go back to 'Dog' class and implement the 'toString' method and then call this method from 'Driver' to print the dogs' info.

this is what I have so far:

for Dog

public class Dog { //instance variables private String name; private int age; //constructor, takes the instance variable and binds to constructor public Dog(String dogName, int dogAge) //has the same name as the class { name = dogName; //assigns the instance variables which has no value to the constructor parameters age = dogAge; } public String toString() { String Dog = ""; Dog += this.name + "\t"; Dog += this.age; return Dog; } }

and for Driver:

public class Driver { public static void main (String [] args) { Dog myDog = new Dog ("Gino", 6); Dog yourDog = new Dog ("Amie",7); System.out.println(Dog.name); System.out.println(Dog.age); } }

I'm not sure if I did this right. Can you explain?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!