Skip to content
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
11 changes: 11 additions & 0 deletions src/main/java/ru/job4j/io/Task14.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.job4j.io;

public class Task14 {
public static void main(String[] args) {
// YOUR CODE
String greeting = "Hello, friend!";
String[] split = greeting.replaceAll("\\s+", "").split("");
String result = String.join("-", split).replace("-!", "!");
System.out.print(result);
}
}
13 changes: 13 additions & 0 deletions src/main/java/ru/job4j/io/Task15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.job4j.io;

public class Task15 {
public static void main(String[] args) {
String source = "+!?";
// YOUR CODE
String[] symbols = source.split("");
String string = symbols[0].repeat(3) + System.lineSeparator() +
symbols[1].repeat(4) + System.lineSeparator() +
symbols[2].repeat(2);
System.out.print(string);
}
}
12 changes: 12 additions & 0 deletions src/main/java/ru/job4j/io/Task16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.job4j.io;

public class Task16 {
public static void main(String[] args) {
String source = "+!?";
// YOUR CODE
String[] symbols = source.split("");
String string = symbols[0].repeat(3)
+ symbols[1].repeat(4) + symbols[2].repeat(2);
System.out.print(string);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/job4j/io/Task17.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.job4j.io;

public class Task17 {
public static void main(String[] args) {
// YOUR CODE
int x = 6;
System.out.println(x);
System.out.println(x * x);
System.out.print(x * x * x);
}
}
10 changes: 10 additions & 0 deletions src/main/java/ru/job4j/io/Task18.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.job4j.io;

public class Task18 {
public static void main(String[] args) {
// YOUR CODE
int x = 9;
System.out.println("Значение x равно " + x);
System.out.print("Значение x^2 равно " + (x * x));
}
}
12 changes: 12 additions & 0 deletions src/main/java/ru/job4j/io/Task19.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.job4j.io;

public class Task19 {
public static void main(String[] args) {
// YOUR CODE
int x = 4;
int y = 11;
System.out.println("Значение x равно " + x + "; значение y равно " + y);
System.out.println("Произведение x и y равно " + (x * y));
System.out.print("Сумма x и y равно " + (x + y));
}
}
19 changes: 19 additions & 0 deletions src/main/java/ru/job4j/io/Task20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.job4j.io;

public class Task20 {
public static void main(String[] args) {
String fullName = "Фамилия Имя Отчество";
String phoneNumber = "+7(938) 123-45-67";
// YOUR CODE
int maxLenght = Math.max(fullName.length(), phoneNumber.length());
String topBorders = "*".repeat(maxLenght + 4);
String bottomBorders = topBorders;
String result = topBorders + System.lineSeparator();
result += "* " + fullName + " ".repeat(maxLenght - fullName.length() + 1)
+ "*" + System.lineSeparator();
result += "* " + phoneNumber + " ".repeat(maxLenght - phoneNumber.length() + 1)
+ "*" + System.lineSeparator();
result += bottomBorders;
System.out.print(result);
}
}
35 changes: 35 additions & 0 deletions src/main/java/ru/job4j/io/Task21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.job4j.io;

import java.util.Scanner;

public class Task21 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int columnCount = in.nextInt();
String star = "*";
String space = " ";
// YOUR CODE
int level = 2;
String topSide = star.repeat(columnCount);
int currentColumn = 1;
// Количество символов на каждом уровне кроме первого и последнего, чтобы не печатать ненужные крайние пробелы.
int levelColumnCount;
StringBuilder result = new StringBuilder(topSide).append(System.lineSeparator());
while (level < columnCount) {
levelColumnCount = level <= columnCount / 2 ? columnCount - level + 1 : level;
while (currentColumn <= levelColumnCount) {
if (currentColumn == level || currentColumn == columnCount - level + 1) {
result.append(star);
} else {
result.append(space);
}
currentColumn++;
}
result.append(System.lineSeparator());
level++;
currentColumn = 1;
}
result.append(topSide);
System.out.print(result);
}
}
20 changes: 20 additions & 0 deletions src/test/java/ru/job4j/io/Task14Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task14Test {

@Test
public void whenDelimiterIsPastedBetweenLettersOfString() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task14.main(null);
assertThat(out.toString(), is("H-e-l-l-o-,-f-r-i-e-n-d!"));
}
}
20 changes: 20 additions & 0 deletions src/test/java/ru/job4j/io/Task15Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task15Test {
@Test
public void eachSequenceOfSameSymbolsOnSeparateLine() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task15.main(null);
String expected = String.format("+++%S!!!!%S??", System.lineSeparator(), System.lineSeparator());
assertThat(out.toString(), is(expected));
}
}
19 changes: 19 additions & 0 deletions src/test/java/ru/job4j/io/Task16Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task16Test {
@Test
public void eachSequenceOfSameSymbolsOnOneLine() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task16.main(null);
assertThat(out.toString(), is("+++!!!!??"));
}
}
21 changes: 21 additions & 0 deletions src/test/java/ru/job4j/io/Task17Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task17Test {
@Test
public void shouldOutputSqrAndCubeOf6() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task17.main(null);
String expected = "6" + System.lineSeparator() + "36" + System.lineSeparator()
+ "216";
assertThat(out.toString(), is(expected));
}
}
20 changes: 20 additions & 0 deletions src/test/java/ru/job4j/io/Task18Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task18Test {
@Test
public void shouldOutputStringPlusNumber() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task18.main(null);
String expected = "Значение x равно 9" + System.lineSeparator() + "Значение x^2 равно 81";
assertThat(out.toString(), is(expected));
}
}
22 changes: 22 additions & 0 deletions src/test/java/ru/job4j/io/Task19Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task19Test {
@Test
public void shouldOutputStringWithTwoVariables() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task19.main(null);
String expected = "Значение x равно 4; значение y равно 11" + System.lineSeparator()
+ "Произведение x и y равно 44" + System.lineSeparator()
+ "Сумма x и y равно 15";
assertThat(out.toString(), is(expected));
}
}
23 changes: 23 additions & 0 deletions src/test/java/ru/job4j/io/Task20Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task20Test {
@Test
public void shouldOutputFullNameAndPhoneInFrame() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
Task20.main(null);
String expected = "************************" + System.lineSeparator()
+ "* Фамилия Имя Отчество *" + System.lineSeparator()
+ "* +7(938) 123-45-67 *" + System.lineSeparator()
+ "************************";
assertThat(out.toString(), is(expected));
}
}
67 changes: 67 additions & 0 deletions src/test/java/ru/job4j/io/Task21Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.job4j.io;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class Task21Test {
@Test
public void whenHourglassWidthEquals3() {
ByteArrayInputStream in = new ByteArrayInputStream("5".getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.setIn(in);
Task21.main(null);
String expected =
"*****" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
"*****";
assertThat(out.toString(), is(expected));
}

@Test
public void whenHourglassWidthEquals8() {
ByteArrayInputStream in = new ByteArrayInputStream("8".getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.setIn(in);
Task21.main(null);
String expected =
"********" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" **" + System.lineSeparator() +
" **" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
"********";
assertThat(out.toString(), is(expected));
}

@Test
public void whenHourglassWidthEquals9() {
ByteArrayInputStream in = new ByteArrayInputStream("9".getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.setIn(in);
Task21.main(null);
String expected =
"*********" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
" * *" + System.lineSeparator() +
"*********";
assertThat(out.toString(), is(expected));
}
}