Question: Datastructures Java - How would I test this particular part in the main? The part I need tested is in bold. import java.util.logging.Level; import java.util.logging.Logger;
Datastructures Java - How would I test this particular part in the main?
The part I need tested is in bold.
import java.util.logging.Level; import java.util.logging.Logger;
public class GamePiece extends Object implements Comparable { private Piece classification; private String name; private String playerName; public GamePiece() { this.classification = Piece.Rock; this.name = this.classification.toString(); } public GamePiece(Piece classification, String name, String playerName) { this.classification = classification; this.name = name; this.playerName = playerName; } public boolean equals(GamePiece piece) { if (this.classification == piece.getClassification()) { return true; } return false; } @Override public int compareTo(GamePiece piece) { Piece currentPieceType = this.classification; Piece newPieceType = piece.classification; //Player A Rock if (currentPieceType == Piece.Rock) { if (newPieceType == Piece.Paper) { System.out.println("Paper covers rock"); return -1; } else if (newPieceType == Piece.Scissors) { System.out.println("Rock crushes scissors"); return 1; }else if (newPieceType == Piece.Rock) { System.out.println("Rock does nothing to rock"); return 0; }else if (newPieceType == Piece.Lizard) { System.out.println("Rock crushes lizard"); return 1; }else if (newPieceType == Piece.Spock) { System.out.println("Spock vaporizes rock"); return -1; } } //Player A Paper else if (currentPieceType == Piece.Paper) { if (newPieceType == Piece.Rock) { System.out.println("Paper covers rock"); return 1; }else if (newPieceType == Piece.Scissors) { System.out.println("Scissors cuts paper"); return -1; }else if (newPieceType == Piece.Lizard) { System.out.println("Lizard eats paper"); return -1; }else if (newPieceType == Piece.Spock) { System.out.println("Paper disproves Spock"); return 1; }else if (newPieceType == Piece.Paper) { System.out.println("Paper does nothing to paper"); return 0; } } //Player A Scissors else if (currentPieceType == Piece.Scissors) { if (newPieceType == Piece.Scissors) { System.out.println("Scissors does nothing to scissors"); return 0; }else if (newPieceType == Piece.Rock) { System.out.println("Rock crushes scissors"); return -1; }else if (newPieceType == Piece.Paper) { System.out.println("Scissors cuts paper"); return 1; }else if (newPieceType == Piece.Lizard) { System.out.println("Scissors decapitates lizard "); return 0; }else if (newPieceType == Piece.Spock) { System.out.println("Spock smashes scissors "); return -1; } } //Player A Lizard else if (currentPieceType == Piece.Lizard) { if (newPieceType == Piece.Lizard) { System.out.println("Lizard does nothing to lizard"); return 0; }else if (newPieceType == Piece.Rock) { System.out.println("Rock crushes lizard"); return -1; }else if (newPieceType == Piece.Scissors) { System.out.println("Scissors decapitates lizard "); return -1; }else if (newPieceType == Piece.Paper) { System.out.println("Lizard eats paper"); return 1; }else if (newPieceType == Piece.Spock) { System.out.println("Lizard poisons Spock"); return 1; } } //Player A Spock else if (currentPieceType == Piece.Spock) { if (newPieceType == Piece.Spock){ System.out.println("Spock does nothing to Spock"); return 0; }else if (newPieceType == Piece.Rock) { System.out.println("Spock vaporizes rock"); return 1; }else if (newPieceType == Piece.Paper) { System.out.println("Paper disproves Spock"); return -1; }else if (newPieceType == Piece.Scissors) { System.out.println("Spock smashes scissors "); return 1; }else if (newPieceType == Piece.Lizard) { System.out.println("Lizard poisons Spock"); return -1; }else{ } } return 0; } public enum Piece { Rock { public String toString() { return "Im hard and have sharp edges."; } }, Paper { public String toString() { return "Im made of trees and can cover a rock easily.. and disprove Spock!"; } }, Scissors { public String toString() { return "Im extra sharp to cut right through paper... and lizards!"; } }, Lizard { public String toString() { return "Hsssss."; } }, Spock { public String toString() { return "It is only logical that you do your CIS 2353 homework."; } } } public void setPlayerName(String playerName) throws Exception { if (playerName.length() > 20) { throw new Exception("Input too long!"); } else { this.playerName = playerName; } } public String getPlayerName() { return playerName; } public Piece getClassification() { return classification; }
public void setClassification(Piece classification) { this.classification = classification; }
public String getName() { return name; }
public void setName(String name) { this.name = Piece.valueOf(name).toString(); } }
//This is what I have for the main bellow
public class GamePieceDemo {
public static void main(String[] args){ GamePiece gamePiece = new GamePiece(); gamePiece.setName("Rock"); try{ gamePiece.setPlayerName("A name with more than twenty chars"); } catch(Exception ex){ System.out.println(ex.getMessage()); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
