For the questions below, consider a class called ChessPiece. This class has two instance data, String type and int player. The variable type will store "King", "Queen", "Bishop", etc and the int player will store 0 or 1 depending on whose piece it is. We wish to implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is compared to a ChessPiece passed as a parameter. Pieces are ordered as follows: "Pawn" is a lesser piece to a "Knight" and a "Bishop", "Knight" and "Bishop" are equivalent for this example, both are lesser pieces to a "Rook" which is a lesser piece to a "Queen" which is a lesser piece to a "King."
-Which of the following pieces of logic could be used in the method that implements Comparable? Assume that the method is passed Object a, which is really a ChessPiece. Also assume that ChessPiece has a method called returnType which returns the type of the given piece. Only one of these answers has correct logic.
A) if (this.type < a.returnType( ) ) return -1;
B) if (this.type = = a.returnType( ) ) return 0;
C) if (this.type.equals(a.returnType( ) ) return 0;
D) if (a.returnType( ) .equals("King") ) return -1;
E) if (a.returnType( ) .equals("Pawn") ) return 1;
Correct Answer:
Verified
Q3: Defining formal parameters requires including each parameter's
Q6: The most important decisions regarding the development
Q15: All objects implement Comparable.
Q32: In order to implement Comparable in a
Q36: For the questions below, consider a class
Q37: Which Layout Manager is used by default
Q38: Which of the following GUI classes requires
Q39: In order to display three components vertically
Q42: If a GUI container is resized by
Q45: In black-box testing, the tester should already
Unlock this Answer For Free Now!
View this answer and more for free by performing one of the following actions
Scan the QR code to install the App and get 2 free unlocks
Unlock quizzes for free by uploading documents