1. rewrite this coding in java to get correct output import java.util.*; public class Children { public static void main(String[] args) { } String name; int friends; public Children(String name) { this.name = name; friends = 0; } public void getFriend(Children k){ friends++; } public int numFriends() { return friends; } public String playsalong(){ return "joy"; } public String toString(){ return "a child named "+name;} } class Girl extends Children { public Girl(String name){ super(name); friends = 1; } public void getFriend(Children k){ if (k instanceof Girl) friends += k.numFriends( ); else friends++; } public String toString( ){ return "a girl named "+ name;} } class Boy extends Children { public Boy(String name){ super(name); } public String toString(){ return "a boy named " +name; } }
1. rewrite this coding in java to get correct output
import java.util.*;
public class Children
{
public static void main(String[] args)
{
}
String name;
int friends;
public Children(String name)
{
this.name = name;
friends = 0;
}
public void getFriend(Children k){
friends++;
}
public int numFriends() {
return friends;
}
public String playsalong(){
return "joy";
}
public String toString(){
return "a child named "+name;}
}
class Girl extends Children
{
public Girl(String name){
super(name);
friends = 1;
}
public void getFriend(Children k){
if (k instanceof Girl)
friends += k.numFriends( );
else
friends++;
}
public String toString( ){
return "a girl named "+ name;}
}
class Boy extends Children
{
public Boy(String name){
super(name);
}
public String toString(){
return "a boy named " +name; }
}
Step by step
Solved in 2 steps with 5 images