|
i am creating a program that have the following output
Name Number of children
John 5
<Added>
Name Number of children
john 5
lisa 3
wendy 6
Now i want use the toString method format the output become
Name Number of children
john *****
lisa ***
wendy ******
can anyone show me how to do it ?? thanks !
|
|
|
public class PersonTest
{
public static void main(String[] args)
{
String[] names = { "john", "lisa", "wendy" };
int[] ages = { 5, 3, 6 };
Person randy = new Person("Randy Johnson", names, ages);
System.out.println(randy);
}
}
class Person
{
String name;
String[] children;
int[] ages;
Person(String name, String[] kids, int[] ages)
{
this.name = name;
children = kids;
this.ages = ages;
}
public String toString()
{
String s = "Name\tNumber of Children\n";
for(int j = 0; j < children.length; j++)
{
s += children[j] + "\t";
for(int k = 0; k < ages[j]; k++)
s += "*";
s += "\n";
}
return s;
}
}
|
|
|
|
|
|
|
|
|
// |