Question

Hi so I am currently working on a project for a java class and I am...

Hi so I am currently working on a project for a java class and I am having trouble with a unit testing portion of the lab. This portion wants us to add three additional tests for three valid arguments and one test for an invalid argument. I tried coding it by myself but I am not well versed in unit testing so I am not sure if it is correct or if the invalid unit test will provide the desired outcome.

@Test
public void isValidPick() {
    Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.LIZARD));
    Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SPOCK));
    Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.PAPER));
    Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.ROCK));
    Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SCISSORS));

    Assert.assertFalse(RPSLSpock.isValidPick(null));

    // TODO remove this comment and add tests for the other three valid arguments

    // TODO remove this comment and verify the method returns false if passed and invalid argument like "banana"
}

Here is the portion of code I am specifically referencing. I will post the full code down below. All I want is to know if I coded the unit test correctly and if I did not what I need to change about it to make it correct. Thank you!

Main Class:

import java.util.*;

/**
 * Main for Rock, Paper, Scissors, Lizard, Spock
 */
public class Main {

    private static Scanner input = new Scanner(System.in);

    /**
     * Main method for running Rock, Paper, Scissors, Lizard, Spock
     * @param args run-time arguments
     */
    public static void main(String[] args) {
        String h_pick;
        String c_pick;
        String answer;
        boolean isValid;

        do {
            System.out.println("Let's play rock, paper, scissors, lizard, spock");
            do {
                System.out.print("Enter your choice: ");
                h_pick = input.nextLine();
                isValid = RPSLSpock.isValidPick(h_pick);
                if (!isValid) {
                    System.out.println(h_pick + " is not a valid choice");
                }
            } while (!isValid);

            c_pick = RPSLSpock.generatePick();
            System.out.print("Computer picked " + c_pick + "  ");

            if (c_pick.equalsIgnoreCase(h_pick)) {
                System.out.println("Tie!");
            } else if (RPSLSpock.isComputerWin(c_pick, h_pick)) {
                System.out.println("Computer wins!");
            } else {
                System.out.println("You win!");
            }

            System.out.print("Play again ('y' or 'n'): ");
            answer = input.nextLine();
        } while ("Y".equalsIgnoreCase(answer));
        System.out.println("Live long and prosper!");
    }
}

RPSLSpock Class:

import java.util.Random;

// TODO remove this comment and  document this class. Be sure to include an @author tag
public class RPSLSpock {

    static Random rand = new Random(System.currentTimeMillis());

    public static final String ROCK = "rock";
    public static final String PAPER = "paper";
    public static final String SCISSORS = "scissors";
    public static final String LIZARD = "lizard";
    public static final String SPOCK = "spock";

    // TODO remove this comment and document this method
    public static boolean isValidPick(String pick) {
        if (pick == null) {
            return false;
        }
        pick = pick.trim();
        return (ROCK.equalsIgnoreCase(pick) ||
                PAPER.equalsIgnoreCase(pick) ||
                SCISSORS.equalsIgnoreCase(pick) ||
                LIZARD.equalsIgnoreCase(pick) ||
                SPOCK.equalsIgnoreCase(pick));
    }

    // TODO remove this comment and document this method
    public static String generatePick() {
        String pick = null;
        switch (rand.nextInt(5)) {
            case 0:
                pick = ROCK;
                break;
            case 1:
                pick = PAPER;
                break;
            case 2:
                pick = SCISSORS;
                break;
            case 3:
                pick = LIZARD;
                break;
            case 4:
                pick = SPOCK;
                break;
        }
        return pick;
    }

    // TODO remove this comment and document this method
    public static boolean isComputerWin(String c_pick,String h_pick) {
        h_pick = h_pick.toLowerCase();
        return ((ROCK.equals(c_pick) && (SCISSORS.equals(h_pick) || LIZARD.equals(h_pick))) ||
                (PAPER.equals(c_pick) && (ROCK.equals(h_pick) || SPOCK.equals(h_pick))) ||
                (SCISSORS.equals(c_pick) && (PAPER.equals(h_pick) || LIZARD.equals(h_pick))) ||
                (LIZARD.equals(c_pick) && (PAPER.equals(h_pick) || SPOCK.equals(h_pick))) ||
                (SPOCK.equals(c_pick) && (ROCK.equals(h_pick) || SCISSORS.equals(h_pick))));
    }
}

RPSLSpockTest Class:

import org.junit.Assert;
import org.junit.Test;

/**
 * Set of unit tests for RPSLSpock methods
 */
public class RPSLSpockTest {

    /**
     * Test isValidPick() method
     * Test that it returns true if argument is {rock,paper,scissors,lizard, or spock}
     * Test that it returns false if the argument is not a valid input String
     */
    @Test
    public void isValidPick() {
        Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.LIZARD));
        Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SPOCK));
        Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.PAPER));
        Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.ROCK));
        Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SCISSORS));

        Assert.assertFalse(RPSLSpock.isValidPick(null));

        // TODO remove this comment and add tests for the other three valid arguments

        // TODO remove this comment and verify the method returns false if passed and invalid argument like "banana"
    }

    /**
     * Test generatePick() method
     * Test that it returns a non-null String
     * Test that the String it returns is valid
     * Since the method is based on a RANDOM number - test it ONE MILLION times
     */
    @Test
    public void generatePick() {
        for (int i=0; i<1000000; ++i) {
            String pick = RPSLSpock.generatePick();
            Assert.assertNotNull(pick);
            Assert.assertTrue(RPSLSpock.isValidPick(pick));
        }
    }

    /**
     * Test the isComputerWin method
     * Test it with all ten possible computer win scenarios (it should return true)
     * Test it with at least one computer loses scenario to make sure it returns false
     */
    @Test
    public void isComputerWin() {
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.ROCK,RPSLSpock.SCISSORS));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.ROCK,RPSLSpock.LIZARD));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.PAPER,RPSLSpock.ROCK));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.PAPER,RPSLSpock.SPOCK));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.PAPER));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.LIZARD));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.LIZARD,RPSLSpock.PAPER));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.LIZARD,RPSLSpock.SPOCK));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SPOCK,RPSLSpock.ROCK));
        Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SPOCK,RPSLSpock.SCISSORS));

        Assert.assertFalse(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.SPOCK));
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files

Main.java

import java.util.*;

/**
* Main for Rock, Paper, Scissors, Lizard, Spock
*/
public class Main {

private static Scanner input = new Scanner(System.in);

/**
* Main method for running Rock, Paper,2 Scissors, Lizard, Spock
* @param args run-time arguments
*/
public static void main(String[] args) {
String h_pick;
String c_pick;
String answer;
boolean isValid;

do {
System.out.println("Let's play rock, paper, scissors, lizard, spock");
do {
System.out.print("Enter your choice: ");
h_pick = input.nextLine();
isValid = RPSLSpock.isValidPick(h_pick);
if (!isValid) {
System.out.println(h_pick + " is not a valid choice");
}
} while (!isValid);

c_pick = RPSLSpock.generatePick();
System.out.print("Computer picked " + c_pick + " ");

if (c_pick.equalsIgnoreCase(h_pick)) {
System.out.println("Tie!");
} else if (RPSLSpock.isComputerWin(c_pick, h_pick)) {
System.out.println("Computer wins!");
} else {
System.out.println("You win!");
}

System.out.print("Play again ('y' or 'n'): ");
answer = input.nextLine();
} while ("Y".equalsIgnoreCase(answer));
System.out.println("Live long and prosper!");
}
}

RPSLSpock.java

import java.util.Random;

/**
* RPSLSpock Class
* @author yourname_here
*/
public class RPSLSpock {

public static String WATER;
static Random rand = new Random(System.currentTimeMillis());

public static final String ROCK = "rock";
public static final String PAPER = "paper";
public static final String SCISSORS = "scissors";
public static final String LIZARD = "lizard";
public static final String SPOCK = "spock";

/**
*
* @param pick users choice
* @return false when pick is null
* when user picks valid option, uses ignoreCase to return pick
*/
public static boolean isValidPick(String pick) {
if (pick == null) {
return false;
}
pick = pick.trim();
return (ROCK.equalsIgnoreCase(pick) ||
PAPER.equalsIgnoreCase(pick) ||
SCISSORS.equalsIgnoreCase(pick) ||
LIZARD.equalsIgnoreCase(pick) ||
SPOCK.equalsIgnoreCase(pick));
}

/**
* randomly generated pick from 5 options (array), breaks once a pick has been matched
* @return computer generated pick
*/
public static String generatePick() {
String pick = null;
switch (rand.nextInt(5)) {
case 0:
pick = ROCK;
break;
case 1:
pick = PAPER;
break;
case 2:
pick = SCISSORS;
break;
case 3:
pick = LIZARD;
break;
case 4:
pick = SPOCK;
break;
}
return pick;
}

/**
*
* @param c_pick computer generated pick versus
* @param h_pick human chosen pick
* @return resultant is a computer win
*/
public static boolean isComputerWin(String c_pick,String h_pick) {
h_pick = h_pick.toLowerCase();
return ((ROCK.equals(c_pick) && (SCISSORS.equals(h_pick) || LIZARD.equals(h_pick))) ||
(PAPER.equals(c_pick) && (ROCK.equals(h_pick) || SPOCK.equals(h_pick))) ||
(SCISSORS.equals(c_pick) && (PAPER.equals(h_pick) || LIZARD.equals(h_pick))) ||
(LIZARD.equals(c_pick) && (PAPER.equals(h_pick) || SPOCK.equals(h_pick))) ||
(SPOCK.equals(c_pick) && (ROCK.equals(h_pick) || SCISSORS.equals(h_pick))));
}
}

RPSLSpockTest.java

import org.junit.Assert;
import org.junit.Test;

/**
* Set of unit tests for RPSLSpock methods
*/

public class RPSLSpockTest {

/**
* Test isValidPick() method
* Test that it returns true if argument is {rock,paper,scissors,lizard, or spock}
* Test that it returns false if the argument is not a valid input String
*/

@Test
public void isValidPick() {
Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.LIZARD));
Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SPOCK));
Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.SCISSORS));
Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.PAPER));
Assert.assertTrue(RPSLSpock.isValidPick(RPSLSpock.ROCK));
Assert.assertFalse(RPSLSpock.isValidPick(RPSLSpock.WATER));

}

/**
* Test generatePick() method
* Test that it returns a non-null String
* Test that the String it returns is valid
* Since the method is based on a RANDOM number - test it ONE MILLION times
*/
@Test

public void generatePick() {
for (int i=0; i<1000000; ++i) {
String pick = RPSLSpock.generatePick();
Assert.assertNotNull(pick);
Assert.assertTrue(RPSLSpock.isValidPick(pick));
}
}

/**
* Test the isComputerWin method
* Test it with all ten possible computer win scenarios (it should return true)
* Test it with at least one computer loses scenario to make sure it returns false
*/

@Test
public void isComputerWin() {
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.ROCK,RPSLSpock.SCISSORS));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.ROCK,RPSLSpock.LIZARD));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.PAPER,RPSLSpock.ROCK));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.PAPER,RPSLSpock.SPOCK));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.PAPER));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.LIZARD));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.LIZARD,RPSLSpock.PAPER));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.LIZARD,RPSLSpock.SPOCK));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SPOCK,RPSLSpock.ROCK));
Assert.assertTrue(RPSLSpock.isComputerWin(RPSLSpock.SPOCK,RPSLSpock.SCISSORS));

Assert.assertFalse(RPSLSpock.isComputerWin(RPSLSpock.SCISSORS,RPSLSpock.SPOCK));
}
}

output

Let's play rock, paper, scissors, lizard, spock
Enter your choice: rock
Computer picked rock Tie!
Play again ('y' or 'n'): y
Let's play rock, paper, scissors, lizard, spock
Enter your choice: paper
Computer picked scissors Computer wins!
Play again ('y' or 'n'): n
Live long and prosper!

Hope this helps!

Please let me know if any changes needed.

Thank you!

Add a comment
Know the answer?
Add Answer to:
Hi so I am currently working on a project for a java class and I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • easy question but i am confused. someone help Consider the following Java classes: class OuterClass {...

    easy question but i am confused. someone help Consider the following Java classes: class OuterClass { static class InnerClass { public void inner Method({ System.out.println("This is my inner class"); public static void outer Method{ System.out.println("This is my outer class"); public class OuterClass Test{ public static void main(String args[]) { Complete the class OuterClass Test to produce as output the two strings in class OuterClass. What are the outputs of your test class?

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way...

    “Oh I know, let’s play Rock, Paper, Scissors, Lizard, Spock. It’s very simple.” What better way to expand the classic Rock, Paper, Scissors game than to add Lizard and Spock! Sheldon and Raj brought Sam Kass’s game to life on The Big Bang Theory in “The Lizard-Spock Expansion” episode.   Assignment & Discussion Your task in Programming Assignment 8 is to create a C# console app-version of Rock, Paper, Scissors, Lizard, Spock (RPSLS). Use any of the programming tools and techniques...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT