Bird
0
0

Given these classes, how can you make Car use GPS to show location?

hard📝 Application Q8 of 15
Java - Classes and Objects

Given these classes, how can you make Car use GPS to show location?

class GPS {
  void showLocation() { System.out.println("Location: 123 Main St"); }
}
class Car {
  GPS gps = new GPS();
  void displayLocation() {
    // What code goes here?
  }
}
AGPS.showLocation();
Bgps.showLocation();
CshowLocation();
Dthis.showLocation();
Step-by-Step Solution
Solution:
  1. Step 1: Understand object usage inside another object

    Car has a GPS object named gps, so to call GPS method, use gps.showLocation().
  2. Step 2: Check other options

    showLocation() alone is undefined in Car, GPS.showLocation() is invalid (non-static), this.showLocation() is wrong.
  3. Final Answer:

    gps.showLocation(); -> Option B
  4. Quick Check:

    Use object reference to call method [OK]
Quick Trick: Call methods on member objects using their variable name [OK]
Common Mistakes:
  • Calling method without object reference
  • Trying to call instance method statically
  • Using this for methods not in current class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes