Question

YOU MUST NOT MAKE ANY CHANGES TO THAT CODE package edu.buffalo.cse116; /** * Class that can...

 YOU MUST NOT MAKE ANY CHANGES TO THAT CODE
package edu.buffalo.cse116;

/**
 * Class that can be used to test if two integers are coprime. Numbers are considered coprime iff the largest divisor
 * shared by both numbers is 1. Identifying coprime (also called relatively prime) numbers is valuable for RSA public
 * key cryptography (and possibly other things, but the author has never seen any other uses).
 */
public class CoprimeNumbers {

  /**
   * Given two integers, this returns true if they are relatively prime and false if they are not. Based upon the first
   * webpage I found ({@link "https://primes.utm.edu/notes/faq/negative_primes.html"}), the primality of negative
   * numbers is up for debate. This method will not treat negatives differently.
   *
   * @param a First integer to be tested
   * @param b Second integer to be tested
   * @return True when the greatest common divisor of these numbers is 1; false otherwise.
   */
  public boolean isCoprime(int a, int b) {
    // Continue using Euclid's algorithm until we find a common divisor
    while (b != 0) {
      // Remember b's value
      int temp = b;
      // Set b to the remainder of dividing a by b (e.g., a mod b).
      b = a % b;
      // Set a equal to b's old value.
      a = temp;
    }
    // The gcd is the value in a. If this is 1 the numbers are coprime.
    if (a == 1) {
      return true;
    }
    // When they are not 1, they have a common divisor.
    else {
      return false;
    }
  }
}

QUESTION:

write a class, named CoprimeNumbersTest and in the edu.buffalo.cse116 package which contains JUnit test cases validating my code works (e.g., your tests will execute all the lines in my code). Because this is important, I reiterate your solution must test my unchanged CoprimeNumbers.java file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the Junit test for your class. It tests for various combinations when b=0 , b=1, a>b , a<b and a=b. You can just run it in eclipse by right click and Run as Junit Test. Since the return value from your code is eithertrue or false, we just use assertTrue() or assertFalse().

package edu.buffalo.cse116;

import static org.junit.Assert.*;

import org.junit.Test;

public class CoprimeNumbersTest {
   CoprimeNumbers cpn= new CoprimeNumbers();
  
   @Test
   public void test1()
   {
       //Test when a = b
       boolean b=cpn.isCoprime(10, 10);
       assertFalse(b);
   }
  
   @Test
   public void test2()
   {
       //Test when b=1
       boolean b=cpn.isCoprime(7, 1);
       assertTrue(b);
   }
  
   @Test
   public void test3()
   {
       //Test when b=0
       boolean b=cpn.isCoprime(7, 0);
       assertFalse(b);
   }
  
  
   @Test
   public void test4()
   {
       //Test when a < b
       boolean b=cpn.isCoprime(5,7);
       assertTrue(b);
   }
   @Test
   public void test5()
   {
       //Test when a > b
       boolean b=cpn.isCoprime(13, 11);
       assertTrue(b);
   }
}

workspace - <Java-lestava/src/edu/buffalo/csell6/CoprimeNumbers lest.java - Eclipse File Edit Source Refactor Navigate Search

Add a comment
Know the answer?
Add Answer to:
YOU MUST NOT MAKE ANY CHANGES TO THAT CODE package edu.buffalo.cse116; /** * Class that can...
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
  • Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade *...

    Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade * */ public class Employee { private int empId; private String empName; private Vehicle vehicle; public Double calculatePay() { return null; } /** * @return the empId */ public int getEmpId() { return empId; } /** * @param empId * the empId to set */ public void setEmpId(int empId) { this.empId = empId; } /** * @return the empName */ public String getEmpName() { return...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test...

    PLEASE HURRY Software Testing Don't make any changes to the provided code. Please write a test case for the below prompt using the provided java programs Use the setString() function in MyCustomStringInterface to set the value to “Peter Piper picked a peck of pickled peppers.”. Then test to see if reverseNCharacters() function returns the reversed string when the characters are reversed in groups of 4 and padding is disabled (in this case, “etePiP r repkcipa decep fo kcip delkpep srep.”)....

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Modify the Triangle class (from previous code, will post under this) to throw an exception in...

    Modify the Triangle class (from previous code, will post under this) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides. In addition, for any valid triangle supply a method to compute and display the area of the triangle using...

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