51.9k views
5 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: 3New 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 Streak
by
4.6k points

2 Answers

4 votes

Answer:

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

person1.incNumKids();

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

Step-by-step explanation:

Print person1's kids, call the incNumKids() method, and print again, outputting text-example-1
User August Kimo
by
5.5k points
6 votes

Answer:

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

person1.incNumKids();

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

Step-by-step explanation:

Reminder: To be able to call a function that is inside a class, we need to use an object of that class. In this case, person1 is our object. If we would like to access any function, we need to type person1.functionName().

1) We need to call getNumKids() to get how many kids they have (It is 3 because user inputs 3 and it is set to the this value using setNumKids() function). To call this function, we need to use person1 object that is created earlier in the main function. To be able to print the result, we call the function inside the System.out.println().

2) To increase the number of kids, we need to call incNumKids() method with person1 object again. This way number of kids are increased by one, meaning they have 4 kids now.

3) To get the current number of kids, we need to call getNumKids() function using person1 object. To be able to print the result, we call the function inside the System.out.println().

User Dan Borza
by
5.2k points