0% found this document useful (0 votes)
7 views

Junit_Assignment24

Uploaded by

rajmudhiraj903
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Junit_Assignment24

Uploaded by

rajmudhiraj903
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.

company;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Algo {


public boolean isPrime(int n){
boolean isPrime = true;
for(int i = 2 ; 2*i < n ; i++){
if(n%i == 0){
isPrime = false;
}
}
return isPrime;
}

public boolean isPerfect(int x) {


int sum = 0;
for(int i = 1; i<=(x/2); i ++) {
if(x%i == 0) {
sum = sum + i;
}
}
if(sum == x) {
return true;
}else {
return false;
}
}

//Capitalize the first letter and make the rest lowercase


//Capitalize the first letter and make the rest lowercase
public static String capitalizeWords(String s) {
// Trim leading and trailing spaces and split by one or more spaces

if (s != null) {

if(s==""){
return "no input";
}

String[] words = s.trim().split("\\s+");


StringBuilder capitalizedString = new StringBuilder();

for (String word : words) {


if (word.length() > 0) {
// Capitalize the first letter and make the rest
lowercase
String capitalizedWord =
Character.toUpperCase(word.charAt(0))
+ word.substring(1).toLowerCase();
capitalizedString.append(capitalizedWord).append(" ");
}
}
// Convert StringBuilder to String and trim the trailing space
return capitalizedString.toString().trim();
} else {
return "String is null, cannot perform operations.";
}
}

/* There is a biker going on a road trip. The road trip consists of n +


1 points at different altitudes. The biker starts his trip on point 0 with
altitude equal 0.
You are given an integer array gain of length n where gain[i] is the net
gain in altitude between points i and i + 1 for all (0 <= i < n). Return the
highest altitude of a point.

Example 1:
Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1 */
public static int calculateHighestAltitude(int[] gain) {
int currentAltitude = 0;
int highestAltitude = 0;

// Calculate altitudes based on gain


for (int altitudeGain : gain) {
currentAltitude += altitudeGain; // Update current altitude
highestAltitude = Math.min(highestAltitude, currentAltitude); //
Update highest altitude
System.out.print(" " + altitudeGain);
}
return highestAltitude;
}

/*
For two strings s and t, we say "t divides s" if and only if s = t + t +
t + ... + t + t (i.e., t is concatenated with itself one or more times).
Given two strings str1 and str2, return the largest string x such that x
divides both str1 and str2.
Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"

Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
*/
public String gcdOfStrings(String str1, String str2) {
String output = "";
if(!(str1+str2).equals(str2+str1)){
return output;
}else{
BigInteger b1= new
BigInteger(String.valueOf(str1.length()));
BigInteger b2= new
BigInteger(String.valueOf(str2.length()));
BigInteger val = b1.gcd(b2);
output = str1.substring(0, val.intValue());
}
return output;
}
/*
You are given an integer array nums consisting of n elements,
and an integer k. Find a contiguous subarray whose length is
equal to k that has the maximum average value and return this
value. Any answer with a calculation error less than
10-5 (ten to the power of negative five) will be accepted.

Example 1:

Input: nums = [1,12,-5,-6,50,3], k = 4


Output: 12.75000
Explanation: Maximum average is
(12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Example 2:

Input: nums = [5], k = 1


Output: 5.00000
*/

public double findMaxAverage(int[] nums, int k) {


double sum = 0;
for(int i = 0; i < k; i++){
sum = sum + nums[i];
}
System.out.println("sum : " + sum);
double avg = sum/k+1;

for(int j = 1; j < nums.length - k + 1; j++){


sum = sum - nums[j-1] + nums[j+k-1];
if(avg < sum/k){
avg = sum/k;
}
System.out.println("avg : " + avg);
}
return avg;
}

public int findLast(int[] x, int y) {


//Effects : if x = null throw nullPointerExceptions
//else return the index of the last elements
// in x that equals y
// if no such element exists, return -1
for(int i=x.length-1 ; i >0 ; i--) {
if(x[i] == y) {
return i;
}
}
return -1;
}

public List<String> getTodos(List<String> list) {


List<String> filteredList = new ArrayList<String>();
for (String topic : list) {
if (topic.contains("coverage")) {
filteredList.add(topic);
}else{
filteredList.add("out of topic");
}

}
return filteredList;
}

1. Design test cases for all operations in the class


2. Create the “algo” instance(object) for testing by using “@Before” or “@BeforeEach”
annotation.
3. Write Junit testing code for each operation (@Test)
4. Execute all test case with coverage and capture the screenshot of testing results
5. The method percentage should be 100%
6. The Line coverage should be more than 90%
7. You can use “assertEquals” or “assertThat” type operation

IntelliJ

You might also like