diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38a2608 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea/ +/Lesson_1/Lesson_1.iml +/out/ diff --git a/Lesson_1/src/Calculator.java b/Lesson_1/src/Calculator.java new file mode 100644 index 0000000..9871a35 --- /dev/null +++ b/Lesson_1/src/Calculator.java @@ -0,0 +1,56 @@ +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + Scanner sa = readInput("Enter first number: "); + int a = sa.nextInt(); + Scanner sb = readInput("Enter second number: "); + int b = sb.nextInt(); + Scanner sop = readInput("Enter operation type (+, -, *, /, ^, %): "); + String op = sop.next(); + System.out.println("Result is: " + getResult(a, b, op)); + } + + private static Scanner readInput(String prompt) { + System.out.println(prompt); + return new Scanner(System.in); + } + + private static Boolean isOpValid(String op) { + String[] opTypes = {"+", "-", "*", "/", "^", "%"}; + + for (String opType: opTypes) { + if (opType.equals(op)) { + return true; + } + } + return false; + } + + private static int getResult(int a, int b, String op) { + if (isOpValid(op)) { + if (op.equals("+")) { + return a + b; + } else if (op.equals("-")) { + return a - b; + } else if (op.equals("*")) { + return a * b; + } else if (op.equals("/")) { + return a / b; + } else if (op.equals("^")) { + return customPow(a, b); + } else { + return a % b; + } + } + return 0; + } + + private static int customPow(int a, int b) { + int result = 1; + for (int i = 0; i < b; i++) { + result *= a; + } + return result; + } +} diff --git a/Lesson_1/src/ConditionalStatement.java b/Lesson_1/src/ConditionalStatement.java new file mode 100644 index 0000000..b176a6f --- /dev/null +++ b/Lesson_1/src/ConditionalStatement.java @@ -0,0 +1,35 @@ +public class ConditionalStatement { + static int age = 16; + static boolean isMale = true; + static float height = 1.81F; + static String name = "Michael"; + + public static void main(String[] args) { + if (age > 20) { + System.out.println("age > 20"); + } + + if (isMale) { + System.out.println("isMale"); + } + + if (!isMale) { + System.out.println("!isMale"); + } + + if (height < 1.80F) { + System.out.println("height < 1.80F"); + } else { + System.out.println("height >= 1.80F"); + } + + if (name.startsWith("M")) { + System.out.println("name.startsWith(\"M\")"); + } else if (name.startsWith("И")) { + System.out.println("name.startsWith(\"И\")"); + } else { + System.out.println("name.startsWith(\"smth else\")"); + } + + } +} diff --git a/Lesson_1/src/Cycle.java b/Lesson_1/src/Cycle.java new file mode 100644 index 0000000..ce39267 --- /dev/null +++ b/Lesson_1/src/Cycle.java @@ -0,0 +1,23 @@ +public class Cycle { + public static void main(String[] args) { + for (int i = 0; i <= 20; i++) { + System.out.println(i); + } + + int i = 6; + while (i > -7) { + System.out.println(i); + i -= 2; + } + + i = 10; + int result = 0; + do { + if (i % 2 != 0) { + result += i; + } + i++; + } while (i < 20); + System.out.println(result); + } +} diff --git a/Lesson_1/src/MyFirstApp.java b/Lesson_1/src/MyFirstApp.java new file mode 100644 index 0000000..e93101c --- /dev/null +++ b/Lesson_1/src/MyFirstApp.java @@ -0,0 +1,5 @@ +public class MyFirstApp { + public static void main(String[] args) { + System.out.println("Hello, world"); + } +} diff --git a/Lesson_1/src/MyFirstGame.java b/Lesson_1/src/MyFirstGame.java new file mode 100644 index 0000000..9f8abee --- /dev/null +++ b/Lesson_1/src/MyFirstGame.java @@ -0,0 +1,40 @@ +import java.time.LocalDateTime; +import java.util.InputMismatchException; +import java.util.Scanner; + +public class MyFirstGame { + public static void main(String[] args) { + byte guess = random(); +// byte guess = 12; + Scanner input; + System.out.println("I guess a random number from 0 to 100. What's it? "); + byte userInput = 0; + int triesNumber = 0; + do { + triesNumber++; + input = new Scanner(System.in); + try { + userInput = input.nextByte(); + if (guess < userInput) { + System.out.println("Wrong number! Your value is greater than my. Try again: "); + } else if (guess > userInput) { + System.out.println("Wrong number! Your value is less than my. Try again: "); + } + } catch (InputMismatchException ex) { + System.out.println("Please use only numbers (from 0 to 100)"); + } + + } while (guess != userInput); + System.out.println("Good job, you found it! With " + triesNumber + " tries"); + } + + private static byte random() { + LocalDateTime now = LocalDateTime.now(); + int seconds = now.getSecond(); + if (seconds % 2 == 0) { + return (byte) (now.getNano() / 10000000); + } else { + return (byte) (now.getNano() / 10000000 + 1); + } + } +} diff --git a/Lesson_1/src/Unicode.java b/Lesson_1/src/Unicode.java new file mode 100644 index 0000000..744d198 --- /dev/null +++ b/Lesson_1/src/Unicode.java @@ -0,0 +1,8 @@ +public class Unicode { + public static void main(String[] args) { + for (int i = 9398; i < 10178; i++) { +// System.out.println(Character.toString((char)i)); + System.out.println((char)i); + } + } +} diff --git a/Lesson_1/src/Variable.java b/Lesson_1/src/Variable.java new file mode 100644 index 0000000..51f6bd8 --- /dev/null +++ b/Lesson_1/src/Variable.java @@ -0,0 +1,22 @@ +public class Variable { + public static void main(String[] args) { + byte cpuCores = 4; + short numOfPackages = 3600; + int gpuMemSizeInKB = 8 * 1024 * 1024; + long totalDiskSpaceInKB = 256 * 1024 * 1024; + float cpuFrequencyGHz = 3.60f; + double memorySizeGB = 6.1; +// char[] osType = "Ubuntu".toCharArray(); + char[] osType = {85, 98, 117, 110, 116, 117} /* "Ubuntu".toCharArray() the same */; + boolean isUpToDate = true; + + System.out.println("CPU cores count: " + cpuCores); + System.out.println("Number of packages: " + numOfPackages); + System.out.println("GPU memory size in KBs: " + gpuMemSizeInKB); + System.out.println("Total disk space in KBs: " + totalDiskSpaceInKB); + System.out.println("CPU frequency in GHz: " + cpuFrequencyGHz); + System.out.println("Memory size in GBs: " + memorySizeGB); + System.out.println("OS type: " + new String(osType)); + System.out.println("Is up-to-date: " + isUpToDate); + } +}