Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion LAB5/src/lab5/LAB5.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void main(String[] args) throws NoSuchMethodException {
QuestionManager questionManager = new QuestionManager();
Scanner input = new Scanner(System.in);
String selectQuestion;
System.out.println("Which question you want to check ? Please enter Q1 , Q2Method1 , Q2Method2");
System.out.println("Which question you want to check ? Please enter Q1 , Q2Method1 , Q2Method2 , Q3 , Q4Method1 , Q4Method2 , Q5 , Q6");
selectQuestion = input.next();
switch (selectQuestion) {
case "Q1":
Expand All @@ -36,6 +36,18 @@ public static void main(String[] args) throws NoSuchMethodException {
case "Q3":
questionManager.Q3();
break;
case "Q4Method1":
questionManager.Q4Method1();
break;
case "Q4Method2":
questionManager.Q4Method2();
break;
case "Q5":
questionManager.Q5();
break;
case "Q6":
questionManager.Q6();
break;
default:
System.out.println("YO");
;
Expand Down
171 changes: 170 additions & 1 deletion LAB5/src/lab5/QuestionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
package lab5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
import javafx.beans.binding.Bindings;

/**
*
Expand Down Expand Up @@ -126,4 +126,173 @@ public void Q3() {
}
}

public void Q4Method1() {
int[][] matrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = i * 3 + j;
}
}
System.out.println("This is previuos view : ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}

for (int i = 0; i < matrix.length; i++) {
for (int j = i + 1; j < matrix.length; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
int len = matrix.length;
for (int i = 0; i < len / 2; i++) {
for (int j = 0; j < len; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[len - 1 - i][j];
matrix[len - 1 - i][j] = temp;
}
}
System.out.println("After 90 degree rotate : ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}
}

public void Q4Method2() {
int[][] matrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = i * 3 + j;
}
}
System.out.println("This is previuos view : ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}

int temp;
final int len = matrix.length;
// For each concentric square around the middle of the matrix to rotate...
// This value will be used as (m, n) offset when moving in.
// Integer division by 2 will skip center if odd length.
for (int s = 0; s < len / 2; s++) // for the length of this ring
{
for (int i = 0; i < len - 2 * s - 1; i++) {
temp = matrix[s][s + i];
matrix[s][s + i] = matrix[len - s - i - 1][s];
matrix[len - s - i - 1][s] = matrix[len - s - 1][len - s - i - 1];
matrix[len - s - 1][len - s - i - 1] = matrix[s + i][len - s - 1];
matrix[s + i][len - s - 1] = temp;
}
}
System.out.println("After 90 degree rotate : ");
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix[row].length; column++) {
System.out.print(matrix[row][column] + " ");
}
System.out.println();
}
}

public void Q5() {
int[] list = new int[20];

for (int i = 0; i < 10; i++) {
list[i] = (int) (Math.random() * 100);
}

System.out.println(Arrays.toString(list));

for (int i = 0; i < list.length - 1; i++) {
// Find the minimum in the list[i..list.length-1]
int currentMax = list[i];
int currentMaxIndex = i;

for (int j = i + 1; j < list.length; j++) {
if (list[j] > currentMax) {
currentMax = list[j];
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if necessary
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
System.out.println(Arrays.toString(list));

Scanner input = new Scanner(System.in);
System.out.println("Please enter a number to search :");
int num;
num = input.nextInt();
int countLinear = 0;
int countBinary = 0;
int low = list.length - 1;
int high = 0;

while (low >= high) {
int mid = (low + high) / 2;
if (num < list[mid]) {
countBinary++;
high = mid + 1;
} else if (num == list[mid]) {
System.out.println("The number was found in the list.");
System.out.println("Binary Search - " + countBinary + " loops .");
break;
} else {
countBinary++;
low = mid - 1;
}
}

for (int i = 0; i < list.length; i++) {
countLinear++;
if (num == list[i]) {
System.out.println("The number was found in the list.");
break;
} else if (i == list.length - 1 && num != list[i]) {
System.out.println("The number was not found in the list.");
}
}
System.out.println("Linear Search - " + countLinear + " loops .");
}

public void Q6() {

Scanner input = new Scanner(System.in);
System.out.println("Enter how manny rows you want :");
int num = input.nextInt();
int matrix[][] = new int[num][num];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
matrix[i][j] = 0;
}
}
for (int i = 0; i < num; i++) {
matrix[i][0] = 1;
}
for (int i = 1; i < num; i++) {
for (int j = 1; j < num; j++) {
matrix[i][j] = matrix[i - 1][j - 1] + matrix[i - 1][j];
}
}
for (int i = 0; i < num; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println(Arrays.toString(matrix));
}
}