What three modifications, made independently, made to class greet, enable the code to compile and run?

Given:
#1
package handy.dandy;
public class KeyStroke {
public void typeExclamation() {
System.out.println(“!”)
}
}
#2
package handy; /* Line 1 */
public class Greet { /* Line 2 */
public static void main(String[] args) { /* Line 3 */
String greeting = “Hello”; /* Line 4 */
System.out.print(greeting); /* Line 5 */
Keystroke stroke = new Keystroke; /* Line 6 */
stroke.typeExclamation(); /* Line 7 */
} /* Line 8 */
} /* Line 9 */

What three modifications, made independently, made to class greet, enable the code to compile
and run?

Given:
#1
package handy.dandy;
public class KeyStroke {
public void typeExclamation() {
System.out.println(“!”)
}
}
#2
package handy; /* Line 1 */
public class Greet { /* Line 2 */
public static void main(String[] args) { /* Line 3 */
String greeting = “Hello”; /* Line 4 */
System.out.print(greeting); /* Line 5 */
Keystroke stroke = new Keystroke; /* Line 6 */
stroke.typeExclamation(); /* Line 7 */
} /* Line 8 */
} /* Line 9 */

What three modifications, made independently, made to class greet, enable the code to compile
and run?

A.
Line 6 replaced with handy.dandy.keystroke stroke = new KeyStroke ( );

B.
Line 6 replaced with handy.*.KeyStroke = new KeyStroke ( );

C.
Line 6 replaced with handy.dandy.KeyStroke Stroke = new handy.dandy.KeyStroke();

D.
import handy.*; added before line 1

E.
import handy.dandy.*; added after line 1

F.
import handy.dandy,KeyStroke; added after line 1

G.
import handy.dandy.KeyStroke.typeException(); added before line 1

Explanation:
Three separate solutions:
C: the full class path to the method must be stated (when we have not imported the package)
D: We can import the hold dandy class
F: we can import the specific method



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Daniel Reis

Daniel Reis

Change “,” to “.”
F.
import handy.dandy,KeyStroke; added after line 1

AchVan

AchVan

Answers E and F doesnt ake any sense vecause if you replace the package statements by an import statement you have an compile error.

AchVan

AchVan

Oops sorry. I didnt see the AFTER. “added after line 1”.

ming

ming

The original Line6 must be KeyStroke stroke = new KeyStroke();, otherwise the answers E and D will not work.