Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Trace the code for classes References 1 and References 2 using Person / / * * * * * * * * * * *
Trace the code for classes References and References using Person
Referencesjava
Illustrates aliases and references
public class References
public static void main String args
Person person new Person Rachel;
Person person new Person Elly;
Person person new Person Sarah;
System.out.print In InThe three original people...";
System.out.print In person person person;
Reassign people
person person;
person person;
person person;
System.out.printIn
The three people reassigned...";
System.out.println person person person;
System.out.print ;
System.out.println Changing the second name to Bozo..."; personchangeName Bozo;
System.out.print In person person person;
System.out.println;
System.out.println Changing the third name to Clarabelle..."; personchangeName Clarabelle;
System.out.printIn person person person;
System.out.print ;
System.out.printIn Changing the first name to Harpo..."; personchangeNameHarpo;
System.out.println person person person;
Referencesjava Ilustrates references versus primitive variables
import java.util.Scanner;
public class References
public static void main String args
int age age age;
Scanner scan new Scanner
System.in;
System.out.println Enter three ages...";
age scan.nextint;
age scan.next Int ;
age scan.nextInt;
Instantiate three Person objects with the ages read in
Person person new Person Rachel age;
Person person new Person Elly age;
Person person new Person Sarah age;
System.out.print
System.out.println The original three people...";
System.out.print In person person person;
Reassign ages in the int variables
age age;
age age;
Reassign the Person objects
person person;
person person ;
System.out.println ;
System.out.print In The changed values are...";
System.out.printIn Ages ints: age age age;
System.out.println person person person;
Make some changes to the integer values and corresponding objects System.out.println InChanging the second age to ; age ;
personchangeAgeage;
System.out.println The changed values are...";
System.out.println Ages ints: age age age;
System.out.printin person person person;
System.out.print
System.out.print In Changing the first age to ;
age ;
personchangeAgeage;
System.out.println The changed values are...";
System.out.println Ages ints: age age age;
System.out.print In person person person;
P
public class Person
i
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;
Raturns the person's name and age as a surng.
public String toString
return name Age age;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started