Posts

Showing posts from December, 2014

Generates Random Integers in a Specific Range

import java.util.Random; /** Generate random integers in a certain range. */ public final class RandomRange {     public static final void main(String... aArgs){     log("Generating random integers in the range 1..10.");         int START = 1;     int END = 10;     Random random = new Random();     for (int idx = 1; idx <= 10; ++idx){       showRandomInteger(START, END, random);     }         log("Done.");   }     private static void showRandomInteger(int aStart, int aEnd, Random aRandom){     if (aStart > aEnd) {       throw new IllegalArgumentException("Start cannot exceed End.");     }     //get the range, casting to long to avoid overflow problems     long range = (long)aEnd - (long)aStart + 1;     // compute a fraction of the range, 0 <= frac < range     long fraction ...

Generates Random Floating Point Numbers

import java.util.Random; /**  Generate pseudo-random floating point values, with an  approximately Gaussian (normal) distribution.  Many physical measurements have an approximately Gaussian  distribution; this provides a way of simulating such values. */ public final class RandomGaussian {   public static void main(String... aArgs){     RandomGaussian gaussian = new RandomGaussian();     double MEAN = 100.0f;     double VARIANCE = 5.0f;     for (int idx = 1; idx <= 10; ++idx){       log("Generated : " + gaussian.getGaussian(MEAN, VARIANCE));     }   }     private Random fRandom = new Random();   private double getGaussian(double aMean, double aVariance){     return aMean + fRandom.nextGaussian() * aVariance;   }   private static void log(Object aMsg){     System.out.println(String.valueOf(aMsg));   } } An example run of this class: Generated...

Generate random numbers

import java.util.Random; /** Generate 10 random integers in the range 0..99. */ public final class RandomInteger {     public static final void main(String... aArgs){     log("Generating 10 random integers in range 0..99.");         //note a single Random object is reused here     Random randomGenerator = new Random();     for (int idx = 1; idx <= 10; ++idx){       int randomInt = randomGenerator.nextInt(100);       log("Generated : " + randomInt);     }         log("Done.");   }     private static void log(String aMessage){     System.out.println(aMessage);   } } Example run of this class: Generating 10 random integers in range 0..99. Generated : 44 Generated : 81 Generated : 69 Generated : 31 Generated : 10 Generated : 64 Generated : 74 Generated : 57 Generated : 56 Generated : 93 Done.

Sending Email through Gmail Server Using JavaServlets

import java.io.*; import java.net.*; import java.util.Properties; import javax.mail.AuthenticationFailedException; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.*; import javax.servlet.http.*; public class SendEmail extends HttpServlet {  protected void processRequest(HttpServletRequest request,                                   HttpServletResponse response)                    throws IOException, ServletException {         final String err = "/error.jsp";         //final String succ = "/success.jsp";    ...

Java Date and Time

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class SimpleDateFormatExample { public static void main(String[] args) { Date curDate = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); String DateToStr = format.format(curDate); System.out.println(DateToStr); format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); DateToStr = format.format(curDate); System.out.println(DateToStr); format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH); DateToStr = format.format(curDate); System.out.println(DateToStr); format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z"); DateToStr = format.format(curDate); System.out.println(DateToStr); try { Date strToDate = format.parse(DateToStr); System.out.println(strToDate); } catch (ParseException e) { e.printStackTrace(); } } } Output: 2014/12/19 19-12-2014 04:54:26 19...