Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help for Part1 and Part2, thanks so much! Part 1 Create a new Java project called 1410_RecursionJUnit. Add a package recursion and a
I need help for Part1 and Part2, thanks so much!
Part 1 Create a new Java project called 1410_RecursionJUnit. 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 Create a second source folder called test and again add a package recursion. Copy the following JUnit test classes into the package recursion in the test folder, and use them to test your code. CountSmilesTest.java SumOfDigitsTest.java D ToUpperTest.java 1. public static int sumOfDigits(int n) This method returns the sum of all the digits. Notice that the minus sign is ignored. sumOfDigits(-34) -> 7 sumOfDigits (1038) -> 12 2. 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 "HI THERE!" Hint: Class Character includes a method to UpperCase 3. public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing parenthesis starting at the specified index. An empty character array should return 0. [A,:,b,)] indicates a char array with 4 characters: A, colon, b, and a closing parenthesis. countSmiles ([:,), ,,i,f,e, ,i,s, ,8,0,0,d,,,)], 0) -> 2 countSmiles([a,:,b,(,,),:,,),e], 0)-> countSmiles([H, a,p.py, ,D, a,y, ,,),:,),:,),!], 0) -> 3 countSmiles([H, a,p,p,y, ,D,a,y, ,,),:,),:,),!], 11) -> 2 countSmiles([H, a,p,p,Y, ,D,a,y, ,,),:,),:,),!], 14) -> 1 Part 2 In this second part of the assignment, you will write JUnit5 tests for an existing method. Add a fourth static method to the class Recursion. Its name is harmonic, it has one parameter n of type int, and the return type is double. The method is supposed to do the following: If n is positive, the method should return the n-th harmonic number, which is calculated like this: 1/1 + 1/2 + 1/3 + ... + 1. If n is negative, the method should return the additive inverse of the n-th harmonic number. If n is zero, an IllegalArgumentException should be thrown. Rather than implementing the method, copy this implementation into your code. If you need to adjust the indentation, go to Eclipse > Source > Format. It might be able to fix the indentation for you. Add a JUnit test file called Harmoictest to the source folder called test. Write at least five different JUnit test methods to test the method harmonic using both valid and invalid input Choose the tests deliberately to provide thorough testing that uncovers as many potential problems as possible. Each of the five JUnit test methods should have a descriptive name that indicates what it is testing for Two things to consider: How to compare floating-point numbers Because of the way floating-point numbers are represented in Java, many numbers cannot be represented with full mathematical precision (e.g. 1/3) To account for that fact, JUnit provides an overloaded assertEquals method e that asserts that the expected and actual floating-point numbers are equal within a given delta. Use this overloaded method to test the method harmonic. To ensure consistency throughout your test class, declare a private final field called DELTA and assign it one billionth. How to test for exceptions: In JUnits, we use lambda expressions to test for exceptions. We'll cover lambda expressions later in the course. All you need to know for now is this special case which has the following format: assertThrows(IllegalArgumentException.class, () -> { // call here the method to trigger the exception }); Notice, that the code snippet above calls the static method assertThrows from class Assertions without explicitly calling it on the type. That works if you included the following static import statement, which was also used in the other three JUnit test classes. import static org. junit.jupiter.api. Assertions.*; Add a fourth static method to the class Recursion. Its name is harmonic, it has one parameter n of type int, and the return type is double. Method harmonic public static double harmonic (int n) { if (n == 0) throw new IllegalArgumentException ("The argument n can't be zero."); if (n == 1) return 1; else if (n 1 return 1.0 + harmonic (n 1); } and invalid input Choose the tests deliberately to provide thorough testing that uncovers as many potentialStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started