Found 9150 Articles for Object Oriented Programming

Program to print a matrix in Diagonal Pattern.

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

1K+ Views

Following is the Java program to print diagonal pattern of a given matrix.Example Live Demopublic class DiagonalMatrix {    public static void main(String args[]){       int a[][]={{1,2,3},{4,5,6},{7,8,9}};       int rows = a.length;       int columns = a[0].length;       for (int i = 0; i < rows; i++) {          for (int r = i, c = 0; r >= 0 && c < columns; r--, c++){             System.out.print(a[r][c] + " ");          }          System.out.println();       }       for (int i = 1; i < columns; i++) {          for (int r = rows-1, c = i; r >= 0 && c < columns; r--, c++) {             System.out.print(a[r][c] + " ");          }          System.out.println();       }    } }Output1 4 2 7 5 3 8 6 9

Java program to print a given matrix in Spiral Form.

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

3K+ Views

Following is a Java program to print the spiral form of a given matrix.Example Live Demopublic class PrintMatrixInSpiralForm {    public static void main(String args[]){       int a[][]={{1,2,3},{4,5,6},{7,8,9}};       int w = 0;       int x = a.length-1;       int y = 0;       int z = a[0].length-1;       while(w

Program for printing array in Pendulum Arrangement.

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

815 Views

To arrange elements of the array following pendulum arrangement.Sort the given array, create an empty array to store the result.Store the 0th element in a variable say temp.Store the element at index 1 in the sorted array in (mid+1)st position of the resultant array, and the next element int the (mid-1)st position and the next element in (mid+2)nd element and so on.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ArrayinPendulumArrangement {    public static int[] swap(int origPos, int newPos, int[] array){       origPos = 1;       newPos = 4;       int temp = array[origPos];   ... Read More

How can we add new tabs to JTabbedPane from a JMenu in Java?

raja
Updated on 07-Feb-2020 11:42:24

344 Views

JTabbedPaneA JTabbedPane is a component can extend JComponent class and it can provide easy access to more than one panel.Each tab is associated with a single component that can be displayed when the tab is selected.A JTabbedPane can generate a ChangeListener interface when a tab is selected.The important methods of JTabbedPane are add(), addTab(), fireStateChanged(), createChangeListener(), setSelectedIndex(), getTabCount() and etc.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class JTabbedPaneTest extends JFrame implements ActionListener {    JTabbedPane tabbedPane;    int ntabs = 0;    public JTabbedPaneTest() {       getContentPane().setLayout(new BorderLayout());       tabbedPane = new JTabbedPane();   ... Read More

Program to print Sum Triangle of an array.

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

529 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

882 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

813 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

Advertisements