Question

In: Computer Science

Given the following code below: public class Calculation { //method that returns cube of the given...

  1. Given the following code below:
  1. public class Calculation {
  2. //method that returns cube of the given number
  3. public int findMax(int arr[]){
  4.    int max=0;
  5.    for(int i=1;i<arr.length;i++){
  6.        if(max<arr[i]){
  7.           max=arr[i]; }
  8.     }
  9.     return max;
  10.   }
  11. //method that returns cube of the given number
  12.   public static int cube(int n){
  13.        return n*n*n;
  14.     }
  15.   }
  1. (5 points) Define the class CalculationTest a subclass of TestCase
  2. (10 points) Define a setUp() method
  3. (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class
  4. (5 points)Define a tearDown() method
  5. Bonus points (20 points ~1% of total grade):
    • (5 points) Define a test suite method
    • (10 points) Define a test method testcube that exercises Calculation.cube() in Calculation class
    • (5 points) Organize your tests in your test suite (testfindMax & testcube)

The language is Java

Solutions

Expert Solution

Program Code Screenshot :

CalculationTest.java

CalculationTestSuite.java

Output :

Program Code to Copy

Calculation.java

public class Calculation {
    //method that returns cube of the given number
    public int findMax(int arr[]){
        int max=0;
        for(int i=1;i<arr.length;i++){
            if(max<arr[i]){
                max=arr[i]; }
        }
        return max;
    }
    //method that returns cube of the given number
    public static int cube(int n){
        return n*n*n;
    }
}

CalculationTest.java

import junit.framework.TestCase;

public class CalculationTest extends TestCase {
    Calculation calculation;

    public CalculationTest(){

    }
    @Override
    public void setUp(){
        //Create a Calculation instance
        calculation = new Calculation();
    }

    public void testfindMax(){
        //Check for the maximums
        assertEquals(calculation.findMax(new int[]{}),0);
        assertEquals(calculation.findMax(new int[]{2,8,4,3}),8);
    }

    public void testCube(){
        //Check for cube test
        assertEquals(Calculation.cube(0),0);
        assertEquals(Calculation.cube(-5),-125);
        assertEquals(Calculation.cube(6),216);
    }

    @Override
    public void tearDown(){

    }
}

CalculationTestSuite.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import static org.junit.Assert.assertEquals;

@RunWith(Suite.class)

@Suite.SuiteClasses({
        CalculationTest.class
})

public class CalculationTestSuite {

    @Test
    public void testCube(){
        assertEquals(Calculation.cube(0),0);
        assertEquals(Calculation.cube(-5),-125);
        assertEquals(Calculation.cube(6),216);
    }
}

Related Solutions

GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
3. Given the test code below, what is the output to the console? public class TestMe{...
3. Given the test code below, what is the output to the console? public class TestMe{ public TestMe(){     System.out.print(“a”); } public void setUp(){     System.out.print(“b”); } public void tearDown(){     System.out.print(“c”); } @Test public void testX(){     System.out.print(“x”); } @Test public void testY(){     System.out.print(“y”); } } A. abxcbyc B. abxcabyc C. bxcbyc D. abxyc
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
Consider the following class and the main method below. Trace the code, then answer the questions...
Consider the following class and the main method below. Trace the code, then answer the questions on the right. public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int...
Given the following code, what is output by the method call, mystery(6 * 8)? public static...
Given the following code, what is output by the method call, mystery(6 * 8)? public static void mystery (int x[]) {     System.out.println("A"); } public static void mystery (int x) {     System.out.println("B"); } public static void mystery (String x) {     System.out.println("C"); } A B C CA CB Which of the following is true about overloaded methods? Java cannot use a method's return type to tell two overloaded methods apart. Java cannot use a method's parameters to tell two overloaded methods apart....
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
public class Sum2 { // TODO - write your code below this comment. } Download the...
public class Sum2 { // TODO - write your code below this comment. } Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4: Sum: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT