Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me create jUnit test cases for class TreasureMaze.java. The TreasureMazeTest.java is created, only need to complete //TO DO comment. TreasureMazeTest.java mport static org.junit.jupiter.api.Assertions.*;

Please help me create jUnit test cases for class TreasureMaze.java. The TreasureMazeTest.java is created, only need to complete //TO DO comment.

TreasureMazeTest.java

mport static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import static main.Coursework2Main.mkMaze; /** * JUnit 5 test cases for TreasureMaze. */ public class TreasureMazeTest { // TODO }

TreasureMaze.java

public class TreasureMaze { /** * The entrance of this TreasureMaze. Must not be null. * Must be an element of this.chambers. */ private Chamber entrance; /** * Some designated Chambers that will be used for printout. * Must not be null. Must include this.entrance. */ private Chamber[] chambers; /** * Constructs a new TreasureMaze. * * @param entranceIndex * The array index in designatedChambers at which the entrance * Chamber of the TreasureMaze can be found. Must be at least * 0 and at most designatedChambers.length - 1. * @param designatedChambers * An array of Chambers in the TreasureMaze that will be used * for toString(). Must not be null, must not contain null. */ public TreasureMaze(int entranceIndex, Chamber[] designatedChambers) { sanityCheck(entranceIndex, designatedChambers); this.entrance = designatedChambers[entranceIndex]; this.chambers = new Chamber[designatedChambers.length]; System.arraycopy(designatedChambers, 0, this.chambers, 0, designatedChambers.length); } /** * Auxiliary method for sanity checks of the constructor parameters. * * @param entranceIndex * The array index in designatedChambers at which the entrance * Chamber of the TreasureMaze can be found. Must be at least * 0 and at most chambers.length - 1. * @param chambers * An array of Chambers in the TreasureMaze that will be used * for toString(). Must not be null, must not contain null. * @throws IllegalArgumentException if the parameters do not meet these * conditions */ private static void sanityCheck(int entranceIndex, Chamber[] chambers) { if (chambers == null) { throw new IllegalArgumentException("Illegal null argument: chambers"); } for (int i = 0; i < chambers.length; i++) { if (chambers[i] == null) { throw new IllegalArgumentException("Illegal null argument: chambers[" + i + "]"); } } if (entranceIndex < 0 || entranceIndex >= chambers.length) { throw new IllegalArgumentException("Illegal entranceIndex: expected 0..chambers.length-1, found: " + entranceIndex); } } /** * Lets a Visitor visit the TreasureMaze starting from the entrance. * Takes the Visitor through all Chambers in a pre-order depth-first * graph traversal. Every Chamber is visited exactly once, as long as * the Visitor wants to continue the visit. * * @param v * the visitor for the TreasureMaze, must not be null */ public void visit(Visitor v) { this.entrance.visit(v); } /** * Returns the total value of all the Treasure objects reachable from * the entrance Chamber of this TreasureMaze. * * @return the total value of all the Treasure objects reachable from * the entrance Chamber of this TreasureMaze. */ public long getValue() { ValueVisitor v = new ValueVisitor(); this.visit(v); return v.getValue(); } /** * Returns the number of different Chambers reachable from the entrance * Chamber of this TreasureMaze. * * @return the number of different Chambers reachable from the entrance * Chamber of this TreasureMaze. */ public int getReachableSize() { SizeVisitor v = new SizeVisitor(); this.visit(v); return v.getSize(); } @Override public String toString() { // Use a StringBuilder for efficient incremental String construction. StringBuilder sb = new StringBuilder(); sb.append("Maze with entrance in Chamber "); sb.append(this.entrance.getLabel()); sb.append(", size "); sb.append(this.getReachableSize()); sb.append(", and these designated chambers: "); for (Chamber c : this.chambers) { sb.append(c); sb.append(' '); } return sb.toString(); } }

Other classes you may need for info:

Chamber.java

public class Chamber { /** Class variable for the label to use for the next Chamber that is * constructed. Incremented in each constructor call so that it is * fresh for the first 2^16 constructor calls. */ private static char nextLabel = 'A'; /** * The label of the Chamber, currently a single character. */ private char label; /** * The Treasure that is the content of this Chamber. Must not be null. */ private Treasure content; /** * The list of exits of this Chamber, i.e., the list of the Chambers one * can reach directly from this Chamber. Must not be null. Initially empty, * may be added to during the object lifetime. */ private List exits; /** * Constructs a new Chamber with a fresh label, a Treasure as initial content, * and initially zero exits. * * @param content * The initial content to store in this Chamber. * Must not be null. */ public Chamber(Treasure content) { if (content == null) { throw new IllegalArgumentException("Illegal null argument: content"); } this.content = content; this.exits = new ArrayList<>(); this.label = nextLabel; nextLabel++; // set to next letter in alphabet } /** * Adds an exit to the end of the list of exits. * * @param exit * The new exit to add to the end of the list of exits. */ public void addExit(Chamber exit) { if (exit == null) { throw new IllegalArgumentException("Illegal null argument: exit"); } this.exits.add(exit); } /** * Returns the label of this Chamber. * * @return the label of this Chamber */ public String getLabel() { return this.label + ""; } /** * Returns an array with the labels of the exits of this Chamber, * in the order in which the exits are stored internally. * * @return an array with the labels of the exits of this Chamber, * in the order in which the exits are stored internally */ private String[] getExitLabels() { final int SIZE = this.exits.size(); String[] result = new String[SIZE]; for (int i = 0; i < SIZE; i++) { result[i] = this.exits.get(i).label + ""; } return result; } @Override public String toString() { return "Chamber " + this.label + ": exits " + Arrays.toString(this.getExitLabels()) + " with " + this.content; } /** * Lets a Visitor visit the Chamber network starting from this Chamber. * Takes the Visitor through the reachable Chambers in a pre-order * depth-first graph traversal. The Visitor gets to visit each of the * Chambers exactly once, as long as the Visitor wants to continue the * visit to the Chamber network. To ensure this, calls to * visitor.visit(Treasure) are made only if the call * visitor.wantsMoreVisits() returns true. * * @param visitor * The visitor for the Chamber network, must not be null. * @throws IllegalArgumentException if visitor is null */ public void visit(Visitor visitor) { if (visitor == null) { throw new IllegalArgumentException("Illegal null argument: exit"); } if(visitor.wantsMoreVisits()) visitor.visit(content); } }

SizeVisitor.java

public class SizeVisitor implements Visitor { /** * this field keeps the count of how many times visit method has been called */ private int count; /** * constructor to initialize above field with value=0 */ public SizeVisitor() { count = 0; } @Override public Treasure visit(Treasure t) { // incrementing count of visits, if t is not null if (t != null) count++; // returning the same object. return t; } /** * Returns the size of the visited data structure according to the * number of times the visit method has been called. * * @return the size of the visited data structure according to the * number of times the visit method has been called. */ public int getSize() { // returning the count of visits return count; } }

ValueVisitor.java

public class ValueVisitor implements Visitor { // defining a variable to keep the running total private long sum; // constructor initializing sum to 0 public ValueVisitor() { sum = 0; } @Override public Treasure visit(Treasure t) { // if t is not null, adding value of t to sum if (t != null) sum += t.getValue(); //returning the same object t return t; } /** * Returns the total value of all Treasure objects that have been arguments * to the visit method. * * @return the total value of all Treasure objects that have been arguments * to the visit method */ public long getValue() { //returning the value of sum variable return sum; } }

Visitor.java

public interface Visitor { /** * Performs a visit on a Treasure object, possibly calling its methods. * Returns the replacement Treasure for t (or t itself) to be stored in * the visited data structure * * @param t * The Treasure to visit. Must not be null. * @return the replacement Treasure for t (or t itself) to be stored in * the visited data structure */ Treasure visit(Treasure t); /** * Returns whether this Visitor wants to visit more Chambers. * The default implementation always returns true. * * @return whether this Visitor wants to visit more Chambers */ default boolean wantsMoreVisits() { return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

Evaluate the impact of unions on nurses and physicians.

Answered: 1 week ago

Question

Describe the impact of strikes on patient care.

Answered: 1 week ago

Question

Evaluate long-term care insurance.

Answered: 1 week ago