diff --git a/src/lesson1/task1/ArrayItems.java b/src/lesson1/task1/ArrayItems.java new file mode 100644 index 0000000..b46970e --- /dev/null +++ b/src/lesson1/task1/ArrayItems.java @@ -0,0 +1,31 @@ + +package lesson1.task1; + +import java.util.Arrays; + +public class ArrayItems { + + public static void main(String[] args) { + Object [] array = new Object[5]; + array[0]="one"; + array[1]=2.0f; + array[2]=3; + array[3]=4L; + array[4]='\u01BC'; + ArrayItems test = new ArrayItems(); + + System.out.println(Arrays.toString(array)); + test.swap(0,2,array); + System.out.println(Arrays.toString(array)); + test.swap(4,2,array); + System.out.println(Arrays.toString(array)); + + } + + public void swap(int index1, int index2, Object [] array){ + + Object temp = array[index1]; + array[index1] = array[index2]; + array[index2] = temp; + } +} diff --git a/src/lesson1/task2/ArrayToArrayList.java b/src/lesson1/task2/ArrayToArrayList.java new file mode 100644 index 0000000..a37cc76 --- /dev/null +++ b/src/lesson1/task2/ArrayToArrayList.java @@ -0,0 +1,18 @@ +package lesson1.task2; + +import java.util.ArrayList; +import java.util.Arrays; + +public class ArrayToArrayList { + + public ArrayList arrayToArrayList(T...array){ + ArrayList result = new ArrayList<>(Arrays.asList(array)); + return result; + } + public static void main(String[] args) { + ArrayToArrayList str = new ArrayToArrayList(); + System.out.println(str.arrayToArrayList("1","2","3","4","5").size()); + // Методом .size я проверил, что результат метода arrayToArrayList - ArrayList + // т.к. у Array определение длины выполняет метод .length + } +} diff --git a/src/lesson1/task3/Apple.java b/src/lesson1/task3/Apple.java new file mode 100644 index 0000000..7812394 --- /dev/null +++ b/src/lesson1/task3/Apple.java @@ -0,0 +1,14 @@ +package lesson1.task3; + +public class Apple extends Fruit{ + + public Apple() { + super(1.0f); + } + + @Override + public float getWeight() { + return weight; + } + +} diff --git a/src/lesson1/task3/Fruit.java b/src/lesson1/task3/Fruit.java new file mode 100644 index 0000000..1ab363a --- /dev/null +++ b/src/lesson1/task3/Fruit.java @@ -0,0 +1,12 @@ +package lesson1.task3; + +public abstract class Fruit { + float weight; + + public abstract float getWeight(); + + public Fruit(float weight) { + this.weight = weight; + } + +} diff --git a/src/lesson1/task3/FruitBox.java b/src/lesson1/task3/FruitBox.java new file mode 100644 index 0000000..b7dce8c --- /dev/null +++ b/src/lesson1/task3/FruitBox.java @@ -0,0 +1,41 @@ +package lesson1.task3; + +import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match; + +import java.util.ArrayList; +import java.util.Arrays; + +public class FruitBox { + ArrayList fruitBox = new ArrayList<>(); + + public FruitBox(T...fruits){ + fruitBox = new ArrayList<>(Arrays.asList(fruits)); + } + + public ArrayList getFruitBox() { + return fruitBox; + } + + public void addFruit (T fruit){ + fruitBox.add(fruit); + } + + public float getBoxWeight(){ + float boxWeight = 0f; + + for (int i = 0; i< fruitBox.size(); i++) { + boxWeight += fruitBox.get(i).getWeight(); + } + return boxWeight; + } + + public boolean compare (FruitBox anotherBox){ + return Math.abs(this.getBoxWeight()-anotherBox.getBoxWeight()) < 0.0001f; + } + + public void repackFruits (FruitBox anotherBox){ + anotherBox.fruitBox.addAll(this.fruitBox); + this.fruitBox.clear(); + } + +} diff --git a/src/lesson1/task3/Main.java b/src/lesson1/task3/Main.java new file mode 100644 index 0000000..f529ea4 --- /dev/null +++ b/src/lesson1/task3/Main.java @@ -0,0 +1,26 @@ +package lesson1.task3; + +public class Main { + public static void main(String[] args) { + + FruitBox appleFruitBox = new FruitBox<>(new Apple(),new Apple(), new Apple(), new Apple(), new Apple()); + FruitBox orangeFruitBox = new FruitBox<>(new Orange(), new Orange(), new Orange(),new Orange()); + + System.out.println("The box of apples weight is " + appleFruitBox.getBoxWeight()); + System.out.println("The box of oranges weight is " +orangeFruitBox.getBoxWeight()); + System.out.println("Is the boxes weigh the same? - " + appleFruitBox.compare(orangeFruitBox)); + System.out.println("-------------------------------------"); + + FruitBox appleFruitBox2 = new FruitBox<>(); + FruitBox orangeFruitBox2 = new FruitBox<>(); + + appleFruitBox.repackFruits(appleFruitBox2); + orangeFruitBox.repackFruits(orangeFruitBox2); + System.out.println("The 1st box of apples weight is " + appleFruitBox.getBoxWeight()); + System.out.println("The 2nd box of apples weight is " + appleFruitBox2.getBoxWeight()); + System.out.println("-------------------------------------"); + System.out.println("The 1st box of oranges weight is " +orangeFruitBox.getBoxWeight()); + System.out.println("The 2nd box of oranges weight is " +orangeFruitBox2.getBoxWeight()); + + } +} diff --git a/src/lesson1/task3/Orange.java b/src/lesson1/task3/Orange.java new file mode 100644 index 0000000..7537c9d --- /dev/null +++ b/src/lesson1/task3/Orange.java @@ -0,0 +1,14 @@ +package lesson1.task3; + +public class Orange extends Fruit{ + + + public Orange() { + super(1.5f); + } + + @Override + public float getWeight() { + return weight; + } +}