
In Top Java Programs for Coder article we have listed here programs to develop the logic of programming of the coder, programmer, developer.
Let’s start with the basic Java programs and then we will gradually increase the level of programming.
Table of Contents
Display numbers from 1 to 10 using infinite for loop.
We can form the infinite loops by using for loop, while loop or do…while loop. And we use break statement to come out of a loop.
public class Example {
public static void main(String[] args) {
int i = 1;
for (;;) {
System.out.println(i);
i++;
if (i > 10) {
break;
}
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
Program to come out of switch block after executing a task.
In this program, b becomes true and hence, JVM displays blue and then executes break, which terminates the switch block.
We use break inside the switch block to come out of the switch block.
public class Example {
public static void main(String[] args) {
char color = 'b';
switch (color) {
case 'r':
System.out.println("Red");
break;
case 'g':
System.out.println("Green");
break;
case 'b':
System.out.println("Blue");
break;
case 'w':
System.out.println("While");
break;
default:
System.out.println("No Color");
}
}
}
Output: Blue
Program for the method without parameters but with return type.
class Sample {
private double num1, num2;
Sample(double x, double y) {
num1 = x;
num2 = y;
}
double sum() {
double res = num1 + num2;
return res;
}
}
public class Example {
public static void main(String[] args) {
Sample s = new Sample(20, 40);
double x = s.sum();
System.out.println("Sum = " + x);
}
}
Output: Sum = 60.0
Write a program for the method without parameters but without return type.
class Sample {
private double num1;
private double num2;
public Sample(double x, double y) {
num1 = x;
num2 = y;
}
public void sum() {
double res = num1 + num2;
System.out.println("Sum = " + res);
}
}
class Example1 {
public static void main(String[] args) {
Sample s = new Sample(20, 30);
s.sum();
}
}
Output: Addition = 50.0
Program to Convert String to Double in Java
You can use the following three examples to convert String to Double in Java. Two of them are using the in-built methods Double.parseDouble() and Double.valueOf().
1) Using Double class constructor
public class Example {
public static void main(String[] args) {
String str = "542.125";
Double str1 = new Double(str);
System.out.println(str1);
}
}
Output: 542.125
2) Using Double.parseDouble() method
public class Example {
public static void main(String[] args) {
String str = "120.125";
Double str1 = Double.parseDouble(str);
System.out.println(str1);
}
}
Output: 120.125
3) Using Double.valueOf() method
public class Example {
public static void main(String[] args) {
String str = "110.1235";
Double str1 = Double.valueOf(str);
System.out.println(str1);
}
}
Output: 110.1235
How to convert a Double array to a String array in Java?
There may be many methods of converting double array to string array. Here we will convert each of array elements to a string and populate the string array with them. We are using toString() method for this purpose.
public class Example {
public static void main(String[] args) {
Double[] arr = {10.21, 30.31, 20.82, 50.33, 11.11};
int size = arr.length;
String[] str = new String[size];
for(int i = 0; i < size; i++) {
str[i] = arr[i].toString();
System.out.println(str[i]);
}
}
}
Output: 10.21 30.31 20.82 50.33 11.11
Java program to Convert Double to Integer in Java.
You can use the following three examples to convert Double to Integer in Java. Here we are providing you three way to convert double to integer which are as follows:
1) Using Typecasting
public class Example {
public static void main(String[] args) {
double data = 101.392;
int value = (int)data;
System.out.println(value);
}
}
Output: 101
2) Using Math.round() method
Math.round() method returns the nearest integer.
public class Example {
public static void main(String[] args) {
double data = 101.392;
int value = (int)Math.round(data);
System.out.println(value);
}
}
Output: 101
3) Using Double.intValue() method
Wrapper class Double truncates all digits after decimal point.
public class Example {
public static void main(String[] args) {
double data = 101.392;
Double data2 = new Double(data);
int value = data2.intValue();
System.out.println(value);
}
}
Output: 101
Program to convert a character array to string in Java
The easiest way to convert a character array to a string is to pass this character array to the constructor of the String class.
The String class provides a constructor that accepts an array of characters and creates a String object.
public class Example {
public static void main(String[] args) {
char[] charArray = {'G', 'e', 'e', 'k', 'C', 'e', 'r'};
String str = new String(charArray);
System.out.println(str);
}
}
Output: GeekCer
You are reading Top Java Programs for Coder. You can also find Top 100 C programs for geeks.
Java program to display the command line arguments
Command line actually a run command in which we give the values at the time of running of the program. In command line arguments we pass the value to the main() method.
If there are four command line arguments, the JVM allots memory for those four values only. If there are no command line arguments then JVM will not allot any memory.
Command line arguments are the way to provide input for the main() method.
By following the program let us understand how command line arguments work and how you can access values inside main() method.
public class Example {
public static void main(String[] args) {
int n = args.length;
System.out.println("Number of args. : " + n);
System.out.println("The args are: ");
for (int i = 0; i < n; i++) {
System.out.println(args[i]);
}
}
}
Output: C:\> javac Example.java C:\> java Example 10 20 Geek GeekCer Number of args. : 4 The args are: 10 20 Geek GeekCer
Write a program to generate fibonacci series
The Fibonacci sequence is a series of numbers where first two numbers are taken as 0 and 1 and then the next number is the addition of the last two numbers.
In order to generate this series, first we should take two numbers as n1 and n2. The next number say n3, will be obtained by adding these two numbers n3 = n1 + n2.
Let’s learn to create Fibonacci series. Fibonacci numbers are the number which follow the series like
0, 1, 1, 2, 3, 5, 8, 13, 21...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Example {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many fibonacci? ");
int n = Integer.parseInt(br.readLine());
long n1 = 0, n2 = 1;
System.out.println(n1);
System.out.println(n2);
long n3 = n1 + n2;
System.out.println(n3);
int count = 3;
while (count < n) {
n1 = n2;
n2 = n3;
n3 = n1 + n2;
System.out.println(n3);
count++;
}
}
}
Output: How many fibonacci? 10 0 1 1 2 3 5 8 13 21 34
To generate Fibonacci numbers repeatedly, we have used logic inside the while loop.
Write a program to create a File using FileWriter in Java
In the following program we are taking a string from which we will read the characters and will write in the FileWriter, which is associated with a file named “file.txt”.
The following program describes how to create a text file using FileWriter:
import java.io.*;
public class Example {
public static void main(String[] args) throws IOException {
String str = "This is a Computer Science Portal\nfor the Geeks";
FileWriter fileWriter = new FileWriter("file.txt");
for (int i = 0; i < str.length(); i++) {
fileWriter.write(str.charAt(i));
}
fileWriter.close();
}
}
Write a program to read a File using FileReader
In the following program, “file.txt” is attached to a FileReader to read data. It is read using the read() method and displayed on the screen.
The following program describes how to read a text file using FileReader .
import java.io.*;
public class Example {
public static void main(String[] args) throws IOException {
int ch;
FileReader fileReader = null;
try {
fileReader = new FileReader("file.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
while ((ch = fileReader.read()) != -1) {
System.out.print((char)ch);
}
fileReader.close();
}
}
Output: This is a Computer Science Portal for the Geeks
How to write program using Thread class?
public class ThreadDemo extends Thread {
public void run(){
System.out.println("This is an example of Thread class.");
}
public static void main(String args[]) {
ThreadDemo thread = new ThreadDemo ();
thread.start();
}
}
Output: This is an example of Thread class.
How to write program using Runnable interface?
public class RunnableDemo implements Runnable {
public void run(){
System.out.println("This is an example of Runnable interface.");
}
public static void main(String args[]) {
RunnableDemo runnable = new RunnableDemo();
Thread thread = new Thread(runnable);
thread.start();
}
}
Output: This is an example of Runnable interface.
Reverse String in Java using recursion
Write a program to reverse string in java without using function.
public class PrintString {
public void reverseString(String inputString) {
if (inputString == null || inputString.length() <= 0) {
System.out.println(inputString);
} else {
System.out.print(inputString.charAt(inputString.length() - 1));
reverseString(inputString.substring(0, inputString.length() - 1));
}
}
public static void main(String[] args) {
String inputString = "Welcome to GeekCer!";
PrintString printString = new PrintString();
printString.reverseString(inputString);
}
}
Output: !reCkeeG ot emocleW
Program to print all permutations of a given string
Following is the program to generate all permutations of a string. In this program we are using two methods. One for swapping characters and the other for finding permutations.
public class StringPermutation {
// Swapping the value of i position with j position
public static String swapString(String inputString, int i, int j) {
char[] arrayOfString = inputString.toCharArray();
char ch;
ch = arrayOfString[i];
arrayOfString[i] = arrayOfString[j];
arrayOfString[j] = ch;
return String.valueOf(arrayOfString);
}
public static void findPermutations(String inputString, int start, int end) {
// Prints the permutations
if (start == end - 1)
System.out.println(inputString);
else {
for (int i = start; i < end; i++) {
inputString = swapString(inputString, start, i);
findPermutations(inputString, start + 1, end);
inputString = swapString(inputString, start, i);
}
}
}
public static void main(String[] args) {
String inputString = "XYZ";
int length = inputString.length();
System.out.println("Permutations of the string are: ");
findPermutations(inputString, 0, length);
}
}
Output: Permutations of the string are: XYZ XZY YXZ YZX ZYX ZXY
You may know about Top C++ programs for geeks