129k views
3 votes
Which of the following statements are valid in Java? (Assume that console is Scanner object initialized to the standard input device.) (i) r = console.nextDouble(); myCircle.area = 3.14 * r * r; System.out.println(myCircle.area); (ii) r = console.nextDouble(); myCircle.set(r); System.out.println(myCircle.area()); Only (i) Only (ii) Both (i) and (ii) None of these

1 Answer

2 votes

Answer:

Both (i) and (ii) are valid in Java

Step-by-step explanation:

In (i) 'area' is a variable property of the object myCircle. It could be assigned a value. In this case it has been assigned a value equal to the product of 3.14 and the square of the read input of type double from the keyboard.

After assigning a value to area, it could be printed directly to the console using the println method.

In (ii) 'area' is a method property of the object myCircle. It could be called when the value of the radius(r) has been set using a second method 'set' of myCircle. The call to myCircle.area() probably prints the area of the circle, myCircle, to the console.

In summary, though different, both are valid ways of doing the same thing.

Hope this helps!

User Fan Jin
by
5.2k points