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

Sunday, January 23, 2011

Fibonacci Series

Find the fibonacci series based on the input number. If number is 8 then it should print like 1 1 2 3 5 8 13 21
package myrnd;

public class Fibonacci {

public static void main(String[] args) {

int num = 8;
int a, b = 0, c = 1;
for (int i = 0; i < num; i++) {
System.out.print(c+" ");
a = b;
b = c;
c = a + b;
}
}
}

Reverse of a number

Find the reverse of a number. eg if input number is 786 then output should be 687.
[sourcecode language="java"]
package myrnd;

public class ReverseNo {

// 786 : reverse : 687
public static void main(String[] args) {
int n = 78890;
int result = 0;
int remainder;
while (n > 0) {
remainder = n % 10;
result = result * 10 + remainder;
n = n / 10;
}
System.out.println("Result:" + result);
}
}

[/sourcecode]

Factorial of a number

[sourcecode language="java"]
package myrnd;

public class Factorial {

public static void main(String[] args) {
int a = 5;
int result = 1;
while (a > 0) {
result *= a;
a--;
}
System.out.println("result is:" + result);
// using recursion.
System.out.println("factorial :" + factorial(5));
}

// another way using recursion.
public static int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
}
return n;
}
}

[/sourcecode]

Some Simple java practice code

Find the sum of numbers between 100 and 200 those are divisible by 7.
[sourcecode language="java"]
package myrnd;

public class SpecificSum {

public static void main(String[] args) {
int result = 0;
for (int i = 100; i < 200; i++) {
if (i % 7 == 0) {
result += i;
}
}
System.out.println("Sum:" + result);
}
}

[/sourcecode]

Monday, November 15, 2010

Java crawler

package com.myjobalert.crawler;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public abstract class Crawler {

public String getURLContents(String urlStr, HttpParam[] httpParams) throws IOException {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
HttpURLConnection httpconn = (HttpURLConnection) conn;

if (httpParams != null) {
httpconn.setRequestMethod("POST");
for (HttpParam param : httpParams) {
httpconn.setRequestProperty(param.getName(), param.getValue());
}
conn = httpconn;
}
InputStreamReader bis = new InputStreamReader(conn.getInputStream());
System.out.println("page size:"+conn.getContentLength());
final int char_per_page = 5000;
char[] buff = new char[char_per_page];
StringBuilder sb = new StringBuilder(char_per_page);
int read = 0;
while (read != -1) {
read = bis.read(buff);
if (read != -1)
sb.append(buff, 0, read);
}
System.out.println("page size:"+(sb.length()/1000)+"KB");
System.out.println("page is downloaded");
return sb.toString();
}
}

class HttpParam {

private String name;
private String value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

http://ws.amazon.com/widgets/q?rt=qf_br_asin_ssw&ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fmyknowledgebo-20%2F8003%2F13de8fda-f06a-4d41-9041-b4aeab627f25&Operation=GetDisplayTemplate Amazon.com Widgets

Sunday, May 2, 2010

Sorting a list in java using comparator

[sourcecode language="css"]
public class SortTest {
    public static void main(String[] args) {
        List<Integer> ls = new ArrayList<Integer>();
        for (int i = 1; i &lt; 10; i++) {
            Random r = new Random();
            int randomNo = r.nextInt(i);
            ls.add(randomNo);
        }
        System.out.println("before sorting:" + ls);
        Comparator c = new Comparator() {
            @Override
            public int compare(Object arg0, Object arg1) {
                if ((Integer) arg0 &gt; (Integer) arg1) {
                    return 1;
                } else if ((Integer) arg0 == (Integer) arg1) {
                    return 0;
                } else {
                    return -1;
                }
            }
        };
        Collections.sort(ls, c);
        System.out.println("after sorting:" + ls);

    }
}
[/sourcecode]