Question: Class Work 5 : Object - Oriented Design Exercises Topics Exercises Parameter Passing - Changing People Changing People The file ChangingPeople.java contains a program that
Class Work : ObjectOriented Design Exercises
Topics Exercises
Parameter Passing Changing People
Changing People
The file ChangingPeople.java contains a program that illustrates parameter passing. The program uses Person objects defined in the file Person.java. Do the following:
Trace the execution of the program. Also show what is printed by the program.
Compile and run the program to see if your trace was correct.
Modify the changePeople method so that it does what the documentation says it does, that is the two Person objects passed in as actual parameters are actually changed.
ChangingPeople.java
Demonstrates parameter passing contains a method that should
change to Person objects.
public class ChangingPeople
Sets up two person objects, one integer, and one String
object. These are sent to a method that should make
some changes.
public static void main String args
Person person new Person Sally;
Person person new Person Sam;
int age ;
String name "Jill";
System.out.println
Parameter Passing... Original values...";
System.out.println person: person;
System.out.println person: person;
System.out.println age: age tname: name
;
changePeople person person age, name;
System.out.println
Values after calling changePeople...";
System.out.println person: person;
System.out.println person: person;
System.out.println age: age tname: name
;
Change the first actual parameter to "Jack Age and change
the second actual parameter to be a person with the age and
name given in the third and fourth parameters.
public static void changePeople Person p Person p int age, String name
System.out.println
Inside changePeople... Original parameters...";
System.out.println person: p;
System.out.println person: p;
System.out.println age: age tname: name
;
Make changes
Person p new Person name age;
p p;
name "Jack";
age ;
pchangeName name;
pchangeAge age;
Print changes
System.out.println
Inside changePeople... Changed values...";
System.out.println person: p;
System.out.println person: p;
System.out.println age: age tname: name
;
Person.java
A simple class representing a person.
public class Person
private String name;
private int age;
Sets up a Person object with the given name and age.
public Person String name, int age
this.name name;
this.age age;
Changes the name of the Person to the parameter newName.
public void changeNameString newName
name newName;
Changes the age of the Person to the parameter newAge.
public void changeAge int newAge
age newAge;
<
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
