diff --git a/lesson1.maraphon/Animal.java b/lesson1.maraphon/Animal.java index ace66bd..686aec1 100644 --- a/lesson1.maraphon/Animal.java +++ b/lesson1.maraphon/Animal.java @@ -58,4 +58,5 @@ public boolean isDistance() { public void info() { System.out.println(type + " " +name+ " "+ onDistance); } + } diff --git a/lesson1.maraphon/Cource.java b/lesson1.maraphon/Cource.java index b30ff47..46db634 100644 --- a/lesson1.maraphon/Cource.java +++ b/lesson1.maraphon/Cource.java @@ -2,11 +2,23 @@ public class Cource { - Obstracle[] c = {new Cross(80),new Wall(5),new Water(3)}; + private Obstracle[] c = {new Cross(80),new Wall(5),new Water(3)}; + + public void doIt(Team team){ + System.out.println("Команда: " + team); + for (Competitor competitor : team.getCompetitors()) { + for (Obstracle obstracle : c) { + if (competitor.isDistance() == true) { + System.out.println(team.getCompetitors() + " успешно перепрыгнул"); + obstracle.doIt(competitor); + } else { + System.out.println(team.getCompetitors() + " не получилось"); + } + } + } - public void doIt(){ - Cross(80); - Wall(5); - Water(3); } + + } + diff --git a/lesson1.maraphon/Human.java b/lesson1.maraphon/Human.java index cd1f10b..a2f7e98 100644 --- a/lesson1.maraphon/Human.java +++ b/lesson1.maraphon/Human.java @@ -56,4 +56,8 @@ public boolean isDistance() { public void info() { System.out.println(name + " " + active); } + + public String getName() { + return name; + } } diff --git a/lesson1.maraphon/MainCross.java b/lesson1.maraphon/MainCross.java index 6275898..1411d87 100644 --- a/lesson1.maraphon/MainCross.java +++ b/lesson1.maraphon/MainCross.java @@ -5,10 +5,12 @@ public class MainCross { public static void main(String[] args) { Cource c = new Cource (); - Team comp = new Team (); + Team comp = new Team ("1" , new Human("Bob"), new Cat("Vaska"), new Dog("Tuzik") ); c.doIt(comp); + + /* Competitor[] competitors = {new Human("Bob"), new Cat("Vaska"),new Dog("Tuzik")}; Obstracle[] obstracles = {new Cross(80),new Wall(5),new Water(3)}; for(Competitor c: competitors){ diff --git a/lesson1.maraphon/Team.java b/lesson1.maraphon/Team.java index 2dbf64b..67d5d65 100644 --- a/lesson1.maraphon/Team.java +++ b/lesson1.maraphon/Team.java @@ -1,7 +1,19 @@ package lesson1.maraphon; public class Team { - Competitor[] comp = {new Human("Bob"), new Cat("Vaska"),new Dog("Tuzik")}; + + private String name; + private Competitor[] competitors; + + public Team (String name, Competitor... competitors){ + this.name = name; + this.competitors = competitors; + } + + + public Competitor[] getCompetitors() { + return competitors; + } } diff --git a/lesson2/arrayExceprion/main.java b/lesson2/arrayExceprion/main.java new file mode 100644 index 0000000..3beca26 --- /dev/null +++ b/lesson2/arrayExceprion/main.java @@ -0,0 +1,27 @@ +package lesson2.arrayExceprion; + +import java.lang.reflect.Array; +import java.util.Arrays; + +public class main { + public static void main(String[] args) { + + String[] weekDays = {"1", "2", "3" , "4"}; + Arr(weekDays); + } + //int i=Integer.parseInt(s); + public static void Arr(String[] a){ + int[] pars = new int[a.length]; + int sum; + for (int i = 0; i < a.length; i++) { + pars[i] = Integer.parseInt(a[i]); + System.out.println(pars[i]); + } + sum = pars[0]+pars[1]+pars[2]+pars[3]; + System.out.println(sum); + } + + +} + + diff --git a/lesson3/maps/PhoneBook.java b/lesson3/maps/PhoneBook.java new file mode 100644 index 0000000..330bce9 --- /dev/null +++ b/lesson3/maps/PhoneBook.java @@ -0,0 +1,31 @@ +package lesson3.maps; + +import java.util.*; + +public class PhoneBook { + private Map pb; + + public PhoneBook() { + pb = new TreeMap<>(); + } + + public void add(String name, String phone) { + pb.put(name, phone); + } + + List get(String name) { + List list = new ArrayList<>(); + for (Map.Entry entry : pb.entrySet()) + if (name.equals(entry.getKey())) + list.add(entry.getKey()+ " " + entry.getValue()); + + return list; + } + + @Override + public String toString() { + return pb.toString(); + } + + +} \ No newline at end of file diff --git a/lesson3/maps/main.java b/lesson3/maps/main.java new file mode 100644 index 0000000..373bed4 --- /dev/null +++ b/lesson3/maps/main.java @@ -0,0 +1,54 @@ +/** + * Java. level 2. Lesson 3. Example of homework + * autor Rudenko Alexander + * version date 07/01/2019 + * + */ +package lesson3.maps; + +import java.util.*; + +public class main { + public static void main(String[] args) { +// 1 Часть задания ---------------------------------- + ArrayList al = new ArrayList<>(); + al.add("Яблоко"); al.add("Банан"); al.add("Апельсин"); al.add("Мандарин"); al.add("Яблоко"); al.add("Груша"); al.add("Ананас"); al.add("Банан"); al.add("Киви"); al.add("Абрикос"); + System.out.println(al); + System.out.println("Всего элементов = " + al.size()); + + Set unic = new HashSet(al); + System.out.println(unic); + System.out.println("Всего уникальных элементов = "+ unic.size()); + + System.out.println("Подсчет уникальных элементов:"); + HashMap wordToCount = new HashMap<>(); + for (String word : al) + { + if (!wordToCount.containsKey(word)) + { + wordToCount.put(word, 0); + } + wordToCount.put(word, wordToCount.get(word) + 1); + } + for (String word : wordToCount.keySet()) + { + System.out.println(word + " " + wordToCount.get(word)); + } +//------------------------------------------------------ + + // 2 Часть задания ---------- + System.out.println("\n" + "Записная книга:"); + + PhoneBook phoneBook = new PhoneBook(); + phoneBook.add("Саня", "863 233 3301"); + phoneBook.add("Вова", "863 212 1102"); + phoneBook.add("Леха", "863 243 1111"); + phoneBook.add("Жека", "863 240 2704"); + phoneBook.add("Витя", "863 248 2222"); + + System.out.println(phoneBook); + System.out.println(phoneBook.get("Вова")); + + // -------------------------- + } +} diff --git a/lesson_4/swing/MyWindow.java b/lesson_4/swing/MyWindow.java new file mode 100644 index 0000000..9522462 --- /dev/null +++ b/lesson_4/swing/MyWindow.java @@ -0,0 +1,65 @@ +package lesson_4.swing; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public class MyWindow extends JFrame { + + public MyWindow() { + setTitle("MyWindows"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setBounds(500, 300, 500, 400); + setLayout(new GridLayout(2, 1)); + +// textarea ------------------------------------------------ + JTextArea textArea = new JTextArea(); + JScrollPane scrollPane = new JScrollPane(textArea); + textArea.append("Здесь будет отображаться вводимый текст \n"); + add(scrollPane); + textArea.setEditable(false); +//----------------------------------------------------------- + +// textfield------------------------------------------------- + JPanel text = new JPanel(); + + JTextField textField = new JTextField("введите текст"); + textField.setPreferredSize(new Dimension(200, 30)); + text.add(textField); + + textField.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (textField.getText().equals("")) { + textArea.append("Вы ввели пустой текст" + "\n"); + } else { + textArea.append(textField.getText() + "\n"); + textField.setText(""); + textField.grabFocus(); + } + } + }); + +// button ---------------------------------------------------- + + JButton button = new JButton("Отправить"); + button.setPreferredSize(new Dimension(100, 30)); + text.add(button); + + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (textField.getText().equals("")) { + textArea.append("Вы ввели пустой текст" + "\n"); + } else { + textArea.append(textField.getText() + "\n"); + textField.setText(""); + } + } + }); +//----------------------------------------------------------- + add(text); + setVisible(true); + + } +} \ No newline at end of file diff --git a/lesson_4/swing/main.java b/lesson_4/swing/main.java new file mode 100644 index 0000000..966db01 --- /dev/null +++ b/lesson_4/swing/main.java @@ -0,0 +1,15 @@ +package lesson_4.swing; + + + +public class main { + public static void main(String[] args) { + + new MyWindow(); + + } + + +} + + diff --git a/lesson_5/threads/main.java b/lesson_5/threads/main.java new file mode 100644 index 0000000..46ab7dc --- /dev/null +++ b/lesson_5/threads/main.java @@ -0,0 +1,79 @@ +package lesson_5.threads; + +public class main { + static final int size = 10000000; + static final int h = size / 2; + + public static void main(String[] args) { +// 1 массив ----------------------------------- + float[] arr1 = new float[size]; + + long a1 = System.currentTimeMillis(); + for (int i = 0; i < arr1.length; i++) { + arr1[i] = 1; + } + System.out.print("Время затраченное на заполнение массива 1: "); + System.out.println(System.currentTimeMillis() - a1 + "msec"); + long b1 = System.currentTimeMillis(); + for (int i = 0; i < arr1.length; i++) { + arr1[i] = (float)(arr1[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2)); + } + System.out.print("Время затраченное на вычисление массива 1: "); + System.out.println(System.currentTimeMillis() - b1 + "msec"); + +//--------------------------------------------------- + System.out.println("------------------"); +// 2 массив ------------------------------------------ + + float[] arr2 = new float[size]; + float[] arr21 = new float[h]; + float[] arr22 = new float[h]; + + long a = System.currentTimeMillis(); + for (int i = 0; i < arr2.length; i++) { + arr2[i] = 1; + } + System.out.print("Время затраченное на заполнение массива 2: "); + System.out.println(System.currentTimeMillis() - a + "msec"); + + long b = System.currentTimeMillis(); + System.arraycopy(arr2, 0, arr21, 0, h); //(источник, откуда начинаем брать, назначение, куда начинаем записывать, сколько) + System.arraycopy(arr2, h, arr22, 0, h); + System.out.print("Время затраченное на разбивку: "); + System.out.println(System.currentTimeMillis() - b + "msec"); + + Thread potok1 = new Thread(new Runnable() { + @Override + public void run() { + long c = System.currentTimeMillis(); + for (int i = 0; i < arr21.length; i++) { + arr21[i] = (float)(arr21[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2)); + } + System.out.print("Время затраченное на вычисление 1 части: "); + System.out.println(System.currentTimeMillis() - c + "msec"); + } + }); + potok1.start(); + + Thread potok2 = new Thread(new Runnable() { + @Override + public void run() { + long d = System.currentTimeMillis(); + for (int i = 0; i < arr22.length; i++) { + arr22[i] = (float)(arr22[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2)); + } + System.out.print("Время затраченное на вычисление 2 части: "); + System.out.println(System.currentTimeMillis() - d + "msec"); + } + }); + potok2.start(); + + long e = System.currentTimeMillis(); + System.arraycopy(arr21, 0, arr2, 0, h); + System.arraycopy(arr22, 0, arr2, h, h); + System.out.print("Время затраченное на склейку: "); + System.out.println(System.currentTimeMillis() - e + "msec"); + + } +} +