diff --git a/Java/Java.iml b/Java/Java.iml new file mode 100644 index 0000000..bcd2974 --- /dev/null +++ b/Java/Java.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/Java/src/com/company/Array.java b/Java/src/com/company/Array.java new file mode 100644 index 0000000..b13267f --- /dev/null +++ b/Java/src/com/company/Array.java @@ -0,0 +1,27 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Created by Vova Moskalenko on 04.06.2017. + */ +public class Array { + + public static void main(String[] args) { + + ArrayList letters = new ArrayList(); + letters.add('B'); + System.out.println(letters); + letters.add(0, 'A'); + System.out.println(letters); + letters.add('C'); + System.out.println(letters); + + String[] arr = {"C++", "JavaScript", "Python", "Haskell", "Swift"}; + List languages = Arrays.asList(arr); + System.out.print("length: " + languages.size() + " " + "languages[0]:" + " " + languages.get(0) + " " + + "languages[languages.length-1]:" + " " + languages.get(languages.size() - 1) ); + } +} diff --git a/Java/src/com/company/ObjectAccess.java b/Java/src/com/company/ObjectAccess.java new file mode 100644 index 0000000..e186692 --- /dev/null +++ b/Java/src/com/company/ObjectAccess.java @@ -0,0 +1,40 @@ +package com.company; + +import java.lang.reflect.Field; + +/** + * Created by Vova Moskalenko on 03.06.2017. + */ +public class ObjectAccess { + + private String name = "Marcus"; + private String city = "Roma"; + private int born = 121; + + public ObjectAccess() {} + + public String getCity() {return city;} + + public void setCity(String city) {this.city = city;} + + public static void main(String[] args) throws Exception { + + ObjectAccess person = new ObjectAccess(); + System.out.println("Person name is: " + person.name); + System.out.println("Person name is: " + person.getClass().getDeclaredField("name").get(person)); + + person.name = null; + Field arr[] = person.getClass().getDeclaredFields(); + for (int i = 0; i < arr.length; i++) { + System.out.println(arr[i].get(person)); + } + + person.setCity(null); + for (int i = 0; i < arr.length; i++) { + System.out.println(arr[i].get(person)); + } + + //with getter + System.out.println(person.getCity()); + } +} diff --git a/Java/src/com/company/Objects.java b/Java/src/com/company/Objects.java new file mode 100644 index 0000000..42bc4a0 --- /dev/null +++ b/Java/src/com/company/Objects.java @@ -0,0 +1,45 @@ +package com.company; + +public class Objects { + //our prewritten fields + private String name; + private String city; + private int born; + + public Objects() {} + + public Objects(String name, String city, int born) { + this.name = name; + this.city = city; + this.born = born; + } + + public void setName(String name) { + this.name = name; + } + + public void setCity(String city) { + this.city = city; + } + + public void setBorn(int born) { + this.born = born; + } + + public static void main(String[] args) { + + Objects obj1 = new Objects(); + obj1.name = "Marcus"; + obj1.city = "Roma"; + obj1.born = 121; + + //creating the object using constructor + Objects obj2 = new Objects("Marcus", "Roma", 121); + + //creating the objects using inner methods + Objects obj3 = new Objects(); + obj3.setName("Marcus"); + obj3.setCity("Roma"); + obj3.setBorn(121); + } +} diff --git a/Java/src/com/company/Serialization.java b/Java/src/com/company/Serialization.java new file mode 100644 index 0000000..a9c2eca --- /dev/null +++ b/Java/src/com/company/Serialization.java @@ -0,0 +1,53 @@ +package com.company; + +import java.io.*; + +/** + * Created by Vova Moskalenko on 04.06.2017. + */ +public class Serialization implements java.io.Serializable { + + String name = "Marcus"; + String city = "Roma"; + int born = 121; + + public static void main(String[] args) { + + Serialization person = new Serialization(); + //serialization + try { + FileOutputStream fileOut = + new FileOutputStream("person.txt"); + ObjectOutputStream out = new ObjectOutputStream(fileOut); + out.writeObject(person); + out.close(); + fileOut.close(); + System.out.printf("Serialized data is saved in person.txt"); + }catch(IOException i) { + i.printStackTrace(); + } + + Serialization person2 = null; + //deserialization + try { + FileInputStream fileIn = new FileInputStream("person.txt"); + ObjectInputStream in = new ObjectInputStream(fileIn); + person2 = (Serialization) in.readObject(); + in.close(); + fileIn.close(); + }catch(IOException i) { + i.printStackTrace(); + return; + }catch(ClassNotFoundException c) { + System.out.println("Serialization class not found"); + c.printStackTrace(); + return; + } + + System.out.println("Deserialized person..."); + System.out.println("Name: " + person.name); + System.out.println("City: " + person.city); + System.out.println("Born: " + person.born); + + } +}