diff --git a/AreaOfATriangle/AreaOfATriangle.iml b/AreaOfATriangle/AreaOfATriangle.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/AreaOfATriangle/AreaOfATriangle.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AreaOfATriangle/src/AreaOfATriangle.java b/AreaOfATriangle/src/AreaOfATriangle.java new file mode 100644 index 0000000..0ec2743 --- /dev/null +++ b/AreaOfATriangle/src/AreaOfATriangle.java @@ -0,0 +1,93 @@ +import java.util.Scanner; + +public class AreaOfATriangle { + int[] triangle = new int[3]; + + public void inputSide() { + Scanner sc = new Scanner(System.in); + boolean flag = false; + while (!this.isTriangleValid()) { + if (flag) { + System.out.println("Please enter the right triangle sides!"); + } + for (int i = 0; i < 3; i++) { + boolean isChecked = false; + while (!isChecked) { + try { + System.out.print("Input Side " + (i + 1) + ": "); + int value = sc.nextInt(); + if (value <= 0) { + throw new NumberFormatException("Please enter a positive number greater than 0"); + } + this.triangle[i] = value; + isChecked = true; + } catch (Exception e) { + if (e.getClass() == NumberFormatException.class) { + System.out.println(e.getMessage()); + } else { + System.out.println("Please enter a valid number"); + } + sc.nextLine(); + } + } + } + flag = true; + } + + sc.close(); + } + + private boolean isTriangleValid() { + int len = this.triangle.length; + if (this.triangle[0] == 0 || this.triangle[1] == 0 || this.triangle[2] == 0) { + return false; + } + int maxSideIdx = 0; + for (int i = 0; i < len; i++) { + int max = Math.max(this.triangle[i], this.triangle[maxSideIdx]); + if (max != this.triangle[maxSideIdx]) { + maxSideIdx = i; + } + } + int start = 0; + int end = len - 1; + while (start < end) { + if (start == maxSideIdx) { + start++; + } else if (end == maxSideIdx) { + end--; + } + if (this.triangle[start] + this.triangle[end] < this.triangle[maxSideIdx]) { + return false; + } + start++; + end--; + } + return true; + } + + public void printResult() { + double p = this.calculateHalfOfPerimeter(); + double area = this.calculateArea(p); + + System.out.println("The area of the triangle is " + area); + } + + private double calculateHalfOfPerimeter() { + double p = 0; + for (int j : this.triangle) { + p += j; + } + p /= 2; + return p; + } + + private double calculateArea(double p) { + double area = p; + for (int side : this.triangle) { + area *= (p - side); + } + area = Math.sqrt(area); + return area; + } +} \ No newline at end of file diff --git a/AreaOfATriangle/src/Main.java b/AreaOfATriangle/src/Main.java new file mode 100644 index 0000000..fc9431d --- /dev/null +++ b/AreaOfATriangle/src/Main.java @@ -0,0 +1,7 @@ +public class Main { + public static void main(String[] args) { + AreaOfATriangle areaOfATriangle = new AreaOfATriangle(); + areaOfATriangle.inputSide(); + areaOfATriangle.printResult(); + } +} diff --git a/Person/Person.iml b/Person/Person.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/Person/Person.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Person/src/Main.java b/Person/src/Main.java new file mode 100644 index 0000000..bcdef3d --- /dev/null +++ b/Person/src/Main.java @@ -0,0 +1,9 @@ +public class Main { + public static void main(String[] args) { + Person person = new Person(); + person.input(); + person.output(); + person.changeName("John", "Dou"); + person.output(); + } +} diff --git a/Person/src/Person.java b/Person/src/Person.java new file mode 100644 index 0000000..ef27b37 --- /dev/null +++ b/Person/src/Person.java @@ -0,0 +1,93 @@ +import java.time.LocalDate; +import java.util.Scanner; + +public class Person { + private String firstName; + private String lastName; + private int birthYear; + + public Person() { + } + + public Person(String firstName, String lastName) { + this.setFirstName(firstName); + this.setLastName(lastName); + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setBirthYear(int birthYear) { + this.birthYear = birthYear; + } + + public String getFirstName() { + return firstName; + } + public String getLastName() { + return lastName; + } + public int getBirthYear() { + return birthYear; + } + private int getAge(){ + int currentYear = LocalDate.now().getYear(); + int birthYear = getBirthYear(); + return currentYear-birthYear; + } + public void input(){ + Scanner sc = new Scanner(System.in); + int currentYear = LocalDate.now().getYear(); + System.out.println("Enter first name: "); + String firstName = sc.nextLine(); + this.setFirstName(firstName); + System.out.println("Enter last name: "); + String lastName = sc.nextLine(); + this.setLastName(lastName); + boolean isChecked = false; + while (!isChecked) { + try { + System.out.println("Enter birth year: "); + int birthYear = sc.nextInt(); + if (birthYear <= 0 || birthYear > currentYear) { + throw new NumberFormatException("Please enter a valid birth year!"); + } + this.setBirthYear(birthYear); + isChecked = true; + } catch (Exception e) { + if (e.getClass() == NumberFormatException.class) { + System.out.println(e.getMessage()); + } else { + System.out.println("Enter integer value!"); + } + } + sc.nextLine(); + } + sc.close(); + } + public void output(){ + System.out.println("\nInformation about person:"); + System.out.println("First name: " + this.getFirstName()); + System.out.println("Last name: " + this.getLastName()); + System.out.println("Birth year: " + this.getBirthYear()); + System.out.println("Age: " + this.getAge()); + } + public void changeName(String ...info) { + String fn = info[0]; + String ln = info[1]; + if (info.length > 2) { + throw new NumberFormatException("More than 2 parameters are not allowed!"); + } + if (!fn.isEmpty()) { + this.setFirstName(fn); + } + if (!ln.isEmpty()) { + this.setLastName(ln); + } + } +} diff --git a/SmallestNumber/SmallestNumber.iml b/SmallestNumber/SmallestNumber.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/SmallestNumber/SmallestNumber.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SmallestNumber/SmallestNumber.java b/SmallestNumber/SmallestNumber.java new file mode 100644 index 0000000..7783d5b --- /dev/null +++ b/SmallestNumber/SmallestNumber.java @@ -0,0 +1,2 @@ +public class SmallestNumber { +} diff --git a/SmallestNumber/src/Main.java b/SmallestNumber/src/Main.java new file mode 100644 index 0000000..d34f315 --- /dev/null +++ b/SmallestNumber/src/Main.java @@ -0,0 +1,7 @@ +public class Main { + public static void main(String[] args) { + SmallestNumber smallestNumber = new SmallestNumber(); + smallestNumber.inputNumber(); + smallestNumber.printResult(); + } +} \ No newline at end of file diff --git a/SmallestNumber/src/SmallestNumber.java b/SmallestNumber/src/SmallestNumber.java new file mode 100644 index 0000000..f66b631 --- /dev/null +++ b/SmallestNumber/src/SmallestNumber.java @@ -0,0 +1,41 @@ +import java.util.Scanner; + +public class SmallestNumber { + int[] number = new int[3]; + String[] counter = {"first", "second", "third"}; + + public void inputNumber() { + Scanner sc = new Scanner(System.in); + for (int i = 0; i < number.length; i++) { + boolean isChecked = false; + while (!isChecked) { + try { + System.out.print("Input the " + counter[i] + " number: "); + int value = sc.nextInt(); + this.number[i] = value; + isChecked = true; + } catch (Exception e) { + System.out.println("Please enter an integer!"); + } + sc.nextLine(); + } + } + sc.close(); + } + + private int getSmallestNumber() { + int minI = 0; + for (int i = 0; i < number.length; i++) { + if (this.number[minI] > this.number[i]) { + minI = i; + } + } + return this.number[minI]; + } + + public void printResult() { + int res = this.getSmallestNumber(); + System.out.println("The smallest number is " + res); + } +} + diff --git a/Softserve.iml b/Softserve.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/Softserve.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/lesson1/Test1.java b/src/lesson1/Test1.java deleted file mode 100644 index ac871de..0000000 --- a/src/lesson1/Test1.java +++ /dev/null @@ -1,7 +0,0 @@ -package lesson1; - -public class Test1 { - public static void main(String[] args) { - System.out.println("Hello, Test1"); - } -} diff --git a/src/lesson1/Test2.java b/src/lesson1/Test2.java deleted file mode 100644 index cda12dc..0000000 --- a/src/lesson1/Test2.java +++ /dev/null @@ -1,7 +0,0 @@ -package lesson1; - -public class Test2 { - public static void main(String[] args) { - System.out.println("Hello, Test2"); - } -} diff --git a/src/lesson1/Test3.java b/src/lesson1/Test3.java deleted file mode 100644 index 5404db9..0000000 --- a/src/lesson1/Test3.java +++ /dev/null @@ -1,7 +0,0 @@ -package lesson1; - -public class Test3 { - public static void main(String[] args) { - System.out.println("Hello, Test3"); - } -} diff --git a/src/lesson1/Test4.java b/src/lesson1/Test4.java deleted file mode 100644 index b5f6c2f..0000000 --- a/src/lesson1/Test4.java +++ /dev/null @@ -1,7 +0,0 @@ -package lesson1; - -public class Test4 { - public static void main(String[] args) { - System.out.println("Hello, Test4"); - } -} diff --git a/src/lesson1/TestClass.java b/src/lesson1/TestClass.java deleted file mode 100644 index 37a17f5..0000000 --- a/src/lesson1/TestClass.java +++ /dev/null @@ -1,4 +0,0 @@ -package lesson1; - -public class TestClass { -} diff --git a/src/lesson1/myclass.java b/src/lesson1/myclass.java deleted file mode 100644 index 2717bde..0000000 --- a/src/lesson1/myclass.java +++ /dev/null @@ -1,4 +0,0 @@ -package lesson1; - -public class myclass { -} diff --git a/src/lesson1/test6.java b/src/lesson1/test6.java deleted file mode 100644 index 3dbec3b..0000000 --- a/src/lesson1/test6.java +++ /dev/null @@ -1,4 +0,0 @@ -package lesson1; - -public class test6 { -} diff --git a/src/lesson1/testclass5.java b/src/lesson1/testclass5.java deleted file mode 100644 index e7af960..0000000 --- a/src/lesson1/testclass5.java +++ /dev/null @@ -1,4 +0,0 @@ -package lesson1; - -public class testclass5 { -}