In: Computer Science
IN JAVA PLEASE
Create a class called Child with an instance data values: name and age.
a. Define a constructor to accept and initialize instance data
b. include setter and getter methods for instance data
c. include a toString method that returns a one line description
of the child
The complete program source code for testing including all parts is given below:
class Child
{
//private data member
String name;
int age;
//default constructor
Child()
{
}
//parameterize constructor
Child(String nm, int ag)
{
name = nm;
age = ag;
}
//method to set name
public void setName(String nm)
{
name = nm;
}
//method to set age
public void setAge(int ag)
{
age = ag;
}
//method to get age
public int getAge()
{
return age;
}
//method to get name
public String getName()
{
return name;
}
//method to return description about child
public String toString()
{
return "Child description: \nName: "+name+"\nAge: "+age;
}
}
public class Main
{
public static void main(String[] args)
{
//create object
Child c = new Child("ABC",
17);
//display output
System.out.println(c);
}
}
OUTPUT:
Child description:
Name: ABC
Age: 17