From c23a6df2b1365bd5f28b66a59b002c2f72c50f35 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 25 Oct 2022 08:47:58 +0200 Subject: [PATCH] Lab 3 --- .gitignore | 29 +++++++++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 71 +++++++++++++++++++++ lab3.iml | 11 ++++ src/Main.java | 151 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 lab3.iml create mode 100644 src/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a63095d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..9b45ae2 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "settings.editor.selected.configurable": "preferences.pluginManager" + } +} + + + + + + + + + + + + + + 1666678375462 + + + + + + + \ No newline at end of file diff --git a/lab3.iml b/lab3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/lab3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..9689281 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,151 @@ +import java.util.Locale; +import java.util.Random; +import java.util.Scanner; + +public class Main { + public static int[] z1_losowanie(int a, int b) { + Random rand = new Random(); + + int n = 10; + + int[] tab = new int[n]; + + for(int i = 0; i < n; i++) { + tab[i] = rand.nextInt(b - a + 1) + a; + } + + return tab; + } + + public static void z1() { + int suma = 0; + + int[] tab = z1_losowanie(10, 50); + for (int j : tab) { + suma += j; + System.out.print(j + " "); + } + System.out.println(); + + System.out.println("Suma: " + suma); + System.out.println("Średnia: " + (suma / 10)); + } + + public static void z2() { + int[] parzystaIloscTab = {2, 3, 5, 7, 11, 13}; + int[] nieparzystaIloscTab = {42, 69, 120, 240, 303}; + + for(int i = 0; i < parzystaIloscTab.length; i += 2) { + System.out.println("Parzysta: " + parzystaIloscTab[i]); + System.out.println("Nieparzysta: " + nieparzystaIloscTab[i]); + } + } + + public static void z3() { + String[] tab = { "Stefan", "Andrzej", "Zdzichu" }; + + for (String s : tab) { + System.out.println(s.toUpperCase(Locale.ROOT)); + } + } + + public static void z4() { + final int size = 5; + + String[] tab = new String[size]; + + Scanner scanner = new Scanner(System.in); + + for(int i = 0; i < size; i++) { + System.out.print("Podaj " + (i + 1) + " słowo: "); + tab[i] = scanner.next(); + } + + for(int i = (size - 1); i >= 0; i--) { + for(int j = (tab[i].length() - 1); j >= 0; j--) { + System.out.print(tab[i].charAt(j)); + } + System.out.println(); + } + } + + public static void z5() { + int[] tab = { -15, -2, 0, 1, 10, 10, 20, 100 }; + int tmp; + + for(int i = 0; i < tab.length; i++) { + for(int j = 0; j < i; j++) { + if(tab[i] < tab[j]) { + tmp = tab[i]; + tab[i] = tab[j]; + tab[j] = tmp; + } + } + } + + for(int i = 0; i < tab.length; i++) { + System.out.println((i + 1) + ". " + tab[i]); + } + } + + public static long z6_silnia(int n) { + if(n == 0 || n == 1) return 1; + return n * z6_silnia(n - 1); + } + + public static void z6() { + Scanner scanner = new Scanner(System.in); + + final int size = 5; + int[] tab = new int[size]; + long[] res = new long[size]; + + for(int i = 0; i < size; i++) { + System.out.print("Podaj " + (i + 1) + " liczbę: "); + tab[i] = scanner.nextInt(); + res[i] = z6_silnia(tab[i]); + } + + for(int i = 0; i < size; i++) { + System.out.println(tab[i] + "! = " + res[i]); + } + } + + public static void z7() { + final int size = 4; + String[] tab1 = { "Stefan", "Andrzej", "Zdzichu", "Eustachy" }; + String[] tab2 = { "Stefan", "Andrzej", "Zdzichu", "Eustachy" }; + + for(int i = 0; i < size; i++) { + if(!tab1[i].equals(tab2[i])) { + System.out.println("Nie są równe na indeksie " + i); + return; + } + } + + System.out.println("Są równe"); + } + + public static void main(String[] args) { + System.out.println("=== Zadanie 1 (suma i średnia) ==="); + z1(); + + System.out.println("=== Zadanie 2 (co drugi) ==="); + z2(); + + System.out.println("=== Zadanie 3 (duże litery) ==="); + z3(); + + System.out.println("=== Zadanie 4 (wspak) ==="); + z4(); + + System.out.println("=== Zadanie 5 (sortowanie) ==="); + z5(); + + System.out.println("=== Zadanie 6 (silnia) ==="); + z6(); + + System.out.println("=== Zadanie 7 (porównywanie) ==="); + z7(); + } +} \ No newline at end of file