Found 9291 Articles for Object Oriented Programming

Printing Triangle Pattern in Java

AmitDiwan
Updated on 04-Jul-2020 10:51:38

1K+ Views

Following is the Java program to print Triangle pattern −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       Scanner my_scan = new Scanner(System.in);       System.out.println("Enter the number of rows which needs to be printed");       int my_row = my_scan.nextInt();       for (int i = 1; i = i; j--){             System.out.print(" ");          }          for (int j = 1; j

Printing Integer between Strings in Java

AmitDiwan
Updated on 04-Jul-2020 10:49:27

906 Views

Following is the Java program to print integer between Strings −Example Live Demopublic 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.

Rename multiple files using Java

AmitDiwan
Updated on 04-Jul-2020 10:46:58

978 Views

Following is the code to rename multiple files using Java −Exampleimport java.io.File; import java.io.IOException; public class Demo{    public static void main(String[] argv) throws IOException{       String path_to_folder = "path\to\folder\where\multiple\files\are\present";       File my_folder = new File(path_to_folder);       File[] array_file = my_folder.listFiles();       for (int i = 0; i < array_file.length; i++){          if (array_file[i].isFile()){             File my_file = new File(path_to_folder + "\" + array_file[i].getName());             String long_file_name = array_file[i].getName();             String[] my_token = long_file_name.split("\s"); ... Read More

How to clear screen using Java?

AmitDiwan
Updated on 04-Jul-2020 10:44:46

3K+ Views

Following is the code to clear screen using Java −Example Live Demopublic 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.

Java Program for GCD of more than two (or array) numbers

AmitDiwan
Updated on 04-Jul-2020 10:41:27

443 Views

Following is the Java program for GCD of more than two numbers −Example Live Demopublic class Demo{    static int gcd_of_nums(int val_1, int val_2){       if (val_1 == 0)       return val_2;       return gcd_of_nums(val_2 % val_1, val_1);    }    static int find_gcd(int arr[], int no){       int result = arr[0];       for (int i = 1; i < no; i++){          result = gcd_of_nums(arr[i], result);          if(result == 1){             return 1;          }   ... Read More

Java Program for focal length of a spherical mirror

AmitDiwan
Updated on 04-Jul-2020 10:05:38

213 Views

Following is the Java code for focal length of a spherical mirror −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static float concave_f_len(float val){       return val/2 ;    }    public static float convex_f_len(float val){       return - (val/2 ) ;    }    public static void main(String argc[]){       float val = 55 ;       System.out.print("The focal length of spherical mirror (concave) is : " + concave_f_len(val) + "units");       System.out.println("The focal length of spherical mirror (convex) is : "+ convex_f_len(val) + "units");    } }OutputThe ... Read More

Java Program to find the vertex, focus and directrix of a parabola

AmitDiwan
Updated on 04-Jul-2020 10:03:43

129 Views

Following is the Java program to find the vertex, focus and directrix of a parabola −Example Live Demopublic 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 More

Java Program to find the perimeter of a cylinder

AmitDiwan
Updated on 04-Jul-2020 10:01:17

193 Views

Following is the Java code to find the perimeter of a cylinder −Example Live Demoimport 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 ... Read More

Java Program to find largest prime factor of a number

AmitDiwan
Updated on 04-Jul-2020 09:59:52

2K+ Views

Following is the Java code to find the largest prime factor of a number −Example Live Demoimport 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 More

String Formatting in Java using %

AmitDiwan
Updated on 04-Jul-2020 09:57:44

121 Views

Followimg is the code to implement String formatting in Java using % −Example Live Demopublic 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 ... Read More

Advertisements