Monday, 2 August 2010

Factors.java

/*************************************************************************
* Compilation: javac Factors.java
* Execution: java Factors N
*
* Computes the prime factorization of N using brute force.
*
* % java Factors 81
* The prime factorization of 81 is: 3 3 3 3
*
* % java Factors 168
* The prime factorization of 168 is: 2 2 2 3 7
*
* % java Factors 4444444444
* The prime factorization of 4444444444 is: 2 2 11 41 271 9091
*
* % java Factors 4444444444444463
* The prime factorization of 4444444444444463 is: 4444444444444463
*
* % java Factors 10000001400000049
* The prime factorization of 10000001400000049 is: 100000007 100000007
*
* % java Factors 1000000014000000049
* The prime factorization of 1000000014000000049 is: 1000000007 1000000007
*
* % java Factors 9201111169755555649
* The prime factorization of 9201111169755555649 is: 3033333343 3033333343
*
* Can use these for timing tests - biggest 3, 6, 9, 12, 15, and 18 digit primes
* % java Factors 997
* % java Factors 999983
* % java Factors 999999937
* % java Factors 999999999989
* % java Factors 999999999999989
* % java Factors 999999999999999989
*
* Remarks
* -------
* - Tests i <= N / i instead of i <= N for efficiency.
*
* - The last two examples still take a few minutes.
*
* - Tests i <= N / i instead of i * i <= N to stave off overflow
* and enable us to factor 9201111169755555649; otherwise program
* goes into infinite loop. However, i * i <= N is almost twice
* as fast on some systems.
*
*************************************************************************/

public class Factors {

public static void main(String[] args) {

// command-line argument
long n = Long.parseLong(args[0]);

System.out.print("The prime factorization of " + n + " is: ");

// for each potential factor i
for (long i = 2; i <= n / i; i++) {

// if i is a factor of N, repeatedly divide it out
while (n % i == 0) {
System.out.print(i + " ");
n = n / i;
}
}

// if biggest factor occurs only once, n > 1
if (n > 1) System.out.println(n);
else System.out.println();
}
}

Harmonic.java


/*************************************************************************
* Compilation: javac Harmonic.java
* Execution: java Harmonic N
*
* Prints the Nth harmonic number: 1/1 + 1/2 + ... + 1/N.
*
* % java Harmonic 10
* 2.9289682539682538
*
* % java Harmonic 10000
* 9.787606036044348
*
*************************************************************************/

public class Harmonic {
public static void main(String[] args) {

// command-line argument
int N = Integer.parseInt(args[0]);

// compute 1/1 + 1/2 + 1/3 + ... + 1/N
double sum = 0.0;
for (int i = 1; i <= N; i++) {
sum += 1.0 / i;
}

// print out Nth harmonic number
System.out.println(sum);
}

}

Converting an integer to binary string - Binary.java


/*************************************************************************
* Compilation: javac Binary.java
* Execution: java Binary n
*
* Prints out n in binary.
*
* % java Binary 5
* 101
*
* % java Binary 106
* 1101010
*
* % java Binary 0
* 0
*
* % java Binary 16
* 10000
*
* Limitations
* -----------
* Does not handle negative integers.
*
* Remarks
* -------
* could use Integer.toBinaryString(N) instead.
*
*************************************************************************/

public class Binary {
public static void main(String[] args) {

// read in the command-line argument
int n = Integer.parseInt(args[0]);

// set v to the largest power of two that is <= n
int v = 1;
while (v <= n/2) {
v = v * 2;
}

// check for presence of powers of 2 in n, from largest to smallest
while (v > 0) {

// v is not present in n
if (n < v) {
System.out.print(0);
}

// v is present in n, so remove v from n
else {
System.out.print(1);
n = n - v;
}

// next smallest power of 2
v = v / 2;
}

System.out.println();

}

}

Though written a program, we can use inbuilt lib of java - See here.

Sunday, 1 August 2010

Input or read in java from Standard IO

Lets first see what we mean by standard stream in java. Click here.

  1. Using scanner

    To read console input, you first construct a Scanner that is attached to the "standard input stream" System.in.
    Scanner in = new Scanner(System.in);


    Now you use the various methods of the Scanner class to read input. For example, the nextLine method reads a line of input.



    System.out.print("What is your name? ");
    String name = in.nextLine();

    Here, we use the nextLine method because the input might contain spaces. To read a single word (delimited by whitespace), call


    String firstName = in.next();


    To read an integer, use the nextInt method.

    System.out.print("How old are you? "); 
    int age = in.nextInt();

    Similarly, the nextdouble method reads the next floating-point number.

    Finally, add the line



    import java.util.*;



  2. Using JOptionPane


    String input = JOptionPane.showInputDialog(promptString)

    eg.

    String name = JOptionPane.showInputDialog("What is your name?");

    Entering Integers
    String name = JOptionPane.showInputDialog("What is your age?");
    int age = Integer.parseInt(input);


    Import
    import javax.swing.*;
     

    Finally, whenever your program calls JOptionPane.showInputDialog, you need to end it with a call to System.exit(0). The reason is a bit technical. Showing a dialog box starts a new thread of control. When themain method exits, the new thread does not automatically terminate. Toend all threads, you call the System.exit method.


  3. Using BufferedReader Its most commonly used constructor is shown here:

    BufferedReader(Reader inputReader)
    Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use the following constructor:
    InputStreamReader(InputStream inputStream)

    So basic step is :

    InputStreamReader inp = new InputStreamReader(System.in)
    BufferedReader br = new BufferedReader(inp);

    Now to read character :

    char c = br.read();

    To read String:

    String str = br.readLine();



    • Reading Characters
      int read( ) throws IOException
      Each time that read( ) is called, it reads a character from the input stream and returns it
      as an integer value. It returns –1 when the end of the stream is encountered. As you can
      see, it can throw an IOException.


      char c;
      BufferedReader br = new
      BufferedReader(new InputStreamReader(System.in));
      c = (char) br.read();
      System.out.println(c)


    • Reading strings
      String readLine( ) throws IOException


      BufferedReader br = new BufferedReader(new
      InputStreamReader(System.in));
      String str;
      str = br.readLine();

Formatting output in java

Friday, 30 July 2010

Output or write on Standard output in java

print
double x=5;
System.out.print(x);

println
This will print the item and put the cursor in new line.
System.out.println(x);

printf
System.out.printf("Hello, %s. Next year, you'll be %d", name, age);

write
System.out.write(x);
It is simpler to use print and println than write.

You can see formatting output in java here.

Wednesday, 28 July 2010

Final keyword in java

A java variable can be declared using the keyword final. Then the final variable can be assigned only once. You cannot be modify it afterwards. However it can be used in different contexts.

Following are the places where final can be used:

  1. variables: a final variable can be set once and only once.
    blank final variable - A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
  2. fields: a final field can also be set only once, by the constructor of the class which defines it.
  3. methods: a final method cannot be overridden nor hidden.
    final parameters - values of the parameters cannot be changed after initialization.
  4. classes: a final class cannot be extended or inherited.

Other effects of final:

  • Java local classes can only reference local variables and parameters that are declared as final.
  • A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

Notice how using final is an entirely negative act. The final keyword works by subtracting, limiting default language mechanisms: the ability to override a method, to set a variable or a field. The motivations behind using final fall into three broad categories: correctness, robustness, and finally performance. Seeing them 1 by one.

Final Variables

Final variables come in handy in mostly three situations: 

a) Declare Constants

Coupled with static, final is used to flag constants. This usage is well-known to all Java programmers, so I won't expand much on it. It is useful to know that the value of a field declared constant in that manner will be computed statically if possible, at compile-time.
private static final int ERROR_CODE = 1 + 3 * 4 / 2;
public static final String ERROR_MESSAGE = "An error
occurred with code="
+ ERROR_CODE;

The compiler will compute the value for ERROR_CODE, concatenate the string equivalent of the result, and assign the resulting String to ERROR_MESSAGE.


b) Final Parameters

The following sample declares final parameters:




public void doSomething(final int i, final int j){
// ...
}
final is used here to ensure the two indexes i and j won't accidentally be reset by the method. It's a handy way to protect against an insidious bug that erroneously changes the value of your parameters. Generally speaking, short methods are a better way to protect from this class of errors, but final parameters can be a useful addition to your coding style.
Note that final parameters are not considered part of the method signature, and are ignored by the compiler when resolving method calls. Parameters can be declared final (or not) with no influence on how the method is overriden.


c) Anonymous Local Classes

The third situation involving final variables is actually mandated by language semantics. In that situation, the Java compiler won't let you use a variable unless it is declared final. This situation arises with closures, also known as anonymous local classes. Local classes can only reference local variables and parameters that are declared final.





public void doSomething(int i, int j){
final int n = i + j; // must be declared final
Comparator comp = new Comparator() {
public int compare(Object left, Object right) {
return n; // return copy of a local variable
}
};//note the semicolon here
}

The reason for this restriction becomes apparent if we shed some light on how local classes are implemented. An anonymous local class can use local variables because the compiler automatically gives the class a private instance field to hold a copy of each local variable the class uses. The compiler also adds hidden parameters to each constructor to initialize these automatically created private fields. Thus, a local class does not actually access local variables, but merely its own private copies of them. The only way this can work correctly is if the local variables are declared final, so that they are guaranteed not to change. With this guarantee in place, the local class is assured that its internal copies of the variables accurately reflect the actual local variables.


2. Final Fields

A final field can be assigned once and only once, and must be initialized by every constructor of the class that declares it. It is also possible to assign the field directly, in the same statement where it is defined. This simply reflects the fact that such shortcut assignments are compiled into a synthetic constructor. E.g. both the following code samples are correct and strictly equivalent; the first is preferred for being shorter. Initializing outside constructor body

public class MyClass{
//initializing hereitself
private final int i = 2;
}

Initialising in constructor body



public class MyClass{
private final int i;
public MyClass()
{
i = 2;
}
}


3. Using final to Prevent Method Overriding



While method overriding is one of Java’s most powerful features, there will be times when you will want to prevent it from occurring. To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden. The following fragment illustrates final:


class A {

final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {

void meth() { // ERROR! Can't override.
System.out.println("Illegal!");
}
}

Because meth( ) is declared as final, it cannot be overridden in B. If you attempt to
do so, a compile-time error will result.

Methods declared as final can sometimes provide a performance enhancement: The compiler is free to inline calls to them because it “knows” they will not be overridden by a subclass. When a small final method is called, often the Java compiler can copy the bytecode for the subroutine directly inline with the compiled code of the calling method, thus eliminating the costly overhead associated with a method call. Inlining is only an option with final methods. Normally, Java resolves calls to methods dynamically, at run
time. This is called late binding. However, since final methods cannot be overridden, a call to one can be resolved at compile time. This is called early binding.



4. Using final to Prevent Inheritance

Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too. As you might expect, it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.
Here is an example of a final class:
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}


As the comments imply, it is illegal for B to inherit A since A is declared as final.


CPP vs Java on Final – Controversy


‘final’ should not be called as constants. Because when an array is declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifcations. In general context constants will not allow to modify. In C++, an array declared as const will not allow the above scenario but java allows. So java’s final is not the general constant used across in computer languages.

A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.

Definition as per java language specification (third edition) – 4.12.4 is “A final variable may only be assigned to once.”(§4.1.2). See notes on final w.r.t Java memory model here -


Java language specification tries to redefine the meaning of constant in the following way!
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).


Immutable Objects


Final is used to construct immutable objects in java. See – Immutable Objects