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
31 changes: 31 additions & 0 deletions src/lesson1/task1/ArrayItems.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/lesson1/task2/ArrayToArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lesson1.task2;

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

public class ArrayToArrayList<T> {

public ArrayList<T> arrayToArrayList(T...array){
ArrayList<T> result = new ArrayList<>(Arrays.asList(array));
return result;
}
public static void main(String[] args) {
ArrayToArrayList<String> str = new ArrayToArrayList<String>();
System.out.println(str.arrayToArrayList("1","2","3","4","5").size());
// Методом .size я проверил, что результат метода arrayToArrayList - ArrayList
// т.к. у Array определение длины выполняет метод .length
}
}
14 changes: 14 additions & 0 deletions src/lesson1/task3/Apple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lesson1.task3;

public class Apple extends Fruit{

public Apple() {
super(1.0f);
}

@Override
public float getWeight() {
return weight;
}

}
12 changes: 12 additions & 0 deletions src/lesson1/task3/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lesson1.task3;

public abstract class Fruit {
float weight;

public abstract float getWeight();

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

}
41 changes: 41 additions & 0 deletions src/lesson1/task3/FruitBox.java
Original file line number Diff line number Diff line change
@@ -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<T extends Fruit> {
ArrayList<T> fruitBox = new ArrayList<>();

public FruitBox(T...fruits){
fruitBox = new ArrayList<>(Arrays.asList(fruits));
}

public ArrayList<T> 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<? extends Fruit> anotherBox){
return Math.abs(this.getBoxWeight()-anotherBox.getBoxWeight()) < 0.0001f;
}

public void repackFruits (FruitBox<T> anotherBox){
anotherBox.fruitBox.addAll(this.fruitBox);
this.fruitBox.clear();
}

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

public class Main {
public static void main(String[] args) {

FruitBox<Apple> appleFruitBox = new FruitBox<>(new Apple(),new Apple(), new Apple(), new Apple(), new Apple());
FruitBox<Orange> 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<Apple> appleFruitBox2 = new FruitBox<>();
FruitBox<Orange> 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());

}
}
14 changes: 14 additions & 0 deletions src/lesson1/task3/Orange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lesson1.task3;

public class Orange extends Fruit{


public Orange() {
super(1.5f);
}

@Override
public float getWeight() {
return weight;
}
}