Java Program which Required for Testing Engineer

----------JAVA Program starts here:-------------

  1. WAP to reverse a String without using inbuilt function.
  2. WAP  to reverse a String using inbuilt function.
  3. WAP to reverse words at ODD place in a sentence.
  4. WAP to check prime number (1-100)
  5. WAP to check the ARMSTRONG number.
  6. WAP to check the Fibonacci Series.
  7. A. WAP to print using DUPLICATES using HashMap   & 
    1.  Duplicates Word (Using ForEach loop)
  8. WAP to do sorting of ARRAY USING for loop   & 
    1. Descending order
    2. The only Odd & Only Even
  9. WAP for GETTER & SETTER method.
  10. WAP to generate pattern.
    1. Pattern Problem- Star at ODD place 
    2. Pattern Problem- Triangle
    3. Pattern Problem- Reverse Triangle
    4. Pattern Problem - Pyramid
  11. WAP to prog to swap to Strings w/o using third variable.
  12. Singleton class example using getInstance()
  13. Program to return Min & Max Value from the Array 
------------------------------------------------------------------------------------------------

1. WAP to reverse a String without using inbuilt function. :
       Find the program to do reverse a string :



public class reverseString {
static String revTheString (String str) {
String strValue = "";

 String[] st = str.split("");    // To convert into String array string[]length --st.length().

                  //  char[] st = str.to CharArray(); // Tp convert into char array  char [] length --st.length().

 
                 for(int i = str.length()-1 ; i >=0 ; i--) {
strValue = strValue + st[i]; 

}
return strValue ;
}

public static void main (String [] args) {

System.out.println(" The reverse string is :"+revTheString("Hello World"));

}
}

public class ReverseAstring {
public static String revMethod(String sentence){
String[] st = sentence.split("");
String revString = "";
for(int i=st.length-1; i >= 0 ; i--){
revString = revString + st[i];
}
return revString;
}
public static StringBuffer revString(String st){
StringBuffer sb = new StringBuffer(st);
return sb.reverse();
}
public static void main(String[] args){
// System.out.println(" The rev string is :"+revMethod("AnkurMalviya"));
System.out.println(" The rev string is :"+revString("helloworld"));
}
}
 The rev string is :dlrowolleh


2. WAP using StringBuffer  / inbuilt function to reverse a String:

public class reverseString {
static StringBuffer revTheString (String str) {

StringBuffer sb = new StringBuffer(str);

return sb.reverse() ;
}
public static void main (String [] args) {
System.out.println(" The reverse string is :"+revTheString("Hello World"));
}
}


3. WAP to reverse words at ODD place in a sentence.

package RevProg;

public class RevAtOddPlace {
String origValue = "";
private static String getRev(String str) {
String evenValue = "";
char[] ch = str.toCharArray();
for(int i = ch.length -1 ; i >= 0 ; i --) {
  evenValue = evenValue + ch[i];
}
return evenValue;
}
private String getRevOddPlace(String sentence) {
String initValue = "";
String [] st =  sentence.split(" ");
for (int i = 0 ; i <= st.length-1 ; i ++ ) {
if(i%2 == 0) {
initValue = st[i];
}
else {
initValue= getRev(st[i]);
}
origValue = origValue + " " + initValue;
}
return origValue;
}

public static void main (String[]args) {
RevAtOddPlace rv = new RevAtOddPlace();
System.out.println("The rev value is: "+rv.getRevOddPlace("Hello world this is demo"));
}
}

Expected O/P: Hello dlrow this si demo



4. WAP to check prime number (1-100) 
 
Prime Number: The number which is not equal to 0 & 1. And which is divided by 1 & ITSELF only known as Prime Number. 
The number which is the multiplication of two smaller number called COMPOSITE NUMBER.
 
a % b (Gives % is remainder ) : 2 % 1 = 0
a / b (Givers / is modulus)       : 2 / 1 = 2
 
package ArrayStuff;

public class CheckPrimeNumber {
    public static void main(String[] args) {

        String PRIME_NUMBER = "";
        for (int i = 1; i <= 100; i++) {
            int counter = 0;

            for (int num = i; num >= 1; num--) {

                if (i % num == 0) {

                    counter = counter + 1;
                }
            }
            if (counter == 2) {
                PRIME_NUMBER = PRIME_NUMBER + i + "   ";
            }
        }
        System.out.println(" The prime number :" + PRIME_NUMBER);

    }


5. WAP to check the ARMSTRONG number.

>>ARMSTRONG  : Is the number , If  abc =Sum[ (a*a*a) + (b*b*b) + (c*c*c) ]

public class ArmstrongProg {

private static void getArmstrongNumberCheck(int num) {
int b = 0, a, n, temp;
temp = num;
n = num;
while (n > 0) {
a = n % 10;
n = n / 10;
b = b + (a * a * a);
}
System.out.println(" the value is b :" + b);
if (temp == b) {
System.out.println(" Its Armstrong Number :" + temp);
} else {
System.out.println(" Its not Armstrong number :" + temp);
}
}

public static void main(String[] args) {
getArmstrongNumberCheck(111);   // NOT 
getArmstrongNumberCheck(407);  // YES
}
}

6. WAP to check the Fibonacci Series.


public class FibonacciSeries {

public static void getFibonacciSeries(int count, int n1, int n2) {

System.out.print(n1 + "  " + n2);
int n3 = n2;
for (int i = count - 1; i >= 1; i--) {

n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print("  " + n3);
}
}

public static void main(String[] args) {
getFibonacciSeries(10, 0, 1);         // 0  1  1  2  3  5  8  13  21  34  55
}
}


7.A.  WAP to print duplicates :  USING HASHMAP

package RevProg;

import java.util.HashMap;
import java.util.Map;

public class DuplicatesValue {

public static void getDuplicates(String str) {
str = str.replace(" ", "");
System.out.println(str);
String[] stArray = str.split("");
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for (int i = 0; i <= stArray.length - 1; i++) {

if (hm.containsKey(stArray[i])) {
hm.put(stArray[i], hm.get(stArray[i]) + 1);
} else {
hm.put(stArray[i], 1);
}

}
// System.out.println(hm);
for (Map.Entry<String, Integer> hmNew : hm.entrySet()) {
if (hmNew.getValue() > 1) {
System.out.println("The duplicates ate :" + hmNew.getKey());

}

}
}

public static void main(String[] args) {
getDuplicates("Am ank am ank ankm hm");
}

}

**** Using toCharArray()****

import java.util.HashMap;
import java.util.Map;

public class Duplicates {

public static void getDuplicate() {

String s = "I am ankur Malviya and I am an Indian";
char [] st = s.toCharArray();
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i <= st.length - 1; i++) {
if (hm.containsKey(st[i])) {
hm.put(st[i], hm.get(st[i]) + 1);
} else {
hm.put(st[i], 1);
}
}
HashMap<Character, Integer> hmm = new HashMap<Character, Integer>();
for (Map.Entry<Character, Integer> e : hm.entrySet()) {
if (e.getValue() > 1) {
hmm.put(e.getKey(), e.getValue());
}
}
System.out.println(" The duplicates are :" + hmm);
}

public static void main(String[] args) {
getDuplicate();
}
}


7. 1.
import java.util.HashMap;
import java.util.Map;

public class DuplicateStrings {

static HashMap<String, Integer> getDuplicates(String inputSentence){
// String rawString = inputSentence.replaceAll(" ","");
String[] stringArray = inputSentence.split(" ");
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(String eachWord : stringArray){
if(hm.containsKey(eachWord)) {
hm.put(eachWord,hm.get(eachWord)+1);
}else{
hm.put(eachWord,1);
}
}
System.out.println("the value :"+hm);
HashMap<String,Integer> hmm = new HashMap<String,Integer>();
for(Map.Entry<String,Integer> e : hm.entrySet()){
if(e.getValue()>1){
hmm.put(e.getKey(),e.getValue());
}
}
return hmm;
}
public static void main(String[] args) {
System.out.println("The duplicates strings are: "+getDuplicates("name name Hello name is my is is is is is is name name in the worl and my name is ankur malviya"));
}
}
8. WAP to do sorting of ARRAY USING for loop 


public class SortingArray {

int[] numArray = { 10, 8, 0, 12, 20, 0, 2, 3 };

public static void getSorted(int[] num) {
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if (num[i] > num[j]) {
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
System.out.print(num[i] + " ");
}
}

public static void main(String[] args) {
SortingArray s = new SortingArray();
getSorted(s.numArray);
}
}

O/p : 0 0 2 3 8 10 12 20

8. 1 Descending Order
public class descendingOrderString {
static void descendArrray(int [] num){
int temp ;
for(int i = 0;i<=num.length-1;i++){

for(int j=i+1;j<=num.length-1;j++){

if(num[i]>num[j]){

temp = num[j];
num[j] = num[i];
num[i] = temp;

}
}
}

for( int t=0; t < num.length-1 ; t ++){
System.out.print(num[t]+", ");
}
System.out.print(num[num.length-1]);
}

public static void main(String[] args) {
descendArrray(new int[]{20,2,3,10,8,100,3,2,20,20,10});
}
}

O/P: 2, 2, 3, 3, 8, 10, 10, 20, 20, 20, 100
8.2 The Only Odd and Only Even 
import java.sql.SQLOutput;

public class getODDSeparateNumbers {
static void getOdd(int [] num){
int temp ;
for(int i = 0;i<=num.length-1;i++){

for(int j=i+1;j<=num.length-1;j++){

if(num[i]>num[j]){

temp = num[j];
num[j] = num[i];
num[i] = temp;

}
}
}

for( int t=0; t < num.length-1 ; t ++){
System.out.print(num[t]+", ");
}
System.out.print(num[num.length-1]);
System.out.println();
System.out.println("the only odds");
for( int t=0; t < num.length ; t ++) {
if (num[t] % 2 != 0) {
System.out.print(num[t]+", ");
}
}
System.out.println();
System.out.println("the only evens");
for( int t=0; t < num.length ; t ++) {
if (num[t] % 2 == 0) {
System.out.print(num[t]+", ");
}
}
}
public static void main(String[] args) {
getOdd(new int [] {1,13,4,1,2,3,0,1,2,13,6,10,5,8,3,1,12,17,18,10,12,20});
}
}
0, 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 8, 10, 10, 12, 12, 13, 13, 17, 18, 20
the only odds
1, 1, 1, 1, 3, 3, 5, 13, 13, 17, 
the only evens
0, 2, 2, 4, 6, 8, 10, 10, 12, 12, 18, 20, 
9. WAP for GETTER & SETTER method.



With correct brand:




10. WAP to generate pattern 1:

public class PatternOne {
private static void getPattern(int line) {
for (int i = 1; i <= line; i++) {

for (int j = line; j >= 1; j--) {
if (j == i) {

System.out.print("*" + " ");
} else {
System.out.print(j + " ");
}

}
System.out.println();

}
}

public static void main(String[] args) {
getPattern(5);
}
}

o/p
2.  Pattern Problem- Triangle
public class PatternProblemTriangle {
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
for(int j = i ; j < 10 ; j ++){
System.out.print("*");
}
System.out.println();
}
}
}
**********
*********
********
*******
******
*****
****
***
**
*

3. Pattern Problem- Reverse Triangle

public class PatternProblemReverseTriangle {
public static void main(String[] args) {
for(int i = 0 ; i < 10 ; i++){
for(int j = 10-1 ; j > i ; j --){
System.out.print(" ");
}
for(int j=i; j>=0; j--){
System.out.print("*");
}
System.out.println();
}
}
}
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
4. Pyramid 

public class PairamidTrianglePattern {
public static void main(String[] args) {
for(int i = 10-1 ; i > 0 ; i --){
for(int j = i ; j > 0 ; j --) {
System.out.print(" ");
}
for(int j = 0 ; j < 2 * (10 - i ) - 1 ; j ++){
System.out.print("*");
}
System.out.println();
}
}
}
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************




11. WAP to prog to swap to Strings w/o using third variable.

public class getSwap {


String a = "";

String b = "";

private static void getSwap (String a, String b) {

a = a + b;

b = a.substring(0, a.length()-b.length());

a = a.substring(b.length());

System.out.println("The new a : "+a+" and the new b is: "+b);

}

public static void main(String[] args) {

getSwap("Ankur","Malviya");


}


}

12. Singleton example using getInstance():

package com.test;

public class SingletonExample {
private static SingletonExample singleton_instance = null;
public String str;
private SingletonExample(){
str = "Hello There...";
}

public static synchronized SingletonExample getInstance(){
if(singleton_instance == null){
singleton_instance = new SingletonExample();
}
return singleton_instance;
}
}


package com.test;

public class SingletonExmChild {
public static void main(String[] args) {

SingletonExample x = SingletonExample.getInstance();
SingletonExample y = SingletonExample.getInstance();
SingletonExample z = SingletonExample.getInstance();

x.str = (x.str).toLowerCase();
System.out.println(" The new Str is : "+z.str);
if(x==y && y==z){
System.out.println(" All x, y & z instance pointing to same obj location x: "+x.hashCode() + " y: "+y.hashCode()+ " and z : "+z.hashCode());
}else{
System.out.println(" All dont have same obj location...");
}


}
} _-------------------- OUTPUT The new Str is : hello there... All x, y & z instance pointing to same obj location x: 168423058 y: 168423058 and z : 168423058



13. Program to return Min & Max Value from the Array 

public class NewMinMaxArray {

public static void main(String[] args) {
int[] numArray = {3, 20, 2, 12, 1, 0, 10};
getMinMax(numArray);
}

public static void getMinMax(int[] numArray){

if(numArray.length==0||numArray== null){
System.out.println("The Array is Null...");
}

int min = numArray[0];
int max = numArray[0];
System.out.println("The initial defined value from array[0] is Min :"+min +" and the max :"+max);

for(int i = 0 ; i < numArray.length ; i ++) {
if (numArray[i] < min){
min = numArray[i];
}else if (numArray[i] > max){
max = numArray[i];
}
}
System.out.println(" The Sorted Final Min :"+min+ " and the max :"+max);
}
}



New Program here...



Comments

Popular posts from this blog

The self healing automation framework - Healenium Updated Dec2023

Heleanium - Web without using Docker