Question

In: Computer Science

Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...

Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment.

  1. public static int sumOfDigits(int n)
    This method returns the sum of all the digits.
    sumOfDigits(-34) -> 7
    sumOfDigits(1038) -> 12
  2. public static int countSmiles(char[] letters, int index)
    This method counts the number of colons followed by a closing parenthesis.
    countSmiles([:,), ,L,i,f,e, ,i,s, ,g,o,o,d, ,:,)], 0) -> 2
    countSmiles([H,a,p,p,y, ,D,a,y, ,:,),:,),:,),!], 0) -> 3
    countSmiles([a,:,b,(,c,),:, ,),e], 0) -> 0
  3. public static String toUpper(String str)
    This method separates all characters by a space and changes all lowercase letters to uppercase letters.
    E.g. "Hi there!" returns "H I T H E R E !"
    Hint: Class Character (Links to an external site.) includes a method toUpperCase (Links to an external site.)​

Create a second source folder called test. It should include a class RecursionTest. That's where you will write the JUnit tests for the three methods above.

  • Each of the three methods should have at least eight corresponding JUnit tests.
  • Choose test data deliberately to provide thorough testing that covers as many potential problems as possible.

Once you have written the JUnit tests, implement the methods.

Solutions

Expert Solution

Recursion.java


public class Recursion {
        
        public static int sumOfDigits(int n) {
                // we cannot sum -ve elements so calculating absolute
                n = Math.abs(n);
                // if n is 0 then return 0
                // else calculate sum of digits
                if(n==0)
                        return 0;
                return n%10+sumOfDigits(n/10);
                
        }
        
        public static int countSmiles(char[] letters, int index) {
                
                // if index is 0 then we cannot count further 
                if(index==letters.length)
                        return 0;
                
                // checking index if it is less than length of letters
                // and checking for smile characters
                if(index+1 < letters.length && 
                                letters[index]==':'&& letters[index+1]==')') {
                        
                        // if smile found add 1
                        return 1 + countSmiles(letters, index+2);
                }else {
                        return countSmiles(letters, index+1);
                }
                
        }
        
        public static String toUpper(String str) {
                
                // if length is 1 return the upper case 
                if(str.length()==1) {
                        return str.toUpperCase();
                }
                // converting to upper case character
                char upper =Character.toUpperCase(str.charAt(0));
                
                // concatenating recursively
                return upper+" "+toUpper(str.substring(1,str.length()));
                
                
        }
}

RecursionTest.java

import static org.junit.Assert.*;
import org.junit.Test;

public class RecursionTest {

        @Test
        public void DigitsSumTest() {
                
                int sum;
                
                // Test1
                // returns the calculated sum
                sum = Recursion.sumOfDigits(-34);
                // tests whether the sum is equal
                assertEquals(7, sum);
                
                // Test 2
                sum = Recursion.sumOfDigits(1038);
                assertEquals(12, sum);
                
        }
        
        @Test
        public void CountSmilesTest() {
                int count;
                char[] ch;
                
                // Test 1
                // character array
                ch = new char[] {':',')',' ','L','i','f','e',
                                ' ' ,'i','s',' ' ,'g','o','o','d',' ',':',')'};
                // returns the calculated counted smiles
                count = Recursion.countSmiles(ch, 0);
                assertEquals(2, count);
                
                // Test 2
                ch = new char[] {'H','a','p','p','y',' ','D',
                                'a' ,'y',' ',':' ,')',':',')',':',')','!'};
                count = Recursion.countSmiles(ch, 0);
                assertEquals(3, count);
                
                // Test 3
                ch = new char[] {'a',':','b','(','c',')',':',' ' ,')','e'};
                count = Recursion.countSmiles(ch, 0);
                assertEquals(0, count);
                
        }
        @Test
        public void UpperTest() {
                String s;

                // test 1
                s= "Hi there!";
                
                // returns the upper case letter
                s = Recursion.toUpper(s);
                // tests whether it is same
                assertEquals(s,"H I   T H E R E !");
                
        }
        
}

Code

Recursion.java

RecursionTest.java

Output


Related Solutions

Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...
In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package csci3444.generics and put all code in it) Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods; public K getKey(); public V getValue(); Create a generic class called “MyGenClass” that implements “MyGenInterface” interface. has attributes “K key” and “V value” that can be inherited has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama...
Problem 1 Create a new project called Lab7 Create a class in that project called ListORama Write a static method in that class called makeLists that takes no parameters and returns no value In makeLists, create an ArrayList object called avengers that can hold String objects. Problem 2 Add the following names to the avengers list, one at a time: Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark Print the avengers object. You will notice that the contents are displayed in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT