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
67 changes: 67 additions & 0 deletions src/main/java/com/epam/jf/plokhoi/homework/Task1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.epam.jf.plokhoi.homework;

import java.util.Arrays;

public class Task1 {

/**
* Группа людей участвует в марафоне, их имена и время за которое они пробежали марафон представленны в массиве.
* Необходимо найти человека, который быстрее всех пробежал дистанцию и вывести его имя и счет.
* (Опционально) Найти человека, который прибежал вторым.
*/
public static void main(String...args) {
String[] names = { "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate" };
int[] times = { 341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265 };

usingFori(names, times);
usingSort(names, times);
}

private static void usingFori(String[] names, int[] times) {
int first = times[0] > times[1] ? 1 : 0;
int second = 1 - first;

for (int i = 2; i < times.length; ++i) {
if (times[first] > times[i]) {
second = first;
first = i;
} else if (times[second] > times[i]) {
second = i;
}
}

System.out.println("1 place: " + names[first] + " with time " + times[first]);
System.out.println("2 place: " + names[second] + " with time " + times[second]);
}

private static void usingSort(String[] names, int[] times) {
Record[] records = new Record[times.length];
for (int i = 0; i < times.length; ++i) {
records[i] = new Record(names[i], times[i]);
}
Arrays.sort(records);
System.out.println("1 place: " + records[0]);
System.out.println("2 place: " + records[1]);
}

static class Record implements Comparable<Record> {

private final int time;
private final String name;

Record(String name, int time) {
this.name = name;
this.time = time;
}

@Override
public int compareTo(Record other) {
return Integer.compare(time, other.time);
}

@Override
public String toString() {
return name + " with time " + time;
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/epam/jf/plokhoi/homework/Task2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.epam.jf.plokhoi.homework;

public class Task2 {

/**
* Необходимо найти и вывести на экран наибольшую и наименьшую по длине строки.
* Если строк с одинаковой длиной несколько - вывести последнюю из них.
* Длину строки можно получить вызвав {@link String#length()}.
* @param args Анализируемые строки.
*/
public static void main(String...args) {
String min = args[0];
String max = min;

for (int i = 1; i < args.length; ++i) {
if (max.length() <= args[i].length()) {
max = args[i];
}
if (min.length() >= args[i].length()) {
min = args[i];
}
}

System.out.println("Min string '" + min + "' with length " + min.length());
System.out.println("Max string '" + max + "' with length " + max.length());
}
}
Empty file added test.txt
Empty file.