Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, 18 June 2013

Date Difference Java

This example learns how to make the difference between two dates and how to convert milliseconds into seconds, seconds into minutes, minutes into hours, hours into days.

Here we are using Calendar, an abstract base class that extends Object class and makes a difference between a Date object and a set of integer fields. Calendar class provides a getInstance()  method for returning a Calendar object whose time fields have been initialized with the current date and time.

The methods used:
setTimeInMillis(long millis): This method is used to set current time in calendar object.
getInstance(): This method is used to get a calendar using the default time zone, locale and current time.

Generating Random Number In A Range With Java

One standard pattern for accomplishing this is:
Min + (int)(Math.random() * ((Max - Min) + 1))

The java Math library function Math.random() generates a double value in the range [0,1).
Notice this range does not include the 1.In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered.
. 
Math.random() * ( Max - Min )
This returns a value in the range [0,Max-Min).
For example if you want [5,10] you need cover 5 integer values so you use
Math.random() * 5
This would return a value in the range [0,5)
Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.
Min + (Math.random() * (Max - Min))
You now will get a value in the range [Min,Max). Following our example, that means [5,10):
5 + (Math.random() * (10 - 5))
But, this is still doesn't include Max and you are getting a double value. In order to get the Max value included, you need to add 1 to your range parameter (Max - Min) and then truncate the decimal part by casting to an int. This is accomplished via:
Min + (int)(Math.random() * ((Max - Min) + 1))
And there you have it. A random integer value in the range [Min,Max], or per the example [5,10]:
5 + (int)(Math.random() * ((10 - 5) + 1))

Is Java “pass-by-reference”?

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d = new Dog("Fifi");
  d.name.equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Max"); // true

Java is always pass-by-value

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d = new Dog("Fifi");
  d.name.equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Max"); // true

Java Interpreter/Lexical Analyzer

This project will be to write an interpreter for a small language with the grammar I have attached. This language has only 1 data type, integer, and the only identifiers are single letter (i.e. there are only 26 possible identifiers). 

This language has the following properties: not case sensitive must have white space (blank characters, tab characters, and end of line characters) between every lexeme The interpreter will parse a program in this language and build some intermediate data structures. 

These data structures will then be interpreted to execute the program. 
The parsing algorithm should detect any syntactical or semantic error. The first such error discovered should cause an appropriate error message to be printed, and then the interpreter should abort. Run-time errors should also be detected with appropriate error messages being printed.

How TO Compare Strings in Java


== tests for reference equality.
.equals() tests for value equality.

Consequently, if you actually want to test whether two strings have the same value you should use.equals() (except in a few situations where you can guarantee that two strings with the same value will be represented by the same object eg: String interning).

== is for testing whether two strings are the same object.

HashMap Vs HashTable

HashMap and HashTable both implements Map interface but their are some difference between HashMap and HashTable.
1. HashMap is not synchronized whereas HashTable is synchronized.
2. HashMap allows one null key and any number of null values but HashTable does not allow null keys or values.
3. Iterator in HashMap is fail fast but Enumeration in HashTable is not fail fast.