Skip to content

java-basic-template is finished #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
92 changes: 81 additions & 11 deletions src/main/java/com/epam/izh/rd/online/service/SimpleMathService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.epam.izh.rd.online.service;


public class SimpleMathService implements MathService {


/**
* Метод возвращает 0, если value1 = value2.
* Метод возвращает -1, если value1 < value2.
Expand All @@ -13,16 +15,24 @@ public class SimpleMathService implements MathService {
*/
@Override
public int compare(int value1, int value2) {
return -2;
int i;
if (value1 == value2) {
i = 0;
} else if ( value1 < value2){
i = -1;
} else {
i = 1;
} return i;
}


/**
* Метод возвращает максимальное число из пары.
* Например для списка (-1, 2) метод должен вернуть 2
*/
@Override
public int maxFrom(int value1, int value2) {
return -1;
return Math.max(value1, value2);
}

/**
Expand All @@ -31,7 +41,10 @@ public int maxFrom(int value1, int value2) {
*/
@Override
public int maxFrom(int[] values) {
return -1;
int o = values[0];
for (int i = 1; i < values.length; i++){
o = Math.max(o, values[i]);
} return o;
}

/**
Expand All @@ -40,17 +53,38 @@ public int maxFrom(int[] values) {
*/
@Override
public int sum(int[] values) {
return -1;
int sum = 0;
for (int value : values) {
sum = sum + value;
}
return sum;
}


/**
* Метод фильтрует массив, оставляя только четные числа.
* Например для списка {-1, -3, 4, 8, 5, 22, 17} метод должен вернуть {4, 8, 22}
*/
@Override
public int[] getEvenDigits(int[] values) {
return new int[]{};
}
int n = 0;
for (int value : values) {
if (value % 2 == 0) {
n++; // Узнал длину массива
}
}
int[] newValue = new int[n];
int index = 0;
for (int i = 0; i < n+1; i++){
if (values.length < 1){
return values;
} else if (values[i]%2 == 0){
newValue[index] = values[i]; // Если число четное, присваиваем его значение в индекс нового массива
index++;
}
}
return newValue;
}

/**
* Метод считает факториал из заданного числа.
Expand All @@ -59,7 +93,10 @@ public int[] getEvenDigits(int[] values) {
*/
@Override
public long calcFactorial(int initialVal) {
return -1L;
int result = 1;
for (int i = 1; i <= initialVal; i ++) {
result = result * i;
} return result;
}

/**
Expand All @@ -74,7 +111,13 @@ public long calcFactorial(int initialVal) {
*/
@Override
public long calcFibonacci(int number) {
return -1L;
long[] array = new long[number + 2];
array[0] = 0;
array[1] = 1;
for (int i = 2; i <= number; i++){
array[i] = array[i - 1] + array[i -2];
}
return array[number];
}

/**
Expand All @@ -83,7 +126,21 @@ public long calcFibonacci(int number) {
*/
@Override
public int[] sort(int[] values) {
return new int[]{};
boolean isSorted = false;
int buf;
while (!isSorted) {
isSorted = true;
for (int i = 0; i < values.length - 1; i++) {
if (values[i] > values[i + 1]) {
isSorted = false;

buf = values[i];
values[i] = values[i + 1];
values[i + 1] = buf;
}
}
}
return values;
}

/**
Expand All @@ -94,7 +151,13 @@ public int[] sort(int[] values) {
*/
@Override
public boolean isPrimary(int number) {
return false;
if (number <=2){
return true;}
for (int i = 2; i < number; i++){
if (number % i == 0){
return false; }
}
return true;
}

/**
Expand All @@ -104,6 +167,13 @@ public boolean isPrimary(int number) {
*/
@Override
public int[] reverseArray(int[] values) {
return new int[]{};
int n = values.length;
int temp;
for (int i = 0; i < n/2; i++){
temp = values[n - i - 1];
values[n - i - 1] = values[i];
values[i] = temp;
}
return values;
}
}