Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is printed by the following when we call method go? Assume we have a class,

ID: 3829204 • Letter: W

Question

What is printed by the following when we call method go? Assume we have a class, Circle, with the public double radius instance variable and appropriate constructor.

public static void go(){

int a = 3 ;

Circle b = new Circle (5);

foo(a,b);

System.out.println(a);

System.out.println(b.radius);

bar(a,b);

System.out.println(a);

System.out.println(b.radius);

public static void foo(int x, Circle c ) {

x = 7 ;

c.radius = 100;

}

public static void bar(int x, Circle c){

x=20;

c = new Circle(60);

}

please explain what is PRINTED AND a walk through of how you got those answers step by step. i know what is printed i just dont know why it was printed.

2) What is the appropriate way to call a static method of a different class? Give an example (with made up names )

Explanation / Answer

Output:

3

100.0

3

100.0

Reason:

public static void go(){

int a = 3 ;

Circle b = new Circle (5);

foo(a,b);

System.out.println(a);

System.out.println(b.radius);

bar(a,b);

System.out.println(a);

System.out.println(b.radius);

public static void foo(int x, Circle c ) {

x = 7 ;

c.radius = 100;

}

public static void bar(int x, Circle c){

x=20;

c = new Circle(60);

}

When this statement is executed

Circle b = new Circle (5);

A new Circle class object is created by passing the 5 as argument.

Then we are passing the variable ‘a’ and The circle object reference as inputs while calling the foo(a,b) method.

Inside that we are assigning the value 7 to the variable x.

And we are changing the value of the radius of the Circle class object.

When we are displaying the value of a it just prints the value 3.As x=7 is the local variable of the method foo() .The scope of the local variable is limit to that method itself.So it wont reflects the in the go() method.

But we when we prints b.radius.it will display 100.Because we are mutating(changing) the value of the variable inside the Circle class Object.

And next we are passing the variable ‘a’ and the circle class object reference as input to the bar() method.

Here inside the bar() method we are assigning the value 20 to the variable x.

After that we are creating the new Circle class object by passing the 60 as input.

After that when the statement System.out.println(a); is executed it displays the 3.As the scope of the local variables are limited to the methods in which they have been declared.

So that value of the variable ‘a’ remains unchanged as 3.

System.out.println(b.radius);

When we tried to print the radius value of the Circle object Pointed by the reference b.

It displays the value 100.

Because inside the bar(0 method we are not performing and change on the existing circle object class radius.Instead we created a new Circle Object.

So the value of the radius inside the Circle class object pointed by the variable b will remains unchanged.

So the final output is

3

100.0

3

100.0

_________________

2)

When we call the static method of another class te format or syntax is Classname,methodname();

we no need to create the obejct of another class to class the static method.

Simplay we can call by using the Classname.

Ex:

Employee.java

public class Employee

{

private int id;

private String firstname;

private String lastname;

public static String getFulName() //This is the static method which return the String

{

return firstname+lastname;

}

}

_____________

public class Test {

   public static void main(String[] args) {

Employee.getFulName(); //Here we are calling the static method of Employee class.

}

}

_________________

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote