Tuesday, January 29, 2019

Write a Java program to find whether given number is Armstrong or not
Armstrong numbers are: 153, 370, 371, 407
Definition: We call a given number as Armstrong if it is equal to the sum of the powers of its own digits.
Ex: abcd...n => pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... =abcd...n
Answer:
package seleniumrepo;

import java.util.Scanner;
class ArmstrongNumber{
          public static void main(String args[]){
                int n, sum = 0, temp, r;
                Scanner in = new Scanner(System.in);
                System.out.println("Enter a number to check if it is an armstrong number");   
                n = in.nextInt();
                temp = n;
                while( n!= 0 ){
                       r = n%10;
                       sum = sum + r*r*r;
                       n= n/10;
                 }
                 if ( temp == sum )
                       System.out.println("Entered number is an armstrong number.");
                  else
                       System.out.println("Entered number is not an armstrong number."); 
   
                 in.close();
         }
}


Based on the type of data stored, data types are classifying as 3 types in Java.
  1. Primitive Datatype (Fundamental Datatype):
  2. Derived Datatype
  3. User Defined Datatype
Primitive Datatype (Fundamental Datatype)
The datatype which stores only single value is Primitive datatype. Primitive datatypes are also known as fundamental datatypes. In java programming language, there are 8 primitive datatypes classified into 4 categories.
  • Integer type category
  • Floating-point category
  • Character type category
  • Boolean type category
            Ex: int, float, char etc.,
Derived Datatypes: 
Derived datatypes are pre-defined datatypes given by the programming language. These derived datatypes are created by primitive datatypes and they can be used for storing multiple values.
            Ex: Array
User Defined Datatypes:
These datatypes will be created by the programmer according to application requirement, in which we can store any type of values and number of values.
            Ex: Class

1. Integer Category Data Types:
This category can be used for storing numbers which can be either +ve or -ve without decimal points.
We have 4 types under integer category. The size and range are as below:
 Data Type                               Size                                                 Range
  byte                                       1 Byte (2^8)                               -128 to 127
  short                                      2 Bytes                                       -32768 to 32767
  int                                          4 Bytes                              -2,147,483,648 to 2,147,483,647
  long                                       8 Bytes        -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

2. Floating-Point Category Data Types:
This category can be used for storing numbers which can be +ve or -ve with decimal points. We have two types under floating point category:
Data Type                               Size                                                 Range
  float                                       4 Bytes                                  1.4 e^-45 to 3.4 e^38
  double                                   8 Bytes                                  4.9e^-324 to 1.8 e^308

3. Character Type Category Data Type:
This category can be used for storing a single character. A character can be represented by an Alphabet or a digit or a special symbol.
This category contains only one datatype that is Char.
Data Type                               Size                                                 Range
  Char                                    2 Bytes                                             0 to 65535

4. Boolean Type Category Data Type:
This category can be used for storing either True or False. We have only one datatype under this category that is Boolean.
Data Type                               Size                                                 Range
  Boolean                          JVM Dependent                                 True or False

Difference between White Box Testing and Black Box Testing:
WBT: This is defined as method of testing in which one can perform testing on an application having internal structural knowledge of it. This is also known as Glass Box Testing.
  • This testing focuses on the structural part of an application and hence the Developers are involved in this type of testing.
BBT: This is defined as the method of testing in which one can perform testing on an application with much focus on functional part of it and ever without having an internal structural knowledge of application.

  • Actually the Test Engineers are involved in the BBT. 

Monday, January 28, 2019


                   package seleniumrepo;

                          public class ExtractNumerics{
                              public static void main(String[] args) {
                                      ExtractNumerics e=new ExtractNumerics();
                                      e.getInteger("!@#123HHGasd");
                              } 
                              public void getInteger(String str) {
                                     str=str.replaceAll("[^0-9]", "");
                                     System.out.println(str);
                             }
                         }

Sunday, January 27, 2019

we cannot iterate a Map directly using iterators, because Map are not Collection.
There are different methods to iterate through MAP elements:
Method 1: Using Iterator and keySet().
package seleniumrepo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
              Map<String,String> mp=new HashMap<String,String>();
              mp.put("1", "avinash");
              mp.put("2", "venu");
              mp.put("3", "dharma");
              mp.put("4", "hari");
              Iterator<String> it=mp.keySet().iterator();
              while(it.hasNext()){
                    String temp=it.next();
                    System.out.println(temp + " " +mp.get(temp));
              }
}
}

Method 2: Using foreach() method. It is defined as default method in the Map interface.

package seleniumrepo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
                Map<String,String> map=new HashMap<String,String>();
                map.put("1", "avinash");
                map.put("2", "venugopal");
                map.put("3", "dharma");
                map.put("4", "hari");
                map.forEach((k,v)->System.out.println("Key: "+k+" Value: "+v));
}

Method 3: Using entrySet() and Map.Entry(K, V).
package javaprograms;

import java.util.HashMap;

import java.util.Map;
public class TestIterator {
public static void main(String[] args) {
                    Map<String,String> map=new HashMap<String,String>();
map.put("1", "avinash");
map.put("70", "venugopal");
map.put("30", "dharma");
map.put("4", "hari");
System.out.println(map.values());
for(Map.Entry<String, String> entry: map.entrySet()) {
 System.out.println("Key: "+entry.getKey()+"\t"+ "Value: "+entry.getValue());
}
}
}

Output:
[avinash, hari, venugopal, dharma]
Key: 1 Value: avinash
Key: 4 Value: hari
Key: 70 Value: venugopal
Key: 30 Value: dharma

package seleniumrepo;

import java.util.Arrays;
/*
 * Check whether two strings are anagram of each other
 * Ex: LISTEN <=> SILENT, TRIANGLE <=> INTEGRAL, HEART <=> EARTH
 */
public class Anagram {
public static void main(String[] args) {
String s="LISTEN";
String s1="SILENT";
char[] ch=s.toCharArray();
char[] ch1=s1.toCharArray();
Arrays.sort(ch);
Arrays.sort(ch1);
if(Arrays.equals(ch, ch1)){
                   System.out.println("True - Given number is words are anagrom of each other");
}else{
                   System.out.println("False - Given number is words are not anagrom of each other");
              }
}
}

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *