Showing posts with label software. Show all posts
Showing posts with label software. Show all posts

Thursday, 14 November 2013

Random integers between two values (a range) in java

Refer to : http://mycodedock.blogspot.in/2013/11/random-integers-in-java.html

Before we go to it, look at the code below. It generates 100 random numbers between 0 and 50 using randomObject.nextInt(int x) function.



Note that the the range of numbers generated is inclusive of 0 but exclusive of 50 (upper limit).

Now, let us modify this program to generate random numbers between two values. What if we want to generate numbers between two ranges? Let us say range is from 100 to 1000.

We will use something like x = MINVAL + RandomObj.nextInt(MAXVAL - MINVAL)

Analyze this code:



Note that all numbers are between 100(inclusive) and 1000(exclusive). This is pretty much what we wished to achieve.

| Advertisement |

Random integers in java

java.util.Random provides randomObject.nextInt() function to generate random integers. Lets explore how this can be used.

Example to generate 100 random integers and print them:


Note that in java integers are 32 bits long. Which means we can have 232 integers. Hence, we have 4294967296 integers which range from -2147483648 to +2147483647 (including 0).

| Advertisement |

Tuesday, 12 November 2013

Function over-loading in Java - part 2

Refer to : http://mycodedock.blogspot.in/2013/11/function-over-loading-in-java-part-1.html

Now, we wish to create a function called calcAns() that takes in two integers - int a, int b and returns a*b. However, at times we also need to calculate answer for three integers - int a, int b, int c which should return a*b*c.

Since the nature of both these functions is very similar, it would be very handy if we could use the same function name for both these functions. Is it possible? Lets find out!


Note that the compiler automatically decides which *version* of calcAns() to use when you call it depending upon number of arguments.

Thumb rule for using function over-loading

  • The functions much have same names
  • The functions must differ either by number of arguments or type of arguments or both (else it would be redundant)
  • The functions may have different return types


| Advertisement |

Function over-loading in Java - part 1

Let us suppose we wish to create a function myFunc() that can take in three integers as arguments - int x, int y, int z and returns (x*y)+z. It is fairly easy to write such a function. But what if later on we wish to give the same sort of functionality for three arguments of type - double, lets say - double a, double b, double c. Can this be achieved without creating a function with a new name?

Lets have a look at the code given below -


Note that the compiler automatically detects which *version* of the method to use when you call myFunc() depending upon the type of arguments.

| Advertisement |

Monday, 11 November 2013

Constructor overloading in Java

The task is to create a class called UselessClass which holds two variables int x and double y. If a class is initialized with a passed value, then it must be set in these variable while creating an instance. Else it sets the variables to zero.

The example code in Java7 is given below:




Note that UselessClass(2,4.567) and UselessClaass() are different constructors even though they have the same name. This is the concept of constructor-overloading.

While creating obj1 the first constructor over-loaded the second constructor.
While creating obj2 the second constructor over-loaded the first constructor.

The thumb-rule for using constructor over loading -

  • Name of constructors must be same as the name of the class
  • The parameters of constructors must be different else they become redundant.


| Advertisement |

Friday, 8 November 2013

Getting number of days between two dates in Java

The task is to design an application in Java7 that can read two dates in YYYY MM DD form and then compute & print the number of days between them.

The code for the application is given below


| Advertisement |

Wednesday, 6 November 2013

Dynamic linked list in java

Refer to : http://mycodedock.blogspot.in/2013/11/linked-list-in-java.html

Now, java.util.LinkedList can also be applied to custom data structures (classes) as the following example demonstrates(Java7)


| Advertisement |

Linked List in java

Java provides java.util.LinkedList library for creating Linked Lists.

This example shows how to use it (Java7)


| Advertisement |

Saturday, 12 October 2013

Insertion Sort Java

Insertion Sort example in Java.
The tool randomly picks up 50 integers and then sorts them using Insertion Sort algorithm.

The code for tool is given below (Java7) :
| Advertisement |

Validating date (DD/MM/YYYY) Java

The task is to create a tool in Java that takes a string as an input and validates if it is in date format (dd/mm/yyyy).

The source-code for the tool (Java 7) :

| Advertisement |

Validating an e-mail string Java

The assignment is to write code that validates if the input string can be an e-mail.

The code for tool is given below (Java7) :
| Advertisement |

Friday, 11 October 2013

Validate password string Java

The task is to create a tool in Java that takes a string as an input and validates if it can be a password.

The source-code for the tool (Java 7) :

| Advertisement |

Validate a username string Java

The task is to create a tool in Java that takes a string as an input and validates if it can be a username.

The source-code for the tool (Java 7) :

| Advertisement |

Finding a numeric string from an alphanumeric string Java

The assignment is to develop a tool in Java that takes an alphanumeric string as input and finds numeric sub-strings.

Simply, we need to print all the indexes where the numeric strings occurs in the alphanumeric string.

The source-code for the tool (Java 7) :
| Advertisement |

Wednesday, 9 October 2013

Halstead analysis tool in java

Read : http://en.wikipedia.org/wiki/Halstead_complexity_measures

The assignment is to write a small program that takes a source code file as an input, identifies n1, n2, N1 & N2; and then prints all Halstead's metrics as output.

Language of the source code (input file) : C

Language used to develop the tool : Java7

Source-code for the tool (Java7) :


Source-code for the input file (C) :


| Advertisement |