281,269 views
42 votes
42 votes
Print person1's kids, call the incNumKids() method, and print again, outputting text as below. End each line with a newline.

Sample output for below program with input 3:
Kids: 3
New baby, kids now: 4
// ===== Code from file PersonInfo.java =====
public class PersonInfo {
private int numKids;

public void setNumKids(int setPersonsKids) {
numKids = setPersonsKids;
}

public void incNumKids() {
numKids = numKids + 1;
}

public int getNumKids() {
return numKids;
}
}
// ===== end =====

// ===== Code from file CallPersonInfo.java =====
import java.util.Scanner;

public class CallPersonInfo {
public static void main(String [] args) {
Scanner scnr = new Scanner(System.in);
PersonInfo person1 = new PersonInfo();
int personsKid;

personsKid = scnr.nextInt();

person1.setNumKids(personsKid);

/* Your solution goes here */

}
}
// ===== end =====

User Nikkita
by
2.7k points

1 Answer

11 votes
11 votes

Answer:

Step-by-step explanation:

The following code was written in Java and modifies the code so that for the given input such as 3 it outputs the exact information shown in the sample output.

import java.util.Scanner;

class CallPersonInfo {

public static void main(String [] args) {

Scanner scnr = new Scanner(System.in);

PersonInfo person1 = new PersonInfo();

int personsKid;

System.out.println("How many kids do you have:");

personsKid = scnr.nextInt();

person1.setNumKids(personsKid);

/* Your solution goes here */

System.out.println("Kids: " + person1.getNumKids());

person1.incNumKids();

System.out.println("New Baby, kids now: " + person1.getNumKids());

}

}

class PersonInfo {

private int numKids;

public void setNumKids(int setPersonsKids) {

numKids = setPersonsKids;

}

public void incNumKids() {

numKids = numKids + 1;

}

public int getNumKids() {

return numKids;

}

}

Print person1's kids, call the incNumKids() method, and print again, outputting text-example-1
User Jon Steinmetz
by
2.5k points