Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Trace the code for classes References 1 and References 2 using Person / / * * * * * * * * * * *

Trace the code for classes References1 and References2 using Person
//****************// References1.java
//
// Illustrates aliases and references
??******************************************************************************************************************************************************
public class References 1
{
public static void main (String[] args)
{
Person person 1= new Person ("Rachel",6);
Person person2= new Person ("Elly",4);
Person person 3= new Person ("Sarah",19);
System.out.print In ("InThe three original people...");
System.out.print In (person1+","+ person2+","+ person3);
// Reassign people
person1= person2;
person2= person3;
person 3= person1;
System.out.printIn("
The three people reassigned...");
System.out.println (person1+","+ person 2+","+ person3);
System.out.print ln();
System.out.println ("Changing the second name to Bozo..."); person2.changeName ("Bozo");
System.out.print In (person1+","+ person2+","+ person3);
System.out.println();
System.out.println ("Changing the third name to Clarabelle..."); person3.changeName ("Clarabelle");
System.out.printIn (person1+","+ person2+","+ person3);
System.out.print ln();
System.out.printIn ("Changing the first name to Harpo..."); person1.changeName("Harpo");
System.out.println (person1+","+ person2+","+ person3);
}
}
??**************************************************************************************************************************************************
// References2.java Ilustrates references versus primitive variables
//
import java.util.Scanner;
public class References 2{
public static void main (String[] args)
int age1, age2, age3;
Scanner scan = new Scanner(
System.in);
System.out.println ("Enter three ages...");
age 1= scan.nextint();
age 2= scan.next Int ();
age 3= scan.nextInt();
// Instantiate three Person objects with the ages read in
Person person 1= new Person ("Rachel", age1);
Person person2= new Person ("Elly", age2);
Person person3= new Person ("Sarah", age3);
System.out.print ln()
System.out.println ("The original three people...");
System.out.print In (person1+","+ person2+","+ person3);
// Reassign ages in the int variables
age1= age2;
age3= age2;
// Reassign the Person objects
person1= person2;
person3= person 2 ;
System.out.println ();
System.out.print In ("The changed values are...");
System.out.printIn ("Ages (ints): "+ age1+","+ age 2+","+ age3);
System.out.println (person1+","+ person2+","+ person3);
// Make some changes to the integer values and corresponding objects System.out.println ("InChanging the second age to 99...); age =99;
person2.changeAge(age2);
System.out.println ("The changed values are...");
System.out.println ("Ages (ints): "+ age1+","+ age 2+","+ age3);
System.out.printin (person 1+",+ person 2+","+ person3);
System.out.print ln()
System.out.print In ("Changing the first age to 100...");
age 1=100;
person1.changeAge(age1);
System.out.println ("The changed values are...");
System.out.println ("Ages (ints): "+ age1+","+ age2+","+ age3);
System.out.print In (person1+","+ person2+","+ person3); }}
//1 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 changeName(String 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;
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are some possible objectives of near-net-shape forging?

Answered: 1 week ago

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago