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
-
Economics & Finance
Object Oriented Programming Articles
Page 259 of 589
Recursive program to find an element in an array linearly.
Following is a Java program to find an element in an array linearly.Exampleimport 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
Read MoreProgram to print Sum Triangle of an array.
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];Exampleimport java.util.Scanner; public class SumTriangleOfAnArray { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the ...
Read MoreProgram for printing array in Pendulum Arrangement.
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.Exampleimport 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 MoreJava program to print a given matrix in Spiral Form.
Following is a Java program to print the spiral form of a given matrix.Examplepublic 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
Read MoreProgram to print a matrix in Diagonal Pattern.
Following is the Java program to print diagonal pattern of a given matrix.Examplepublic 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
Read MoreProgram for converting Alternate characters of a string to Upper Case.
You can convert a character to upper case using the toUpperCase() method of the character class.ExampleFollowing program converts alternate characters of a string to Upper Case.import java.util.Scanner; public class UpperCase { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string :"); String str = sc.nextLine(); str = str.toLowerCase(); char[] ch = str.toCharArray(); for(int i=0; i
Read MoreCan we extend interfaces in Java? Explain?
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Just like classes you can extend one interface from another using the extends keyword as shown below −interface ArithmeticCalculations{ public abstract int addition(int a, int b); public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations{ public abstract double squareRoot(int a); public abstract double powerOf(int a, int b); }But, when you implement the sub-class you need to provide body for the abstract methods in both interfaces.ExampleIn the following example we have created two interfaces ...
Read MoreWrite a program to convert an array to String in Java?
The Arrays class of the java.util package provides a method named toString() this method accepts an array value (of any type) and returns a String.ExampleFollowing Java program accepts various arrays from the user, converts them into String values and prints the results.import java.util.Arrays; import java.util.Scanner; public class ObjectArrayToStringArray { public static void main(String args[]){ Scanner sc = new Scanner(System.in); //Integer array to String System.out.println("Enter 5 integer values: "); int intArray[] = new int[5]; for(int i=0; i
Read MoreWhat are copy constructors in Java?
Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.writing a copy constructorUsually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values.int name; int age; public Student(String name, int age){ this.name = name; this.age ...
Read MoreDisplay nanoseconds with Java Date and Time Conversion Character
To display nanoseconds, use the ‘N’ Date and Time conversion specifier.System.out.printf("Nanoseconds = %tN", d);Exampleimport java.util.Date; public class Demo { public static void main(String[] args) { Date d = new Date(); System.out.printf("Nanoseconds = %tN", d); } }OutputNanoseconds = 092000000
Read More