Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida, Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1: Lab sheet 3

Arrays
1. Program to print the given one dimentional array and the sorted array using enhanced for loop.
class Demo {
public static void main(String args[]) {
int A[] = { 5, 8, 2, 9, 1, 4, 7, 56, 15 };
System.out.println("The given array is ");
//for (int i = 0; i < A.length; i++) {
for (int i : A) {
System.out.print(i);
System.out.print("\t");
}
Arrays.sort(A);
System.out.println();
System.out.println("The sorted array is");
//for (int i = 0; i < A.length; i++) {
for (int i: A) {
System.out.print(i);
System.out.print("\t");
}
}
}
2. Program to print Reverse of an Array
import java.io.*;
class RevArray {
public static void main(String S[]) throws IOException {
int a[] = new int[5];
int i, temp;
DataInputStream ins = new DataInputStream(System.in);
System.out.println("Enter 5 elements");
for (i = 0; i < 5; i++) {
a[i] = Integer.parseInt(ins.readLine());
}
System.out.println("The given Array is");
for (int j:a)
System.out.print(j + " ");
System.out.println();
for (i = 0; i <2; i++) {
temp = a[i];
a[i] = a[4 - i];
a[4 - i] = temp;
}
System.out.println("The reverse of the given Array is");
for (int k:a)
System.out.print(k + " ");
}
}
3. Program for Passing Arrays in methods
public class PassingArray {
public static void main(String args[]) {
int kk[]={1,2,3,4,5};
System.out.println("The original array is");
for (int i:kk)

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println(i);
change(kk);
System.out.println("The changed array is");
for (int j:kk)
System.out.println(j);
}
public static void change(int x[]){
for (int counter=0;counter<x.length;counter++)
x[counter]+=5;
}
}
4. Program for Matrix Multipication
import java.io.*;
class Multiplication {
public static void main(String a[]) throws IOException {
int i, j, k;
int b[][] = new int[2][2];
int c[][] = new int[2][2];
int d[][] = new int[2][2];
System.out.println("Enter 4 elements");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
DataInputStream in = new DataInputStream(System.in);
b[i][j] = Integer.parseInt(in.readLine());
}
}
System.out.println("Enter 4 elements");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)

{
DataInputStream ins = new DataInputStream(System.in);
c[i][j] = Integer.parseInt(ins.readLine());
}
}
System.out.println("result");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
d[i][j] += (b[i][k] * c[k][j]);
}
System.out.print(d[i][j] + " ");
d[i][j] = 0;
}
System.out.println("");
}
}
}
5. Program to delete an element from array
import java.io.*;
class DelArray {
public static void main(String arg[]) throws IOException {
int a[] = new int[10];
DataInputStream ins = new DataInputStream(System.in);
int i, n = 0, q = 0;
int ctr = 0;
System.out.print("Enter no. of elements ");

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


n = Integer.parseInt(ins.readLine());
System.out.println("Enter elements ");
for (i = 0; i < n; i++)
a[i] = Integer.parseInt(ins.readLine());
System.out.println("Enter element to be deleted ");
q = Integer.parseInt(ins.readLine());
for (i = 0; i < n; i++) {
if (a[i] == q) {
for (int k = i; k < n - 1; k++)
a[k] = a[k + 1];
ctr++;
}
}
if (ctr == 0)
System.out.println("Element not found");
else {
for (i = 0; i < (n - ctr); i++)
System.out.print(a[i] + "\t");
}
}
}
6. Program to sort an array and insert an element inside it?
import java.util.Arrays;
public class Insert {
public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
printArray("The given array is",array);
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 1);
System.out.println("Didn't find 1 at " + index);
int newIndex = -index - 1;
array = insertElement(array, 1, newIndex);
printArray("With 1 added", array);
}
private static void printArray(String message, int array[]) {
System.out.println(message + ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if (i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
private static int[] insertElement(int original[], int element, int index) {
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index);
destination[index] = element;
System.arraycopy(original, index, destination, index + 1, length - index);
return destination;
}
}

Best of luck
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like