-
Notifications
You must be signed in to change notification settings - Fork 103
lesson14 task1 #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: for-pr
Are you sure you want to change the base?
lesson14 task1 #110
Changes from 4 commits
33563a1
318e3ed
49bc972
b98a860
f815be3
cf5894b
3901d07
d9e3347
d6417a6
34ef3fc
7ea65ff
da9f70b
6b72e7e
88b156b
68f2859
c09db58
e95273d
bdaa498
e061808
45633b9
b852b24
0ce340c
364f204
3f65c1a
1975241
ee64b50
cf959f2
581d8eb
455de07
e125d86
fe8d83f
1fdcb45
6a9497e
97ae782
ba035de
dbcb8af
0ab1b7e
854c1b7
28bc2ac
5f13106
1f793d1
c975f83
570cd3b
2d5e2b4
db89cbc
1613dc4
1c95eac
fb22c28
0a713f0
148cd15
bbf9c23
c5db7fd
5583949
e9eefe4
4537115
c93f510
0a576e0
6452514
80bd8aa
25abfcd
4a00c95
6d74fe0
1b40cb1
2e0b784
c6d69e3
bfd5100
cb2ee42
d4a4804
407cfa3
e3b5269
6ad93a3
c947ca7
82fcc77
a8af1d3
f46b897
901504e
3f15ffd
e566251
1d546f2
08f6031
2b4e220
103ce78
ad8e5f5
501306c
7474b55
c738c0b
62e7730
962c590
5abfe49
f1a7b9a
5b17229
68240ee
751268c
f6a2af0
cdf1c4a
6a664d3
493af39
347457c
ffd43b2
12c0215
d406737
38caaf8
f5d11ff
eeb0e7f
f8585d4
d2b4906
498141d
78aa437
df675a8
f92de7b
d848de1
4d7ed83
f173163
3a530fa
7f9545f
a2b7ca3
8623eae
49cb8be
a2c243f
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,16 +1,50 @@ | ||
| package com.walking.lesson14_polymorphism.task1; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Реализуйте класс «Правильная фигура». | ||
| * Для него создайте классы-наследники «Треугольник» и «Квадрат». | ||
| * ���������� ����� ����������� ������. | ||
| * ��� ���� �������� ������-���������� ������������ � ��������. | ||
| * <p> | ||
| * Пользователь должен иметь возможность ввести длину стороны и выбрать тип фигуры. | ||
| * Программа должна нарисовать в консоли выбранную пользователем фигуру, | ||
| * используя символы '-', '|', '/', '\'. | ||
| * ������������ ������ ����� ����������� ������ ����� ������� � ������� ��� ������. | ||
| * ��������� ������ ���������� � ������� ��������� ������������� ������, | ||
| * ��������� ������� '-', '|', '/', '\'. | ||
| * <p> | ||
| * Обратите внимание, символ '\' в Java необходимо экранировать: '\\'. | ||
| * �������� ��������, ������ '\' � Java ���������� ������������: '\\'. | ||
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner in = new Scanner(System.in); | ||
| System.out.print("������� ��� ������: "); | ||
| String figureType = in.nextLine(); | ||
| System.out.print("������� ����� �������: "); | ||
| int length = in.nextInt(); | ||
|
|
||
| if (length < 1) { | ||
| System.out.println("������������ ����"); | ||
| return; | ||
| } | ||
|
|
||
| RightFigure figure = new RightFigure(length); | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| createShape(figure, figureType); | ||
|
|
||
| } | ||
|
|
||
| private static void createShape(RightFigure figure, String figureType) { | ||
| switch (figureType.toLowerCase()) { | ||
| case Triangle.NAME: | ||
| figure = new Triangle(figure.length); | ||
| figure.getTheFigure(); | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break; | ||
| case Square.NAME: | ||
| figure = new Square(figure.length); | ||
| figure.getTheFigure(); | ||
| break; | ||
| default: | ||
| figure.getTheFigure(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.walking.lesson14_polymorphism.task1; | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public class RightFigure { | ||
| int length; | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public RightFigure(int length) { | ||
| this.length = length; | ||
| } | ||
|
|
||
| protected void getTheFigure() { | ||
| System.out.println("Íåèçâåñòíàÿ ôèãóðà"); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.walking.lesson14_polymorphism.task1; | ||
|
|
||
| public class Square extends RightFigure { | ||
| public static final String NAME = "êâàäðàò"; | ||
|
|
||
| private final String HORIZONTAL = "-"; | ||
| private final String VERTICAL = "|"; | ||
| private final String SPACE = " "; | ||
|
|
||
| public Square(int length) { | ||
| super(length); | ||
| } | ||
|
|
||
| @Override | ||
| public void getTheFigure() { | ||
|
||
| getHorizontalSide(); | ||
| getVerticalSide(); | ||
| getHorizontalSide(); | ||
| } | ||
|
|
||
| private void getHorizontalSide() { | ||
| System.out.println(SPACE + HORIZONTAL.repeat(length - 2) + SPACE); | ||
| } | ||
|
|
||
| private void getVerticalSide() { | ||
| for(int i = 0; i < length - 2; i++) { | ||
| System.out.println(VERTICAL + SPACE.repeat(length - 2) + VERTICAL); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.walking.lesson14_polymorphism.task1; | ||
|
|
||
| public class Triangle extends RightFigure { | ||
| public static final String NAME = "òðåóãîëüíèê"; | ||
|
|
||
| private final String RIGHT_SIDE = "\\"; | ||
| private final String LEFT_SIDE = "/"; | ||
| private final String BOTTOM_SIDE = "_"; | ||
| private final String SPACE = " "; | ||
|
|
||
| public Triangle(int length) { | ||
| super(length); | ||
| } | ||
|
|
||
| @Override | ||
| public void getTheFigure() { | ||
| getTriangle(); | ||
| getBottomSide(); | ||
| } | ||
|
|
||
| private void getBottomSide() { | ||
| System.out.println(LEFT_SIDE + BOTTOM_SIDE.repeat(length) + RIGHT_SIDE); | ||
| } | ||
|
|
||
| private void getTriangle() { | ||
| int k = length / 2; | ||
| int j; | ||
| if (length % 2 == 0) { | ||
|
||
| j = 0; | ||
| } else { | ||
| j = 1; | ||
| } | ||
|
|
||
| for (int i = 0; i < length / 2; i++) { | ||
| System.out.println(SPACE.repeat(k) + LEFT_SIDE + SPACE.repeat(j) + RIGHT_SIDE); | ||
| k--; | ||
| j += 2; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task1_interface; | ||
|
|
||
|
|
||
| /** | ||
| * Реализуйте задачу | ||
| * <a href="https://github.com/KFalcon2022/practical-tasks/tree/master/src/com/walking/lesson14_polymorphism/task1">...</a> | ||
| * используя интерфейс. | ||
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task2; | ||
|
|
||
| public class Goodbye implements Printer { | ||
| final static String MASSAGE = "Bye"; | ||
|
||
| final static String RESPOND = "Goodbye"; | ||
|
|
||
| public static void print() { | ||
| System.out.println(RESPOND); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task2; | ||
|
|
||
| public class Hello implements Printer { | ||
| final static String MASSAGE = "Hi"; | ||
| final static String RESPOND = "Hello"; | ||
|
|
||
| public static void print() { | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| System.out.println(RESPOND); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task2; | ||
|
|
||
| public class How implements Printer { | ||
| final static String MASSAGE = "How are you"; | ||
| final static String RESPOND = "How are you doing"; | ||
|
|
||
| public static void print() { | ||
| System.out.println(RESPOND); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,30 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task2; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Реализуйте задачу | ||
| * <a href="https://github.com/KFalcon2022/practical-tasks/blob/master/src/com/walking/lesson3/Task2SwitchCase.java">...</a> | ||
| * с использованием интерфейсов. Каждая реализация должна возвращать свое сообщение. | ||
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner in = new Scanner(System.in); | ||
| System.out.println("Enter massage: "); | ||
| String massage = in.nextLine(); | ||
|
|
||
| switch (massage) { | ||
| case Hello.MASSAGE: | ||
| Hello.print(); | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break; | ||
| case Goodbye.MASSAGE: | ||
| Goodbye.print(); | ||
| break; | ||
| case How.MASSAGE: | ||
| How.print(); | ||
| break; | ||
| default: | ||
| Printer.print(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task2; | ||
|
|
||
| public interface Printer { | ||
|
|
||
| static void print() { | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| System.out.println("Unknown massage"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task3; | ||
|
|
||
| public abstract class Animal { | ||
|
|
||
| public Animal () { | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
|
|
||
| abstract void sound(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task3; | ||
|
|
||
| public class Cat extends Animal { | ||
| private final String SOUND = "meow"; | ||
|
|
||
| public Cat() { | ||
KFalcon2022 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| super(); | ||
| } | ||
|
|
||
| public void sound() { | ||
| System.out.println(SOUND); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task3; | ||
|
|
||
| public class Cow extends Animal { | ||
| private final String SOUND = "moo"; | ||
|
|
||
| public Cow() { | ||
| super(); | ||
| } | ||
|
|
||
| public void sound() { | ||
| System.out.println(SOUND); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.walking.lesson16_abstract_class_interface.task3; | ||
|
|
||
| public class Dog extends Animal { | ||
| private final String SOUND = "woof"; | ||
|
|
||
| public Dog() { | ||
| super(); | ||
| } | ||
|
|
||
| public void sound() { | ||
| System.out.println(SOUND); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.