Skip to main content

Posts

Showing posts with the label Java Language Basics

Write a program to find the biggest number in a 3*3 array. The program is supposed to receive 9 integer numbers as command line arguments. Example1: C:\>java Sample 1 2 3 O/P: Please enter 9 integer numbers Example2: C:\>java Sample 1 23 45 55 121 222 56 77 89 O/P: The given array is : 1 23 45 55 121 222 56 77 89 The biggest number in the given array is 222 | Two Dimensional Array

Write a program to find the biggest number in a 3*3 array. The program is supposed to receive 9 integer numbers as command line arguments. Example1: C:\>java Sample 1 2 3 O/P: Please enter 9 integer numbers Example2: C:\>java Sample 1 23 45 55 121 222 56 77 89 O/P:  The given array is : 1 23 45  55 121 222  56 77 89  The biggest number in the given array is 222  //Use Two Dimensional Array.  >> If you have any alternate methods then please comment below. public class Sample { public static void main(String[] args) { if(args.length != 9) { System.out.println("Please enter 9 integer numbers"); } else { int[][] array = new int[3][3]; int p = 0; for (int i = 0; i<array.length; i++) { for (int j = 0; j<array.length; j++) { array[i][j] = Integer.parseInt(args[p++]); } } System.out.println("The given array is : "); for (int i = 0; i<array.length; i++) { for (int j = 0; j<array.length; j

Write a program to reverse the elements of a given 2*2 array. Four integer numbers needs to be passed as Command Line arguments. Example1) C:\>java Sample 1 2 3 O/P: Please enter 4 integer numbers Example2) C:\>java Sample 1 2 3 4 O/P: The given array is : 1 2 3 4 The reverse of the array is : 4 3 2 1 | Two Dimensional Array

Write a program to reverse the elements of a given 2*2 array. Four integer numbers needs to be passed as Command Line arguments. Example1) C:\>java Sample 1 2 3 O/P: Please enter 4 integer numbers Example2) C:\>java Sample 1 2 3 4 O/P:   The given array is :   1 2    3 4   The reverse of the array is :   4 3    2 1 //Use Two Dimensional Array.  >> If you have any alternate methods then please comment below. public class Sample { public static void main(String[] args) { int a = args.length; int arr[][] = new int[4][4]; if(a<4) { System.out.println("Please enter 4 integer numbers"); } if(a==4) { int p = 0; for (int i=0; i<2; i++) { for (int j=0; j<2; j++) { arr[i][j] = Integer.parseInt(args[p]); p++; } } System.out.println("The given array is: "); for (int i=0; i<2; i++) { for (int j=0; j<2; j++) { System.out.print(arr[i][j]+" "); } Syste

Given 2 int arrays, a and b, each length 3, form a new array of length 2, containing their middle elements. middleWay([1, 2, 3], [4, 5, 6]) → [2, 5] middleWay([7, 7, 7], [3, 8, 0]) → [7, 8] middleWay([5, 2, 9], [1, 4, 5]) → [2, 4]

Given 2 int arrays, a and b, each length 3, form a new array of length 2, containing their middle elements. middleWay([1, 2, 3], [4, 5, 6]) → [2, 5] middleWay([7, 7, 7], [3, 8, 0]) → [7, 8] middleWay([5, 2, 9], [1, 4, 5]) → [2, 4] //Use One Dimensional Array.  >> If you have any alternate methods then please comment below. import java.util.Arrays; public class MiddleWay { public static void main(String[] args) { int[] arr1 = {1,2,3}; int[] arr2 = {4,5,6}; System.out.println("Array1: "+Arrays.toString(arr1)); System.out.println("Array2: "+Arrays.toString(arr2)); int[] new_array = {arr1[1], arr2[1]}; System.out.println("New Array: "+Arrays.toString(new_array)); } } Write an interface called Playable, with a method void play(); Let this interface be placed in a package called music.  Write a class called Veena which implements Playable interface. Let this class be placed in a package music.string  Write a class calle

Given an array of type int, print true if every element is 1 or 4. only14([1, 4, 1, 4]) → true only14([1, 4, 2, 4]) → false only14([1, 1]) → true

Given an array of type int, print true if every element is 1 or 4.  only14([1, 4, 1, 4]) → true only14([1, 4, 2, 4]) → false only14([1, 1]) → true //Use One Dimensional Array.  >> If you have any alternate methods then please comment below. public class Only14 { public static void main(String[] args) { int arr[] = {1,4,1,4}; System.out.println(Only14.only14(arr)); } public static boolean only14(int arr[]) { boolean flag = true; for (int i=0; i<arr.length; i++) { if (arr[i] != 1 && arr[i] != 4) { flag = false; break; } } return flag; } } Write an interface called Playable, with a method void play(); Let this interface be placed in a package called music.  Write a class called Veena which implements Playable interface. Let this class be placed in a package music.string  Write a class called Saxophone which implements Playable interface. Let this class be placed in a package music.wind  Write another class Test in a packag

Print an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and print the given array, or make a new array. evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1] evenOdd([3, 3, 2]) → [2, 3, 3] evenOdd([2, 2, 2]) → [2, 2, 2]

Print an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and print the given array, or make a new array. evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1] evenOdd([3, 3, 2]) → [2, 3, 3] evenOdd([2, 2, 2]) → [2, 2, 2] //Use One Dimensional Array.  >> If you have any alternate methods then please comment below. public class EvenOdd { public static void main(String[] args) { int arr[] = {1,0,1,0,0,1,1}; int s[] = new int[arr.length]; System.out.println(s = evenOdd(arr)); for(int p:s) { System.out.print(p+" "); } } public static int[] evenOdd(int[] nums){ int noEvens = 0; int x[] = new int [nums.length]; for(int i=0, j=0; i<nums.length; i++) { if(nums[i] % 2 == 0) { noEvens++; x[j] = nums[i]; j++; } } for (int i=0, j=noEv

Print a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and display the given array or make a new array. withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0] withoutTen([10, 2, 10]) → [2, 0, 0] withoutTen([1, 99, 10]) → [1, 99, 0]

Print a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and display the given array or make a new array. withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0] withoutTen([10, 2, 10]) → [2, 0, 0] withoutTen([1, 99, 10]) → [1, 99, 0] //Use One Dimensional Array.  >> If you have any alternate methods then please comment below. public class RemoveTen { private static int[] arr = {1,10,10,2}; public static int[] RemoveTen(int[] nums) { int[] copy = new int[nums.length]; int j = 0; for (int i=0; i<nums.length; i++) if (nums[i] != 10) { copy[j] = nums[i]; j++; } return copy; } public static void main(String[] args) { int[] result = RemoveTen(arr); for (int i=0; i<result.length; i++) { System.out.println(result[i]+"