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

Course Ex

java

Uploaded by

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

Course Ex

java

Uploaded by

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

//////////////EXERCITIUL 1 ///////////

int LengthArrayOne, LengthArrayTwo, i, j, SameNumber = 0;

Scanner input = new Scanner(System.in);


System.out.println("Length of ArrayONE:");
LengthArrayOne = input.nextInt();

int ArrayOne[] = new int[LengthArrayOne];


System.out.println("Enter the ArrayONE elements:");
for (i = 0; i < LengthArrayOne; i++) {
ArrayOne[i] = input.nextInt();
}

System.out.println("Length of ArrayTWO");
LengthArrayTwo = input.nextInt();

int ArrayTwo[] = new int[LengthArrayTwo];


System.out.println("Enter the ArrayTWO elements:");
for (i = 0; i < LengthArrayTwo; i++) {
ArrayTwo[i] = input.nextInt();
}

if (LengthArrayOne > LengthArrayTwo) {


for (i = 0; i < LengthArrayOne; i++)
for (j = 0; j < LengthArrayTwo; j++)
if (ArrayOne[i] == ArrayTwo[j]) {
SameNumber++;
}
}
else
{
for (j = 0; j < LengthArrayTwo; j++) {
for (i = 0; i < LengthArrayOne; i++)
if (ArrayTwo[j] == ArrayOne[i]) {
SameNumber++;
}
}
}

System.out.println("Number of identical elements=" + SameNumber);

///////////////// EXERCITIUL 2 ////////////

int LengthArray, i;

Scanner input = new Scanner(System.in);


System.out.println("Length of Array:");
LengthArray = input.nextInt();

int[] Array = new int[LengthArray];

System.out.println("Enter the elements of Array");


for (i = 0; i < LengthArray; i++) {
Array[i] = input.nextInt();
}
int[] ReverseArray = new int[LengthArray];
int IndexOfReverseArray = 0;
for (i = LengthArray - 1; i >= 0; i--) {
ReverseArray[IndexOfReverseArray] = Array[i];
IndexOfReverseArray++;
}

if (Arrays.equals(Array, ReverseArray))
System.out.println("Array it is a Palindrome array");
else
System.out.println("Array it is not a Palindrome array");

////////////// EXERCITIUL 3 ////////////////////

int row, columns = 0;


int SumColumns = 0;

Scanner input = new Scanner(System.in);


System.out.println("Enter the number of rows:");
row = input.nextInt();
System.out.println("Enter the number or columns ");
columns = input.nextInt();
int[][] M = new int[row][columns];

// enter array elements


for (int i = 0; i < row; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Row " + (i + 1) + " " + "Column " + (j
+ 1) + "=");
M[i][j] = input.nextInt();
}
}
// print 2d array elements
System.out.println("The 2D Array is: \n ");
for (int i = 0; i < row; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(M[i][j] + " ");
}
System.out.println();
}
// Calculate sum of each item in the column
int[] Array = new int[columns];

int IndexColumns = 0;
while (IndexColumns < columns) {
int IndexRow = 0;
while (IndexRow < row) {
SumColumns = SumColumns + M[IndexRow][IndexColumns];
IndexRow++;
}
Array[IndexColumns] = SumColumns;
SumColumns = 0;
IndexColumns++;
}
// print sum of each item in the column
System.out.println("Sum of each item in the column");
for (int i = 0; i < columns; i++) {
System.out.print(Array[i] + " ");
}
////////////////////// EXERCITIUL 4 ////////////////

String[] arr = { "Now", "is", "the", "time", "for", "all", "good",


"men", "to", "come", "to", "the", "aid",
"of", "their", "country"};

for (int i = 0; i < arr.length; i++) {


bubbleSort(arr);
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

public static void bubbleSort(String arr[]) {

for (int j = 0; j < arr.length-1; j++) {


for (int i = j + 1; i < arr.length; i++) {
if (arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
}
}

////////////////// EXERCITIUL 5 ///////////////////

String[] arr = { "Now", "is", "the", "time", "for", "all", "good", "men", "to",
"come", "to", "the", "aid",
"of", "their", "country" };

System.out.print("Sirul concatenat="
+ " ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();

String str = String.join(",", arr);


System.out.print("Sirul concatenat cu join= " + "" + str);
System.out.println();

String[] splitstr = str.split(",", str.length());


System.out.print("Concatenare cu metoda split= ");
for(String n : splitstr) {
System.out.print(n);
}

//////////////////// EXERCITIUL 6 /////////////////

double d = 102939939.939E+20;
boolean b = true;
long l = 1232874;
char[] arr1 = { 'a', 'b', 'c', 'd', 'e', 'f' };

String DoubleString = String.valueOf(d);


String BooleanString = String.valueOf(b);
String LongString = String.valueOf(l);
String CharString = String.valueOf(arr1);

System.out.println("DoubleString= " + DoubleString);


System.out.println("BooleanString= " + BooleanString);
System.out.println("LongString= " + LongString);
System.out.println("CharString= " + CharString);

You might also like