Skip to content

Commit

Permalink
java3 lesson1 v1.0 2020-01-30 22-04
Browse files Browse the repository at this point in the history
  • Loading branch information
popovichia committed Jan 30, 2020
1 parent cb175a6 commit 28f9b0b
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java3/lesson1/nbactions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath javacore.lesson1.Tests</exec.args>
<exec.executable>java</exec.executable>
</properties>
</action>
</actions>
13 changes: 13 additions & 0 deletions java3/lesson1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>javacore</groupId>
<artifactId>lesson1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
62 changes: 62 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/Task1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1;

import java.util.ArrayList;

/**
*
* @author igor
*/
public class Task1 <T extends Object> {
public T[] changeAB(T[] array) {
if(array == null || array.length != 2) {
return null;
}
T[] arrayReverse = array.clone();
arrayReverse[0] = array[1];
arrayReverse[1] = array[0];
return arrayReverse;
}
public String print(T[] array) {
String resultString = "";
for(T t : array) {
resultString += t + ", ";
}
return resultString;
}
public void test() {
System.out.println("Test started! Task1.\n");

Task1 mainObject = new Task1();

String[] arrayStrings = new String[2];
arrayStrings[0] = "Первая строка";
arrayStrings[1] = "Вторая строка";

Integer[] arrayIntegers = new Integer[2];
arrayIntegers[0] = 1;
arrayIntegers[1] = 2;

Double[] arrayDoubles = new Double[2];
arrayDoubles[0] = 0.0;
arrayDoubles[1] = 1.1;

System.out.println("arrayStrings - " + mainObject.print(arrayStrings));
arrayStrings = (String[]) mainObject.changeAB(arrayStrings);
System.out.println("Reverse arrayStrings - " + mainObject.print(arrayStrings));

System.out.println("arrayIntegers - " + mainObject.print(arrayIntegers));
arrayIntegers = (Integer[]) mainObject.changeAB(arrayIntegers);
System.out.println("Reverse arrayIntegers - " + mainObject.print(arrayIntegers));

System.out.println("arrayDoubles - " + mainObject.print(arrayDoubles));
arrayDoubles = (Double[]) mainObject.changeAB(arrayDoubles);
System.out.println("Reverse arrayDoubles - " + mainObject.print(arrayDoubles));

System.out.println("--------------------------\n");
}
}
37 changes: 37 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/Task2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1;

import java.util.ArrayList;

/**
*
* @author igor
*/
public class Task2 <T extends Object> {
public ArrayList<T> convertToArrayList(T[] array) {
ArrayList<T> arrayList = new ArrayList<>();
for(int i = 0; i < array.length; i++) {
arrayList.add(array[i]);
}
return arrayList;
}
public void test() {
System.out.println("Test started! Task2.\n");

Task2 mainObject = new Task2();

Integer[] arrayInteger = {1,2,3,4,5,6,7,8,9,0};
System.out.println(arrayInteger.getClass().getCanonicalName());
System.out.println(mainObject.convertToArrayList(arrayInteger));

String[] arrayString = {"a1","a2","a3","a4","a5","a6","a7","a8","a9","a0"};
System.out.println(arrayString.getClass().getCanonicalName());
System.out.println(mainObject.convertToArrayList(arrayString));

System.out.println("--------------------------\n");
}
}
14 changes: 14 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/Task4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1;

/**
*
* @author igor
*/
public class Task4 {

}
23 changes: 23 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/Tests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1;

import javacore.lesson1.task3.Task3;

/**
*
* @author igor
*/
public class Tests {
public static void main(String[] args) {
Task1 task1 = new Task1();
task1.test();
Task2 task2 = new Task2();
task2.test();
Task3 task3 = new Task3();
task3.test();
}
}
16 changes: 16 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/task3/Apple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1.task3;

/**
*
* @author igor
*/
public class Apple extends Fruit {
public Apple(float weight) {
super(weight);
}
}
39 changes: 39 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/task3/Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1.task3;

import java.util.ArrayList;

/**
*
* @author igor
*/
public class Box<T extends Fruit> {
private ArrayList<T> fruits;
public Box() {
fruits = new ArrayList<>();
}
public void addFruit(T fruit) {
this.fruits.add(fruit);
}
public float getWeight() {
float resultWeight = 0f;
if(fruits.size() == 0) {
return resultWeight;
}
for(T fruit: fruits) {
resultWeight += fruit.getWeight();
}
return resultWeight;
}
public boolean compare(Box<T> anotherBox) {
return this.getWeight() == anotherBox.getWeight() ? true : false;
}
@Override
public String toString() {
return this.fruits.toString();
}
}
24 changes: 24 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/task3/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1.task3;

/**
*
* @author igor
*/
public class Fruit {
protected float weight;
public Fruit(float weight) {
this.weight = weight;
}
public float getWeight() {
return this.weight;
}
@Override
public String toString() {
return this.getClass().getCanonicalName();
}
}
16 changes: 16 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/task3/Orange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1.task3;

/**
*
* @author igor
*/
public class Orange extends Fruit {
public Orange(float weight) {
super(weight);
}
}
42 changes: 42 additions & 0 deletions java3/lesson1/src/main/java/javacore/lesson1/task3/Task3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javacore.lesson1.task3;

/**
*
* @author igor
*/
public class Task3 {
public void test() {
System.out.println("Test started! Task3.\n");

Apple apple1 = new Apple(2.0f);
Apple apple2 = new Apple(2.0f);
Apple apple3 = new Apple(2.0f);

Orange orange1 = new Orange(1.0f);
Orange orange2 = new Orange(1.1f);
Orange orange3 = new Orange(1.5f);

Box boxApples = new Box();
boxApples.addFruit(apple1);
boxApples.addFruit(apple2);
boxApples.addFruit(apple3);
System.out.println("boxApples - " + boxApples);
System.out.println("boxApples weight - " + boxApples.getWeight());

Box boxOranges = new Box();
boxOranges.addFruit(orange1);
boxOranges.addFruit(orange2);
boxOranges.addFruit(orange3);
System.out.println("boxOranges - " + boxOranges);
System.out.println("boxOranges weight - " + boxOranges.getWeight());

System.out.println("Compare boxOranges and boxApples - " + boxOranges.compare(boxApples));

System.out.println("--------------------------\n");
}
}

0 comments on commit 28f9b0b

Please sign in to comment.