/*************************************************************************
* Compilation: javac PowersOfTwo.java
* Execution: java PowersOfTwo N
*
* This program takes a command-line argument N and prnts a table of
* the powers of 2 that are less than or equal to 2^N.
*
* % java PowersOfTwo 5
* 0 1
* 1 2
* 2 4
* 3 8
* 4 16
* 5 32
*
* % java PowersOfTwo 6
* 0 1
* 1 2
* 2 4
* 3 8
* 4 16
* 5 32
* 6 64
*
* Remarks
* ------------
* Only works if 0 <= N < 31 since 2^31 overflows an int.
*
*************************************************************************/
public class PowersOfTwo {
public static void main(String[] args) {
// read in one command-line argument
int N = Integer.parseInt(args[0]);
int i = 0; // count from 0 to N-1
int powerOfTwo = 1; // the ith power of two
// repeat until i equals N
while (i <= N) {
System.out.println(i + " " + powerOfTwo); // print out the power of two
powerOfTwo = 2 * powerOfTwo; // double to get the next one
i = i + 1;
}
}
}
Monday, 2 August 2010
PowersOfTwo.java
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);
}
}
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();
}
}
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 functions in java (UDFs)
Inputing strings
public int inputInt() throws IOException
{
int k=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
str = br.readLine();
k = Integer.parseInt(str);
return k;
}
For inputing integers
public int inputInt() throws IOException
{
int k=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
str = br.readLine();
k = Integer.parseInt(str);
return k;
}
public int inputInt() throws IOException
{
int k=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
str = br.readLine();
k = Integer.parseInt(str);
return k;
}
For inputing integers
public int inputInt() throws IOException
{
int k=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
str = br.readLine();
k = Integer.parseInt(str);
return k;
}
Input or read in java from Standard IO
Lets first see what we mean by standard stream in java. Click here.
- Using scanner
To read console input, you first construct a Scanner that is attached to the "standard input stream" 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.Scanner in = new Scanner(System.in);
Here, we use the nextLine method because the input might contain spaces. To read a single word (delimited by whitespace), callSystem.out.print("What is your name? ");
String name = in.nextLine();To read an integer, use the nextInt method.String firstName = in.next();
Similarly, the nextdouble method reads the next floating-point number.System.out.print("How old are you? ");
int age = in.nextInt();
Finally, add the lineimport java.util.*;
- 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);
Importimport 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. - 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 IOExceptionBufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
str = br.readLine();
- Reading Characters
Subscribe to:
Posts (Atom)