Skip to content
This repository was archived by the owner on Oct 25, 2021. It is now read-only.
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
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
# Java Core June 2021

## *Nikolaev Artem*
## *Zaytsev Artem*

| 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 |
| HW_1 | [Console printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_1) | The app that reads input arguments and prints them, until "error" argument |
| HW_2.1 | [Traffic Light](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/Homework_2/traffic_light) | The app that returns the color of the traffic light depending on the number of seconds from the beginning of the day |
| HW_2.2 | [Pyramid Printer](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/Homework_2/pyramid_printer) | The app that "builds" a pyramid with the given base |
| HW_2.3 | [Random Chars Table](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/Homework_2/random_chars_table) | The app that "builds" a table of chars, end print the letters which meet a given strategy (even/odd)|
| HW_3 | [ImmutableClass](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_3) | ImmutableClass (MyImmutableElectricGuitarESP class)|
| HW_3.1 | [Traffic Light Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_2/traffic_light) | Tests for "Traffic Light" Application|
| HW_3.2 | [Pyramid Printer Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_2/pyramid_printer) | Tests for "Pyramid Printer" Application |
| HW_3.3 | [Random Chars Table Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_2/random_chars_table) | Tests for "Random Chars Table" Application|
| HW_4.1 | [Custom_File_Reader](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_4/custom_file_reader) | Custom_File_Reader task|
| HW_4.1_Tests | [Custom_File_Reader Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_4/custom_file_reader) | Tests for Custom_File_Reader|
| HW_4.2 | [Singleton](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_4/singleton) | Singleton task|
| HW_4.2_Tests | [Singleton Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_4/singleton) | Tests for Singleton|
| HW_4.3 | [Custom_Annotation](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_4/custom_annotation) | Custom_Annotation task|
| HW_4.3_Tests | [Custom_Annotation Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_4/custom_annotation) | Tests for Custom_Annotation |
| HW_5.1 | [Power Of Number](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_5/power_of_number) | Power Of Number task|
| HW_5.1_Tests | [Power Of Number Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_5/power_of_number) | Tests for Power Of Number task|
| HW_5.2 | [Custom Regex Matcher](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/main/java/homework_5/custom_regex_matcher) | Custom Regex Matcher task|
| HW_5.2_Tests | [Custom Regex Matcher Tests](https://github.com/NikolaevArtem/Java_Core_June_2021/tree/feature/ZaytsevArtem/src/test/java/homework_5/custom_regex_matcher) | Tests for Custom Regex Matcher task|

[Link to markdown giude](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

[Link to my codingbat result page](https://codingbat.com/done?user=zarp1986@gmail.com&tag=4480593357)

[Link to markdown guide](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ repositories {
}

dependencies {
implementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'
Comment on lines +13 to +14
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these are needed. You allowed junit in source code

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.7.0'
}

test {
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/homework_1/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
package homework_1;

public class Main {
protected static final String RED_COLOR = "\033[31m";
protected static final String GREEN_COLOR = "\033[32m";
protected static final String RESET_COLOR = "\033[0m";

public static void main(String[] args) {
System.out.println("Hello homework!");
}

}
int k = args.length;
if (k == 0) {
System.out.println(GREEN_COLOR + "Application has been called " + RED_COLOR + "without " + GREEN_COLOR + "the arguments!" + RESET_COLOR);
} else {
for (int i = 0; i < k; i++) {
if (args[i].equals("error")) {
//https://stackoverflow.com/questions/565252/how-to-set-a-strings-color
System.out.println(RED_COLOR + "Alarm!" + RESET_COLOR);
//exit from the loop
break;
}
else {
String s = "letter";
if (args[i].length() > 1) {s = s + "s";}
System.out.println(args[i] + ": " + args[i].length() + " " + s);
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/homework_2/pyramid_printer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package homework_2.pyramid_printer;

public class Main {
public static void main(String[] args) {
new PyramidPrinter().run();

}

}
41 changes: 41 additions & 0 deletions src/main/java/homework_2/pyramid_printer/PyramidPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package homework_2.pyramid_printer;

import java.util.Scanner;

public class PyramidPrinter {
protected final String ERROR_MSG = "Only 1 non-negative integer is allowed as passed parameter! Please, try again";
int base;

public void run() {
System.out.println("Enter the base of the pyramid, valid value is integer:");
readAndValidateInput();
drawPyramid(base);

}

protected void readAndValidateInput() {
try (Scanner scanner = new Scanner(System.in)){
String s = scanner.nextLine();
if (s.matches("\\d+")) {
base = Integer.parseInt(s);
} else {
throw new IllegalArgumentException();
}
}
catch (IllegalArgumentException e){
System.out.println(ERROR_MSG);
}

}

protected void drawPyramid(int base){
for (int i = 0; i < base; i++){
for (int j = 0; j <= i; j++){
System.out.print("x");
}
System.out.println();
}

}

}
9 changes: 9 additions & 0 deletions src/main/java/homework_2/random_chars_table/Main.java
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) {
new RandomCharsTable().run();

}

}
105 changes: 105 additions & 0 deletions src/main/java/homework_2/random_chars_table/RandomCharsTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package homework_2.random_chars_table;


import java.util.Scanner;

public class RandomCharsTable {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix namings, declarations, whitespaces, cleanup and simplify your code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope, that i've done it.

protected static final int MIN_ASCII_CODE = 65;
protected static final int MAX_ASCII_CODE = 90;
protected static final String ERR_MSG = "Passed parameters should match the format [positive integer] [positive integer] [even|odd]";
int cols = 0; int rows = 0;
String strategy = "";
boolean exceptionWasThrown;

public void run(){
System.out.println("Enter the dimensions of matrix and required strategy : two integers and required strategy - even|odd, delimiter - space character))");
readAndValidateInput();
if (exceptionWasThrown) {
System.out.println(ERR_MSG);
} else {
int [][] matrix = createCharsTable(cols, rows);
printCharsTable(matrix, strategy);
}

}

protected void readAndValidateInput (){
try (Scanner scanner = new Scanner(System.in)) {
int paramCount = 0;
String tempStr = "";
String inString = scanner.nextLine();
if (inString.matches("\\d+\\s\\d+\\s[odevn]{3,4}")){
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
if (c != ' ') {
tempStr = tempStr + c;
}
else {
if (paramCount == 0) {
cols = Integer.parseInt(tempStr);
paramCount++;
tempStr = "";
continue;
}
if (paramCount == 1) {
rows = Integer.parseInt(tempStr);
paramCount++;
tempStr = "";
}
}
}
}
else {
throw new IllegalArgumentException();
}
strategy = tempStr;
if ((!strategy.equals("even") && !strategy.equals("odd")) || cols <= 0 || rows <= 0) {
throw new IllegalArgumentException();
}
} catch (RuntimeException e) {
exceptionWasThrown = true;
}

}

public int[][] createCharsTable(int col, int row){
int [][] matrix = new int[col][row];
for (int i = 0; i < col; i++){
for (int j = 0; j < row; j++){
matrix [i][j] = rnd(MIN_ASCII_CODE, MAX_ASCII_CODE);
}
}
return matrix;

}

public void printCharsTable (int[][] matrix, String strategy){
String tempStr;
boolean evStrat = strategy.equals("even");
String stratHeader = (evStrat) ? "Even letters - " : "Odd letters - ";
String stratRes = "";
for (int i = 0; i < matrix.length; i++ ) {
tempStr = "";
for (int j = 0; j < matrix[0].length; j++){
tempStr = tempStr + (char) matrix[i][j] + "|";
if (evStrat && matrix[i][j] % 2 == 0) {
stratRes = (stratRes.equals("")) ? (char) matrix[i][j] + "," : stratRes + " " + (char) matrix[i][j] + ",";
}
if (!evStrat && matrix[i][j] % 2 == 1) {
stratRes = (stratRes.equals("")) ? (char) matrix[i][j] + "," : stratRes + " " + (char) matrix[i][j] + ",";
}
}
System.out.println("|" + tempStr);
}
stratRes = (stratRes.length() > 1) ? stratRes.substring(0, stratRes.length() - 1) : stratRes;
System.out.println(stratHeader + stratRes);

}

public static int rnd(int min, int max) {
max -= min;
return (int) (Math.random() * ++max) + min;

}

}
9 changes: 9 additions & 0 deletions src/main/java/homework_2/traffic_light/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package homework_2.traffic_light;

public class Main {
public static void main(String[] args) {
new TrafficLight().run();

}

}
63 changes: 63 additions & 0 deletions src/main/java/homework_2/traffic_light/TrafficLight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package homework_2.traffic_light;

import java.util.Scanner;


public class TrafficLight {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope, that i fixed it.

protected static final String RED_COLOUR = "\u001B[31m";
protected static final String GREEN_COLOUR = "\u001B[32m";
protected static final String YELLOW_COLOUR = "\u001B[33m";
protected static final String RESET_COLOUR = "\u001B[0m";
protected static final String ERR_MSG = "Only 1 non-negative integer is allowed as passed parameter! Please, try again";
int seconds;
boolean exceptionWasThrown;

public void run(){
System.out.println("Enter the time (valid range is from 0 to 86399 ):");
readAndValidateInput();
if (exceptionWasThrown) {
System.out.println(ERR_MSG);
} else {
showingTrafficLightColor(seconds);
}

}

protected void readAndValidateInput () throws RuntimeException {
try (Scanner scanner = new Scanner(System.in)) {
String s = scanner.nextLine();
if (!s.matches("\\d+")) {
throw new RuntimeException();
}
else seconds = Integer.parseInt(s);
}
catch (RuntimeException e){
exceptionWasThrown = true;
}

}

protected void showingTrafficLightColor(int sec){
if (sec > 86399){
System.out.println("The day is over");
return;
}
sec = sec % 60;
String res = "unknown error(";
//YELLOW color
if ((sec >= 35 && sec < 40) || (sec >= 55 && sec < 60)) {
res = YELLOW_COLOUR + "YELLOW" + RESET_COLOUR;
}
//GREEN color
if (sec >= 0 && sec < 35) {
res = GREEN_COLOUR + "GREEN" + RESET_COLOUR;
}
//RED color
if (sec >= 40 && sec < 55) {
res = RED_COLOUR + "RED" + RESET_COLOUR;
}
System.out.println(res);

}

}
Loading