Found 7442 Articles for Java

Program to print Sum Triangle of an array.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

531 Views

To generate a sum triangle of a given arrayGet the elements of the array from the user say, myArray[n].Create a two dimensional array say, result[n][n].Store the contents of the given array in the first row from bottom of the 2D array.result[n][i] = myArray[i].Now, from the second row of the 2D array fill elements such that ith element in each row is the sum of ith and (i+1)st elements of the previous row.result[i][j] = result[i+1][j] + result[i+1][j+1];Example Live Demoimport java.util.Scanner; public class SumTriangleOfAnArray {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter ... Read More

Recursive program to find an element in an array linearly.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

887 Views

Following is a Java program to find an element in an array linearly.Example Live Demoimport java.util.Scanner; public class SearchingRecursively {    public static boolean searchArray(int[] myArray, int element, int size){       if (size == 0){          return false;       }       if (myArray[size-1] == element){          return true;       }       return searchArray(myArray, element, size-1);    }    public static void main(String args[]){       System.out.println("Enter the required size of the array: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Java program to cyclically rotate an array by one.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

To rotate the contents of an array cyclically −create an empty variable. (temp)save the last element of the array in it.Now, starting from the nth element of the array, replace the current element with the previous element.Store the element in temp in the 1st position.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class CyclicallyRotateanArray {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter the required size of the array ::");       int size = sc.nextInt();       int [] myArray = new int[size];       System.out.println("Enter elements of ... Read More

Java program for Multiplication of Array elements

Venkata Sai
Updated on 18-Jun-2024 15:41:39

20K+ Views

To find the product of elements of an array.create an empty variable. (product)Initialize it with 1.In a loop traverse through each element (or get each element from user) multiply each element to product.Print the product.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ProductOfArrayOfElements {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int product = 1;       System.out.println("Enter the elements of the ... Read More

Java program to print a given pattern. (stars)

Venkata Sai
Updated on 30-Jul-2019 22:30:26

814 Views

Following is a Java program which prints a pyramid of numbers.Example Live Demopublic class PrintingPyramidStars {    public static void main(String args[]){       int n,i,j;       n = 5;       for(i = 0; i

Java Programs for printing Pyramid Patterns. (of numbers)

Venkata Sai
Updated on 30-Jul-2019 22:30:26

262 Views

Following is a Java program which prints a pyramid of numbers.Example Live Demopublic class PrintingPyramids {    public static void main(String args[]){       int n,i,j,k=1;       n = 5;       for(i = 0; i

Java Program to find all angles of a triangle

Venkata Sai
Updated on 30-Jul-2019 22:30:26

601 Views

To find the angles of a triangle we can use sin/cosine rules according to cosine rule −cos A = (b^2 + c^2 - a^2)/2bcwhere A, B , C are the vertices and a, b, c are the sides of the given triangle then, angles of the triangle when the sides a, b, c given are −angleAtA = acos((b^2 + c^2 - a^2)/(2bc)) angleAtB = acos((a^2 + c^2 - b^2)/(2ac)) angleAtC = acos((a^2 + b^2 - c^2)/(2ab))

How can we implement right click menu using JPopupMenu in Java?

Alshifa Hasnain
Updated on 06-May-2025 18:38:17

2K+ Views

In this article, we will learn to implement right right-click menu using JPopupMenu in Java. A JPopupMenu appears anywhere on the screen when the right mouse button is clicked. JPopupMenu A JPopupMenu menu is a free-floating menu that is associated with an underlying component called the invoker. Most of the time, a popup menu is linked to a specific component to display context-sensitive choices. Syntax The following is the syntax for JPopupMenu initialization: JPopupMenu popup = new JPopupMenu(); In order to create a popup menu, we can use the JPopupMenu class., We can add the JMenuItem to the ... Read More

What are the differences between a JTextPane and a JEditorPane in Java?

Alshifa Hasnain
Updated on 22-Apr-2025 18:27:30

2K+ Views

In Java, a JTextPane is an extension of JEditorPane which provides word processing features like fonts, text styles, colors and etc. If we need to do heavy-duty text processing, we can use this class, whereas a JEditorPane supports display/editing of HTML and RTF content and can be extended by creating our own EditorKit. JTextPane A JTextPane is a subclass of JEditorPane. A JTextPane is used for a styled document with embedded images and components. A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the ... Read More

What is the importance of a WindowListener interface in Java?

raja
Updated on 30-Jun-2020 13:32:30

301 Views

The class which process the WindowEvent needs to be implemented this interface and an object of this class can be registered with a component by using addWindowListener() method.Methods of WindowListener InterfaceThe WindowListener interface defines 7 methods for handling window eventsvoid windowActivated(WindowEvent we) − Invoked when a window is activated.void windowDeactivated(WindowEvent we) − Invoked when a window is deactivated.void windowOpened(WindowEvent we) − Invoked when a window is opened.void windowClosed(WindowEvent we) − Invoked when a window is closed.void windowClosing(WindowEvent we) − Invoked when a window is closing.void windowIconified(WindowEvent we) − Invoked when a window minimized.void windowDeiconfied(WindowEvent we) − Invoked when a window is restored.Syntaxpublic ... Read More

Advertisements