Posts

Showing posts with the label JUnit

Java program to return the first non repeated character in a string

Image
This post is about java program to return the first non repeated character in a string. Example: Given a string "hhello", It should return 'e' as the first non repeated character. Logic of the program: Using a linked hash map will solve this program. All the characters[a-z] can be stored in a linked hash map as key and value should be corresponding to be number of occurrence of each character.  To return the first non repeated char, just iterate the linked hash map and return first key having value as 1. Program: package com.techjourney; import java.util.LinkedHashMap; import java.util.Map; /** * Created by Sujith Mohan on 6/4/2015. */ public class FirstNonRepeatedChar { public static char returnFirstNonRepeatedChar(String str) { if (str== null ) throw new NullPointerException(); if (str.equals( "" )) return '\0' ; LinkedHashMap<Character, Integer> map = ...

Java program to check if a string is pangram

Hi everyone, in this blog, I will be writing about a java program to check whether a string is pangram or not. Pangrams are words or sentences containing every letter of alphabet at least once. Most commonly used example of pangram is " A quick brown fox jumps over the lazy dog. " Logic of the program Initialize a string testString as "abcdefghijklmnopqrstuvwxyz" Loop the input string for every character, and replace those character in testString with empty character. If testString is an empty string, then input string is Pangram The program for pangram is as follows: package com.blog.techjourney; public class Pangram { public static Boolean checkForPangram(String str) { int len = str.length(); //test string for testing all characters are present in the given string String testString = "abcdefghijklmnopqrstuvwxyz" ; for ( int i=0;i<len;i++) { char chars =str.charAt(i); //Converting the char to String a...

Sample test case program using JUnit

Hi Everyone, in this post, I will be writing a sample case using JUnit. JUnit is an unit testing framework for java application. JUnit has been important in developement of test driven developement, and is one of a family of unit testing framework which is collectively known as xUnit that is originated from sUnit. Here, I am writing a sample test case program for the program conversion of String to Int in Java.  Before starting the program we have to download the jar file of JUnit and put it in the lib folder. The program is as follows: package com.program; import com.program.*; import static org.junit.Assert.*; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import com.program.InvalidNumberException; import com.program.StringToInt; public class StringToIntTestCase { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("SetUp BeforeClass...

Program to find the second largest element in an array

This program helps you to find the second largest element in an array. There are many methods by which we can find the second largest element. I am using a method which has the lowest time complexity - O(n). Here we are first finding the largest element in an array by traversing each element. In the next iteration, we are checking whether each element is less than the largest element and thus assigning its value to second largest element. There are some other methods like sorting the array and traversing from n-2 th element to 0th element to check whether it is different from n-1 th element and returning the value. This will take a time complexity of O(n logn). package com.programs; /* * Java program to get the second largest element in an array */ public class secondLargest { public static int findSecondLargest( int arr[]) { int length = arr.length; int large, slarge, flag = 0; large = arr[0]; slarge = arr[0]; for ( int i = 0; i < length; i++) ...