Given the following class diagram,
which Java implementation most closely matches this structure?
Diagram: Book + price + getRating()
A. public class Book { public int numOfPages; B. public class Book { public String getRating() {return null;} } /*from www . jav a 2 s. c om*/ C. public class Book { public int price; public String getRating() {return null;} } D. public class Book { void price; }
C.
Option A does not compile because it is missing the closing bracket for the class.
Option D does not compile as void is not a valid type for a variable.
Options A and D are incorrect as they are missing the getRating()
method.
Option A uses an abbreviation for price
.
Option B is incorrect since it is missing the price
attribute.
Option C is the correct answer as it properly defines the attribute price
and method getRating()
.