Purchase Solution

Java programming - Person Runner

Not what you're looking for?

Ask Custom Question

Using the Person and PersonRunner classes below, execute the main method in PersonRunner. Why does the program end abnormally? Comment out the first two lines and run the program again. Why does the reference to p3 cause Frank's name to be printed when we set Frank's name with the p2 reference?
public class PersonRunner
{
public static void main(String[] args)
{
Person p1 = null;
p1.speak();
Person p2 = new Person("Beth");
p2.speak();
Person p3 = p2;
p2.speak();
p2.setName("Frank");
p3.speak();
}
}

public class Person
{
private String name;

public Person(String name)
{
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void speak()
{
System.out.println("My name is " + name);
}
}

Purchase this Solution

Solution Summary

The solution answers all the questions regarding the failure of given program. And then go on to explain what happens when we modify the program and why there are any problems/errors.

Solution Preview

Dear student,

Please find below the solution.

Thanks,
Kavita Narang

(--Please note that my work should only be used as a guidance and not be copied into your assignments.
The answers available here are meant for assistance and the understanding of challenging academic topics.
Use the solution alongside your own work and watch your knowledge and grades ...

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

C++ Operators

This quiz tests a student's knowledge about C++ operators.