148k views
1 vote
Print person1's kids, apply the IncNumKids() function, and print again, outputting text as below. End each line with a newline.

Sample output for the below program:
Kids: 3 New baby, kids now: 4

Sample program:
// public class PersonInfo { private int numKids; public void setNumKids(int personsKids) { numKids = personsKids; return; } public void incNumKids() { numKids = numKids + 1; return; } public int getNumKids() { return numKids; } } //

2 Answers

2 votes

Final answer:

To fulfill the request, instantiate the PersonInfo class, use the setNumKids() to set the initial number of kids, print it, apply the incNumKids() method to increment the count, and print the updated count.

Step-by-step explanation:

To address the student's request, we need to create an instance of the PersonInfo class, set the number of kids for person1, then print that information. Afterward, we will apply the IncNumKids() function to simulate having a new baby, and print the updated number of kids.

The code example, based on the given program structure, will look like this:

// Assume the given PersonInfo class code is available
public class Main {
public static void main(String[] args) {
PersonInfo person1 = new PersonInfo();
person1.setNumKids(3);

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

person1.incNumKids();

System.out.println("New baby, kids now: " + person1.getNumKids());
}
}
This code initializes person1 with 3 kids due to the setNumKids(3) method call. It prints that value, calls incNumKids() to increment the count (simulating a new baby), and prints the new count.
User Posttwo
by
5.2k points
1 vote

Final answer:

To fulfill the student's request, a main method is created to use the 'PersonInfo' class. This main method sets the initial number of kids, prints it, increases the count by one, and prints the new count, matching the desired output.

Step-by-step explanation:

The question pertains to an exercise in object-oriented programming (OOP), which is a typical concept taught in computer science classes. In OOP, objects represent instances of classes. The provided PersonInfo class has methods to set and increment the number of kids a person has, namely setNumKids() and incNumKids(). However, a main method that demonstrates the class' usage is missing from the sample program and must be written.

To accomplish the task, one would write the main method like so:

public static void main(String[] args) {
PersonInfo person1 = new PersonInfo();
person1.setNumKids(3);
System.out.println("Kids: " + person1.getNumKids());
person1.incNumKids();
System.out.println("New baby, kids now: " + person1.getNumKids());
}

This will instantiate person1 using the PersonInfo class, initially set the number of kids to 3, print the number of kids, then increment the number and print it again, resulting in the sample output provided.

User Pfunk
by
4.9k points