Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 14 of 450
How to clear screen using Java?
Following is the code to clear screen using Java −Examplepublic class Demo{ public static void main(String[] args){ System.out.print("\033[H\033[2J"); System.out.flush(); } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.
Read MoreJava Program for Counting Sort
The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Exampleimport java.util.*; public class Demo{ static void count_sort(int[] arr){ int max_val = Arrays.stream(arr).max().getAsInt(); int min_val = Arrays.stream(arr).min().getAsInt(); int range = max_val - min_val + 1; int count[] = new int[range]; int result[] = new int[arr.length]; for (int i = 0; i < arr.length; i++){ count[arr[i] - min_val]++; ...
Read MoreJava Program for Comb Sort
The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Exampleimport java.util.Arrays; public class Demo{ void comb_sort(int nums[]){ int len_gap = nums.length; float shrink_val = 1.3f; boolean swap = false; while (len_gap > 1 || swap) { if (len_gap > 1) { len_gap = (int)(len_gap / shrink_val); } swap = ...
Read MoreJava Program for Anagram Substring Search
Following is an example for Anagram Substring Search in Java −Examplepublic class Demo{ static final int max_val = 256; static boolean compare_vals(char my_arr_1[], char my_arr_2[]){ for (int i = 0; i < max_val; i++) if (my_arr_1[i] != my_arr_2[i]) return false; return true; } static void search_subs(String my_pattern, String my_text){ int pat_len = my_pattern.length(); int txt_len = my_text.length(); char[] count_pat = new char[max_val]; char[] count_txt = new char[max_val]; for (int ...
Read MoreJava Program for Bitonic Sort
In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Examplepublic class Demo{ void compare_swap(int my_arr[], int i, int j, int direction){ if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){ int temp = my_arr[i]; my_arr[i] = my_arr[j]; my_arr[j] = temp; } } void merge_vals(int my_arr[], int low, int cnt, int direction){ ...
Read MoreString Formatting in Java using %
Followimg is the code to implement String formatting in Java using % −Examplepublic class Demo { public static void main(String args[]){ String my_str = " sample."; String concat_Str = String.format("This is a" + "%s", my_str); String format_str_1 = String.format("The value is %.4f", 78.92367); System.out.println(concat_Str); System.out.println(format_str_1); } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a floating ...
Read MoreJava Program to find largest prime factor of a number
Following is the Java code to find the largest prime factor of a number −Exampleimport java.io.*; import java.util.*; public class Demo{ static long maxPrimeFactors( long val){ long max_prime = -1; while (val % 2 == 0) { max_prime = 2; val >>= 1; } for (int i = 3; i 2) max_prime = val; return max_prime; } public static void main(String[] args){ int val = 148592; ...
Read MoreJava Program to find the perimeter of a cylinder
Following is the Java code to find the perimeter of a cylinder −Exampleimport java.io.*; public class Demo{ static int find_peri(int dia, int ht){ return 2*(dia + ht); } public static void main(String[] args){ int dia = 7; int ht = 15; System.out.println("The perimeter of the cylinder is " + find_peri(dia, ht) + " units"); } }OutputThe perimeter of the cylinder is 44 unitsA class named Demo defines a static function that takes in two values, diameter and height. This function computes the sum of ...
Read MoreJava Program to find the vertex, focus and directrix of a parabola
Following is the Java program to find the vertex, focus and directrix of a parabola −Examplepublic class Demo{ public static void find_values(float val_1, float val_2, float val_3){ System.out.println("The value of vertex is (" + (-val_2 / (2 * val_1)) + ", "+ (((4 * val_1 * val_3) - (val_2 * val_2)) / (4 * val_1)) + ")"); System.out.println("The value of focus is (" + (-val_2 / (2 * val_1)) + ", " + (((4 * val_1 * val_3) - (val_2 * val_2) + 1) / (4 * val_1)) + ")"); ...
Read MorePrinting Integer between Strings in Java
Following is the Java program to print integer between Strings −Examplepublic class Demo{ public static void main(String[] args){ System.out.println("The equals symbol is present between two integer values "); System.out.println(45+5 + "=" +(56+11)); System.out.println(45+5 + " equals symbol " +(56+11)); } }OutputThe equals symbol is present between two integer values 50=67 50 equals symbol 67A class named Demo contains the main function that prints integer values between two strings.
Read More