question archive Try the following code to see that you cannot access the variables outside of their scope levels in the toString() method

Try the following code to see that you cannot access the variables outside of their scope levels in the toString() method

Subject:Computer SciencePrice:2.87 Bought7

Try the following code to see that you cannot access the variables outside of their scope levels in the toString() method. Try to fix the errors by either using variables that are in scope or moving the variable declarations so that the variables have larger scope.

public class Person
{
private String name;
private String email;

public Person(String initName, String initEmail)
{
name = initName;
email = initEmail;
}

public String toString()
{
for (int i=0; i < 5; i++) {
int id = i;
}
// Can you access the blockScope variables i or id?
System.out.println("i at the end of the loop is " + i);
System.out.println("The last id is " + id);

// Can toString() access parameter variables in Person()?
return initName + ": " + initEmail;
}

// main method for testing
public static void main(String[] args)
{
// call the constructor to create a new person
Person p1 = new Person("Sana", "sana@mail.com");
System.out.println(p1);
}
}

Option 1

Low Cost Option
Download this past answer in few clicks

2.87 USD

PURCHASE SOLUTION

Option 2

Custom new solution created by our subject matter experts

GET A QUOTE

rated 5 stars

Purchased 7 times

Completion Status 100%