Sunday, August 8, 2010

finding Fibonacci series in Java

import java.util.*;
/**
 *
 * @author Akhter Wahab
 */

public class Main {

    /**
     * @param args the command line arguments
     */
    public static long fib(int n) {
        if (n <= 1) return n;
        else return fib(n-1) + fib(n-2);
    }

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int N = Integer.parseInt(scan.nextLine());//taking input from the user the limit of the Fibonacci series
        for (int i = 1; i <= N; i++)
            System.out.println(i + ": " + fib(i));
    }

}

Saturday, August 7, 2010

finding Prime Number in java

class Prime {
  public static void main(String[] args) {
    int number = 11;
    int i;
    for (i=2; i < num ;i++ ){
      int n = number%i;
      if (n==0){
        System.out.println("number is not Prime!");
        break;
      }
    }
    if(i == num){
      System.out.println("number is Prime !");
    }
  }
}

difference between abstraction and encapsulation

 The difference between both are ,Abstraction and encapsulation are two  separate concepts in Java. Abstraction is a technique that is used to represent common functionality amongst a set of classes. An analogy is to look at a set of vehicles: Car, Truck and Bus have some common features that all road vehicles share. From a Java perspective, the mechanics of turning four wheels, an engine, could be handled in abstract form through a common superclass. Abstraction allows subclasses to share common code without duplication.

Friday, August 6, 2010

Java Mail as text


public class MakeSend {
 
 String userName;
 String passWord;
 Logger logger = Logger.getLogger(MakeSend.class);
 
 public MakeSend(String userName,String passWord){
  
  this.userName = userName;
  this.passWord = passWord;
 }
  
 public String doSend(String smtp, StringBuffer msgBody,
String toEmail, String subject, String mimetype){
  
  Session mailSession;
  
  try{
   Properties mailProps = new Properties();
   mailProps.setProperty("mail.transport.protocol", "smtp");
   mailProps.setProperty("mail.smtp.host", smtp);
   mailProps.setProperty("mail.smtp.auth", "true");
   
   Authenticator mailAuth = new SMTPAuthenticator();
   mailSession = Session.getInstance(mailProps,mailAuth);
   Transport transport = mailSession.getTransport();
   
   MimeMessage msg = new MimeMessage(mailSession);
   
   msg.setFrom(new InternetAddress(userName));
   msg.setSubject(subject,"UTF-8");
   msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
   msg.setDataHandler(new DataHandler(
     new javax.mail.util.ByteArrayDataSource(msgBody.
toString(), mimetype)));
   
   transport.connect();
   transport.sendMessage(msg, msg.getRecipients(Message.
RecipientType.TO));
   transport.close();
   
  } catch (MessagingException mex) {   
   logger.error("Email send failed, exception on:
[" + toEmail + "] -" + mex);
          return "ERROR";
  }catch (IOException ioex){
   logger.error("Email send failed, exception on:
[" + toEmail + "] -" + ioex);
   return "ERROR";
  }
  finally {
   mailSession = null;
  }
  
  return "OK";
 }
 
 private class SMTPAuthenticator extends Authenticator {
  
  public PasswordAuthentication getPasswordAuthentication(){
     
   return new PasswordAuthentication(userName,passWord);
  }  
 }

}

java Mail

you can send mail by using transport service here is the cod for example

public void sendEmail(String emailBody, String emailRecipients, String subject){
        try {
            String[] toAddress = parseArray(emailRecipients);
            String from = (FROM_ADDRESS);
            if (toAddress != null) {
                for (int i = 0; i < toAddress.length; i++) {
                    String address = (String) toAddress[i];
                    m_transportService.sendText(emailBody, subject, address, from);
                }
            }
        } catch (Exception e) {
            getLogger().log(LogLevel.WARN, e.getMessage());
        }
    }

my First Program in JAVA

public class MyFirstProgram{
    public MyFirstProgram(){
    }
public static void main (String args[]){
      System.out.println("This is my First Program ");
}

the out put is "This is my first program " pm the console

primitive data types in JAVA

There is Eight primitive data types in the JAVA
boolean
one-bit. can take one value true and false only.

true and false are defined constants of the language and are not the same as True and False, TRUE and FALSE, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.

byte
one byte .values from -128 to 127.

short
two bytes, -32,768 to 32,767

int
four bytes -2,147,483,648 to 2,147,483,647.

long
eight bytes Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

float
four bytes range from 1.40129846432481707e-45 to 3.40282346638528860e+38

double
eight bytes IEEE 754. range from 4.94065645841246544e-324d to 1.79769313486231570e+308d .
char
two bytes, 0 to 65,535 unicode