Skip to content
Open
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
11 changes: 11 additions & 0 deletions AreaOfATriangle/AreaOfATriangle.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
93 changes: 93 additions & 0 deletions AreaOfATriangle/src/AreaOfATriangle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions AreaOfATriangle/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Main {
public static void main(String[] args) {
AreaOfATriangle areaOfATriangle = new AreaOfATriangle();
areaOfATriangle.inputSide();
areaOfATriangle.printResult();
}
}
11 changes: 11 additions & 0 deletions Person/Person.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
9 changes: 9 additions & 0 deletions Person/src/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
93 changes: 93 additions & 0 deletions Person/src/Person.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
11 changes: 11 additions & 0 deletions SmallestNumber/SmallestNumber.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
2 changes: 2 additions & 0 deletions SmallestNumber/SmallestNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class SmallestNumber {
}
7 changes: 7 additions & 0 deletions SmallestNumber/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Main {
public static void main(String[] args) {
SmallestNumber smallestNumber = new SmallestNumber();
smallestNumber.inputNumber();
smallestNumber.printResult();
}
}
41 changes: 41 additions & 0 deletions SmallestNumber/src/SmallestNumber.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

11 changes: 11 additions & 0 deletions Softserve.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
7 changes: 0 additions & 7 deletions src/lesson1/Test1.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/lesson1/Test2.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/lesson1/Test3.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/lesson1/Test4.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/lesson1/TestClass.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/lesson1/myclass.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/lesson1/test6.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/lesson1/testclass5.java

This file was deleted.