-
Notifications
You must be signed in to change notification settings - Fork 2
Semen_Antufiev #23
base: master
Are you sure you want to change the base?
Semen_Antufiev #23
Changes from all commits
ed90031
ffc5bc7
5fc4b66
f48b06e
9d17d84
d8ea763
431d201
3452255
8cc135a
de07ae6
963ac04
65f4556
0ca6164
770b5c8
7a446d2
c7a5739
560a704
6cc3fbe
2c248d5
70a947f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,17 @@ | ||
| # Java Core June 2021 | ||
|
|
||
| ## *Nikolaev Artem* | ||
| ## *Antufiev Semen* | ||
|
|
||
| | Number | Solution | Short description | ||
| | --- | --- | --- | | ||
| | HW1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/master/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
|
|
||
| | HW1 | [Solution_1](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument | | ||
| | HW2_1 | [Pyramid printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/pyramid_printer) | The app that reads input arguments and prints pyramid| | ||
| | HW2_2 | [Traffic light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/traffic_light) | The app that reads input arguments and prints color of traffic light now| | ||
| | HW2_3 | [Random chars table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_2/random_chars_table) | The app that reads input arguments and prints table with random charset| | ||
| | HW3 | [Immutable_class](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_3) | Example of immutable class | ||
| | HW4_1 | [Custon_anotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/custom_annotation) | Custom annotation and handler | ||
| | HW4_2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/singleton) | Example of singleton class | ||
| | HW4_3 | [Custom_file_reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/SemenAntufiev/src/main/java/homework_4/custom_file_reader) | Work with file readers | ||
| [Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) | ||
|
|
||
| [codingbat](https://codingbat.com/done?user=alexantufiev@gmail.com&tag=8698341536) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package homework_2.pyramid_printer; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| PyramidPrinter pyramidPrinter = new PyramidPrinter(); | ||
| pyramidPrinter.run(); | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package homework_2.pyramid_printer; | ||
|
|
||
| import java.util.Scanner; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class PyramidPrinter { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| public static final String ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; | ||
|
|
||
| public void run() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| String sizeAsString = scanner.nextLine(); | ||
|
|
||
| if (isNumber(sizeAsString)) { | ||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
|
Comment on lines
+14
to
+17
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If number, then error? |
||
| int size = Integer.parseInt(sizeAsString); | ||
| for (int i = 0; i < size; i++) { | ||
| for (int j = 0; j <= i; j++) { | ||
| System.out.print("*"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we're printing x... |
||
| } | ||
| System.out.println(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isNumber(String str) { | ||
| return !Pattern.matches( "[0-9]+", str); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package homework_2.random_chars_table; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| RandomCharsTable randomCharsTable = new RandomCharsTable(); | ||
| randomCharsTable.run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package homework_2.random_chars_table; | ||
|
|
||
| import java.util.Random; | ||
| import java.util.Scanner; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class RandomCharsTable { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public static final String ERROR_MESSAGE = "Passed parameters should match the format [positive integer] [positive integer] [even|odd]"; | ||
| public void run() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| String[] params = new String[3]; | ||
| for (int i = 0; i < params.length; i++) { | ||
| params[i] = scanner.next(); | ||
| } | ||
| if (isNumber(params[0]) || isNumber(params[1])) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite strange |
||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
| int rows = Integer.parseInt(params[0]); | ||
| int columns = Integer.parseInt(params[1]); | ||
| if (rows == 0 || columns == 0) { | ||
| return; | ||
| } | ||
|
Comment on lines
+21
to
+23
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not expected behaviour, according to chat |
||
| if (rows < 0 || columns < 0) { | ||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
| String strategy = params[2]; | ||
| int strategyMod; | ||
| if (strategy.equals("even")) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to a new separate method (getResult, for example)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because your code is too complex and difficult to read |
||
| strategyMod = 0; | ||
| } else if (strategy.equals("odd")){ | ||
| strategyMod = 1; | ||
| } else { | ||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
| StringBuilder result = new StringBuilder(strategy + " letters - "); | ||
| for (int i = 0; i < rows; i++) { | ||
| System.out.print("| "); | ||
| for (int j = 0; j < columns; j++) { | ||
| char letter = (char) (65 + new Random().nextInt(26)); | ||
| System.out.print(letter + " | "); | ||
| if (letter % 2 == strategyMod) { | ||
| result.append(letter).append(", "); | ||
| } | ||
| } | ||
| System.out.println(); | ||
| } | ||
| System.out.println(result.deleteCharAt(result.length() - 2)); | ||
| } | ||
|
|
||
| private boolean isNumber(String str) { | ||
| return !Pattern.matches( "[0-9]+", str); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package homework_2.traffic_light; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| TrafficLight trafficLight = new TrafficLight(); | ||
| trafficLight.run(); | ||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package homework_2.traffic_light; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Scanner; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class TrafficLight { | ||
|
|
||
| public static final String ERROR_MESSAGE = "Only 1 non-negative integer is allowed as passed parameter"; | ||
| public static final String DAY_OVER = "The day is over"; | ||
|
|
||
| public void run() { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and in other places: i would recommend getting rid of multiple if/else statements and separate logic to other methods |
||
| Scanner scanner = new Scanner(System.in); | ||
| String secondsAsString = scanner.next(); | ||
| if (isNumber(secondsAsString)) { | ||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
| long seconds = Long.parseLong(secondsAsString); | ||
| if (seconds < 0) { | ||
| System.out.println(ERROR_MESSAGE); | ||
| return; | ||
| } | ||
| if (seconds > 86399) { | ||
| System.out.println(DAY_OVER); | ||
| return; | ||
| } | ||
| Instant instant = Instant.ofEpochSecond(seconds); | ||
| String fullTime = instant.toString(); | ||
| int size = fullTime.length(); | ||
| int onlySeconds = Integer.parseInt(fullTime.substring(size - 3, size - 1)); | ||
| if (onlySeconds >= 0 && onlySeconds < 35) { | ||
| System.out.println("Green"); | ||
| } else if (onlySeconds >= 40 && onlySeconds < 55) { | ||
| System.out.println("Red"); | ||
| } else { | ||
| System.out.println("Yellow"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isNumber(String str) { | ||
| return !Pattern.matches( "[0-9]+", str); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package homework_3; | ||
|
|
||
|
|
||
| /* | ||
| The class must be declared as final | ||
| Data members in the class must be declared as private | ||
| Data members in the class must be declared as final | ||
| A parameterized constructor should initialize all the fields performing a deep copy | ||
| Deep Copy of objects should be performed in the getter methods | ||
| No setters | ||
| */ | ||
|
|
||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public final class ImmutableClass { | ||
|
|
||
| private final int age; | ||
|
|
||
| private final int high; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add new mutable class field
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it's a requirement |
||
|
|
||
| private final int weight; | ||
|
|
||
| private final String name; | ||
|
|
||
| public ImmutableClass(int age, int high, int weight, String name) { | ||
| this.age = age; | ||
| this.high = high; | ||
| this.weight = weight; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public ImmutableClass(int weight, int high) { | ||
| this.high = high; | ||
| this.weight = weight; | ||
| this.age = 10; | ||
| this.name = "Some person"; | ||
| } | ||
|
|
||
| public int getAge() { | ||
| return age; | ||
| } | ||
|
|
||
| public int getHigh() { | ||
| return high; | ||
| } | ||
|
|
||
| public int getWeight() { | ||
| return weight; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public ImmutableClass changeWeightAndHigh(int weight, int high) { | ||
| return new ImmutableClass(weight, high); | ||
| } | ||
|
|
||
| public ImmutableClass changeName(String name) { | ||
| return new ImmutableClass(this.age, this.high, this.weight, name); | ||
| } | ||
|
|
||
| public ImmutableClass changeAge(int age) { | ||
| return new ImmutableClass(age, this.high, this.weight, this.name); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof ImmutableClass)) return false; | ||
| ImmutableClass that = (ImmutableClass) o; | ||
| return age == that.age && high == that.high && weight == that.weight && Objects.equals(name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(age, high, weight, name); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package homework_4.custom_annotation; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class DefaulValueImpl { | ||
|
|
||
| public static void insetDefaultValue(Object object) throws IllegalAccessException, InvocationTargetException { | ||
| setDefaultValueToFields(object, object.getClass()); | ||
| setDefaultValueToMethods(object, object.getClass()); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| private static void setDefaultValueToMethods(Object object, Class<?> clazz) throws InvocationTargetException, IllegalAccessException { | ||
| List<Method> collectPrivateMethods = Arrays.stream(clazz.getDeclaredMethods()) | ||
| .filter((o) -> o.isAnnotationPresent(DefaultValue.class)) | ||
| .collect(Collectors.toList()); | ||
| setDefaultValueMethods(collectPrivateMethods, clazz, object); | ||
| } | ||
|
|
||
| private static void setDefaultValueToFields(Object object, Class<?> clazz) throws IllegalAccessException { | ||
| List<Field> collectPublic = Arrays.stream(clazz.getFields()) | ||
| .filter((o) -> o.isAnnotationPresent(DefaultValue.class)) | ||
| .collect(Collectors.toList()); | ||
| getDefaultFields(collectPublic, clazz, object); | ||
|
|
||
| List<Field> collectPrivate = Arrays.stream(clazz.getDeclaredFields()) | ||
| .filter((o) -> o.isAnnotationPresent(DefaultValue.class)) | ||
| .collect(Collectors.toList()); | ||
| getDefaultFields(collectPrivate, clazz, object); | ||
|
|
||
| } | ||
|
|
||
| private static void setDefaultValueMethods(List<Method> collectMethods, Class<?> clazz, Object object) throws InvocationTargetException, IllegalAccessException { | ||
| for (Method method : collectMethods) { | ||
| DefaultValue annotation = method.getAnnotation(DefaultValue.class); | ||
| String value = annotation.value(); | ||
| Class<?> type = method.getParameterTypes()[0]; | ||
| method.setAccessible(true); | ||
| if (type == Integer.class || type == int.class) { | ||
| method.invoke(object, Integer.parseInt(value)); | ||
| } else if (type == Double.class || type == double.class) { | ||
| method.invoke(object, Double.parseDouble(value)); | ||
| } else { | ||
| method.invoke(object, value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void getDefaultFields(List<Field> collect, Class<?> clazz, Object object) throws IllegalAccessException { | ||
| for (Field field : collect) { | ||
| DefaultValue annotation = field.getAnnotation(DefaultValue.class); | ||
| String value = annotation.value(); | ||
| Class<?> type = field.getType(); | ||
| field.setAccessible(true); | ||
| if (type == Integer.class || type == int.class) { | ||
| if (field.get(object) == null || field.get(object).equals(0)) | ||
| field.set(object, Integer.parseInt(value)); | ||
| } else if (type == Double.class || type == double.class) { | ||
| if (field.get(object) == null || field.get(object).equals(0.0)) | ||
| field.set(object, Double.parseDouble(value)); | ||
| } else { | ||
| if (field.get(object) == null || field.get(object).equals("")) field.set(object, value); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package homework_4.custom_annotation; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.METHOD, ElementType.FIELD}) | ||
| public @interface DefaultValue { | ||
| String value(); | ||
|
|
||
| } |





There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.