0% found this document useful (0 votes)
50 views

Array Sorting Program: 2-D or Multidimensional Array

This document contains code snippets for several Java programs that demonstrate different programming concepts: 1) An array sorting program that imports utilities, declares and initializes an integer array, sorts the array, and prints the sorted values. 2) A 2D array program that initializes a 2D integer array, takes user input to populate the array, and prints the values. 3) A program that finds the minimum and maximum values in an integer array by sorting the array and accessing the first and last elements. 4) A program demonstrating a main class calling methods from an outer class, including user input and method overloading. 5) A factorial program that uses a for loop to calculate the factorial of a

Uploaded by

Khan Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Array Sorting Program: 2-D or Multidimensional Array

This document contains code snippets for several Java programs that demonstrate different programming concepts: 1) An array sorting program that imports utilities, declares and initializes an integer array, sorts the array, and prints the sorted values. 2) A 2D array program that initializes a 2D integer array, takes user input to populate the array, and prints the values. 3) A program that finds the minimum and maximum values in an integer array by sorting the array and accessing the first and last elements. 4) A program demonstrating a main class calling methods from an outer class, including user input and method overloading. 5) A factorial program that uses a for loop to calculate the factorial of a

Uploaded by

Khan Arif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

Array Sorting Program

import java.util.Arrays;

public class ArraySorting {

public static void main(String[] args) {


int arr[] = {1,4,3,50};
Arrays.sort(arr);
for(int x: arr)
{
System.out.println("Values of array = "+x +"\n");
}
}

2-D or Multidimensional Array


import java.util.Scanner;

public class TwoDimenstionalArray {

public static void main(String[] args)

{
int arif[][]= new int[2][3];
Scanner obj= new Scanner(System.in);
System.out.println("Enter 6 Elements");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
arif[i][j]=obj.nextInt();
}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Value is: "+arif[i][j]);
}
}
}

Find Minimum And Maximum in Array:


import java.util.Arrays;

public class FindMaximumMinimumInArray {

public static void main(String[] args)


{
int [] num ={-6,-3,2,0,5};
Arrays.sort(num);
System.out.println("Minimum = " +num[0]);
System.out.println("Maximum = " + num[num.length-1]);
}

Methods:
Creating an outer Class and Calling it's Methods in Main
program
Below is the Code For Main Program.....
public static void main(String[] args)
{

{
helper obj1 = new helper();
obj1.add();
obj1.sub();

int e,f;
Scanner obj= new Scanner(System.in);
System.out.print("Enter First value");
e=obj.nextInt();
System.out.print("enter second Value");
f=obj.nextInt();
obj1.mul(e,f):
}
}

Code For Outer Class...


class helper {
public static void add()
{
int a=5,b=3;
System.out.println("Addition is= "+(a+b));
}
public static void sub()
{
int c=15,d=4;
System.out.println("Subtraction is "+(c-d));
}
public static void mul(int x,int y)
{
System.out.println("Multiplication is= "+(x*y));
}

Factorial Program:
import java.util.Scanner;

public class FactoriaProgram


{

public static void main(String[] args) {


fact();
}
public static void fact()
{
int i,number;
int fact=1;
System.out.println("enter number to factorial");
Scanner obj = new Scanner(System.in);
number = obj.nextInt();
for(i=1; i<=number;i++)
{
fact = fact*i;
}
System.out.println("Factorial of "+number+ "is = "+ fact);
}
}

Inheritence Program:
import java.util.Scanner;

class A
{
int a,b;
}

class B extends A
{
void arif(int a, int b)
{
System.out.println("Addition of 2 Values is= " +(a+b));
}
}

public class Inheritence {

public static void main(String[] args)


{
int c,d;
Scanner sc= new Scanner(System.in);
c=sc.nextInt();
d=sc.nextInt();
B obj = new B();
obj.arif(c,d);

You might also like