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
12 changes: 12 additions & 0 deletions level3.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Во время разработки возникает много разных файлов и папок, которые не относятся к коду и могут засорять репозиторий. Для того, чтобы не убирать их вручную каждый раз существует gitignore - это файл .gitignore, который кладется в папку с проектом и в нем описываются файлы, которые GIT должен игнорировать. Советую настроить его, чтобы такие технические файлы не попадали в GIT. Пример можно посмотреть здесь - https://github.com/Naikonst/Geekbrains-Java3

79 changes: 79 additions & 0 deletions src/Geekbrains/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package Geekbrains;

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
}
}
class ArrayTask {
//1
public static void arrToSwap(Object[] array, int a, int b) {
Object tmp = array[a];
array[a] = array[b];
array[a] = tmp;
}
//2
public <T> ArrayList convert(T[] array) {
return new ArrayList<>(Arrays.asList(array));
}
}
// 3
class Fruit {
private float weight;

public Fruit(float weight) {
this.weight = weight;
}

float getWeight() {
return weight;
}
}
class Apple <T> extends Fruit {
public Apple(float weight) {
super(weight);
}

}
class Orange <T> extends Fruit {
public Orange(float weight) {
super(weight);
}
}

class Box <T extends Fruit> {
// Складываем фрукты одного типа
private ArrayList <T> arrayList;
// конструктор на один или несколько фрутов
public Box() {
this.arrayList = new ArrayList<>();
}
public Box(T... fruits) {
this.arrayList = new ArrayList<>(Arrays.asList(fruits));
}
// вес
public float getWeight() {
float weight = 0.0f;
for (T o : arrayList) {
weight += o.getWeight();
}
return weight;
}
// сравнить
public boolean compare(Box <?> box){
return (this.getWeight()-box.getWeight() < 0.0001);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь надо использовать модуль - Math.abs, иначе 2 коробка будет больше, то разница будет отрицательной и вернется true.

}
// добавить
public void add(T fruit) {
arrayList.add(fruit);
}
// перемещение
public void sprinkle(Box<T> another) {
another.arrayList.addAll(arrayList);
arrayList.clear();
}
}