From 075524c6060102f55f21d28ee6879c6f16255175 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 4 Oct 2024 13:26:49 +0200 Subject: [PATCH] added micro lab --- src/learn/MicroLabTask1.java | 31 +++++++++++++++++++++++++ src/learn/MicroLabTask2.java | 45 ++++++++++++++++++++++++++++++++++++ src/learn/MicroLabTask3.java | 34 +++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/learn/MicroLabTask1.java create mode 100644 src/learn/MicroLabTask2.java create mode 100644 src/learn/MicroLabTask3.java diff --git a/src/learn/MicroLabTask1.java b/src/learn/MicroLabTask1.java new file mode 100644 index 0000000..aa9417c --- /dev/null +++ b/src/learn/MicroLabTask1.java @@ -0,0 +1,31 @@ +package learn; + +public class MicroLabTask1 { + public static int countE(String[] strings) { + int count = 0; + for (String s : strings) { + for (char c: s.toCharArray()) { + if (c == 'e') { + count++; + } + } + } + return count; + } + + public static > int findFirstOutOfPlace(T[] arr) { + for (int i = 0; i < arr.length - 1; i++) { + if (arr[i+1].compareTo(arr[i]) < 0) { + return i; + } + } + + return -1; + } + + public static void main(String[] args) { + System.out.println(countE(new String[]{"hello", "my", "dear", "friend", "and", "enemies"})); + System.out.println(findFirstOutOfPlace(new Integer[]{0,1,2,3})); + System.out.println(findFirstOutOfPlace(new Integer[]{0,1,3,2})); + } +} diff --git a/src/learn/MicroLabTask2.java b/src/learn/MicroLabTask2.java new file mode 100644 index 0000000..fa115ce --- /dev/null +++ b/src/learn/MicroLabTask2.java @@ -0,0 +1,45 @@ +package learn; + +class Car { + int speed; + String brand; + + Car(int speed, String brand) { + this.speed = speed; + this.brand = brand; + } + + @Override + public String toString() { + return "Car{speed=" + speed + "km/h, brand='" + brand + "'}"; + } +} + +class Person { + String firstname; + String lastname; + int age; + int height; + + Person(String firstname, String lastname, int age, int height) { + this.firstname = firstname; + this.lastname = lastname; + this.age = age; + this.height = height; + } + + Person(String firstname) { + this(firstname, "", 0, 0); + } +} + +public class MicroLabTask2 { + public static void main(String[] args) { + Car car1 = new Car(250, "Porsche 911"); + System.out.println(car1); + + Person p = new Person("Homer"); + String v = p.lastname.toUpperCase(); + System.out.println(v); + } +} diff --git a/src/learn/MicroLabTask3.java b/src/learn/MicroLabTask3.java new file mode 100644 index 0000000..ea75536 --- /dev/null +++ b/src/learn/MicroLabTask3.java @@ -0,0 +1,34 @@ +package learn; + +class Printer { + private static int totalNumberOfPrinters = 0; + private String printerName; + + Printer(String name) { + this.printerName = name; + totalNumberOfPrinters++; + } + + public boolean setPrinterName(String printerName) { + boolean different = !this.printerName.equals(printerName); + this.printerName = printerName; + return different; + } + + public static int getTotalNumberOfPrinters() { + return totalNumberOfPrinters; + } + + private void toLowerCase() { + this.printerName = this.printerName.toLowerCase(); + } +} + +public class MicroLabTask3 { + public static void main(String[] args) { + Printer p1 = new Printer("First printer"); + Printer p2 = new Printer("Printer the Second"); + Printer p3 = new Printer("Printerus III"); + System.out.println(Printer.getTotalNumberOfPrinters()); + } +}