-
Notifications
You must be signed in to change notification settings - Fork 17
Homework 5 second time #23
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
Open
frolovGeorgy
wants to merge
8
commits into
iaulitin:master
Choose a base branch
from
frolovGeorgy:homework_5_second_time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b318f57
Homework 5 completed
frolovGeorgy 4ffbe43
Homework 5 completed again
frolovGeorgy 470523e
edit ComplexNumber
frolovGeorgy da0299d
Merge branch 'homework_5' into homework_5_second_time
frolovGeorgy 6b97418
deleted unused imports, added constant names for goods
frolovGeorgy 5d1686a
Created custom exception, added new interface with getters
frolovGeorgy 41a48e7
Created custom exception, added new interface with getters
frolovGeorgy 9ff9fec
Merge remote-tracking branch 'origin/homework_5_second_time' into hom…
frolovGeorgy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,5 @@ | |
| .idea | ||
| *.iws | ||
| *.iml | ||
| *.ipr | ||
| *.ipr | ||
| /out/ | ||
47 changes: 47 additions & 0 deletions
47
src/ru/milandr/courses/complexnumber/NewComplexNumberInterface.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package ru.milandr.courses.complexnumber; | ||
|
|
||
| public interface NewComplexNumberInterface { | ||
|
|
||
| /** | ||
| * @param anotherNumber a number to add to current number | ||
| * @return complex number representing operation result | ||
| */ | ||
| NewComplexNumberInterface add(NewComplexNumberInterface anotherNumber); | ||
|
|
||
| /** | ||
| * @param anotherNumber a number to subtract from current number | ||
| * @return complex number representing operation result (this minus another) | ||
| */ | ||
| NewComplexNumberInterface subtract(NewComplexNumberInterface anotherNumber); | ||
|
|
||
| /** | ||
| * @param anotherNumber a number of times to take current number | ||
| * @return complex number representing operation result | ||
| */ | ||
| NewComplexNumberInterface multiply(NewComplexNumberInterface anotherNumber); | ||
|
|
||
| /** | ||
| * @param anotherNumber a number to divide by | ||
| * @return complex number representing operation result (this divided by another) | ||
| */ | ||
| NewComplexNumberInterface divide(NewComplexNumberInterface anotherNumber); | ||
|
|
||
| /** | ||
| * @return result of current complex number negating | ||
| */ | ||
| NewComplexNumberInterface negate(); | ||
|
|
||
| /** | ||
| * @return modulus value for current complex number | ||
| */ | ||
| double calculateModulus(); | ||
|
|
||
| /** | ||
| * @return argument value for current complex number | ||
| */ | ||
| double calculateArgument(); | ||
|
|
||
| double getReal(); | ||
|
|
||
| double getImagine(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package ru.milandr.courses.complexnumber.frolov; | ||
|
|
||
| import ru.milandr.courses.complexnumber.NewComplexNumberInterface; | ||
|
|
||
| public class Abobus implements NewComplexNumberInterface { | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface add(NewComplexNumberInterface anotherNumber) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface subtract(NewComplexNumberInterface anotherNumber) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface multiply(NewComplexNumberInterface anotherNumber) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface divide(NewComplexNumberInterface anotherNumber) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface negate() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public double calculateModulus() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public double calculateArgument() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public double getReal() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public double getImagine() { | ||
| return 0; | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
src/ru/milandr/courses/complexnumber/frolov/ComplexNumber.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package ru.milandr.courses.complexnumber.frolov; | ||
|
|
||
| import ru.milandr.courses.complexnumber.NewComplexNumberInterface; | ||
|
|
||
| public class ComplexNumber implements NewComplexNumberInterface { | ||
|
|
||
| private final double real; | ||
| private final double imagine; | ||
|
|
||
| public ComplexNumber(double a, double b) { | ||
| this.real = a; | ||
| this.imagine = b; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public NewComplexNumberInterface add(NewComplexNumberInterface anotherNumber) { | ||
| ComplexNumber myNumber = checkException(anotherNumber); | ||
| return new ComplexNumber((real + myNumber.real), (imagine + myNumber.imagine)); | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface subtract(NewComplexNumberInterface anotherNumber) { | ||
| ComplexNumber myNumber = checkException(anotherNumber); | ||
| return new ComplexNumber((real - myNumber.real), (imagine - myNumber.imagine)); | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface multiply(NewComplexNumberInterface anotherNumber) { | ||
| ComplexNumber myNumber = checkException(anotherNumber); | ||
| double newReal = (real * myNumber.real - imagine * myNumber.imagine); | ||
| double newImagine = real * myNumber.imagine + imagine * myNumber.real; | ||
| return new ComplexNumber(newReal, newImagine); | ||
| } | ||
|
|
||
| @Override | ||
| public NewComplexNumberInterface divide(NewComplexNumberInterface anotherNumber) { | ||
| ComplexNumber myNumber = checkException(anotherNumber); | ||
| double v = Math.pow(myNumber.real, 2) + Math.pow(myNumber.imagine, 2); | ||
| double newReal = (real * myNumber.real + imagine * myNumber.real) / v; | ||
| double newImagine = (myNumber.real * imagine - real * (myNumber.imagine)) / v; | ||
| return new ComplexNumber(newReal, newImagine); | ||
| } | ||
|
|
||
| @Override | ||
| public ComplexNumber negate() { | ||
| double newReal = real * -1; | ||
| double newImagine = imagine * -1; | ||
| return new ComplexNumber(newReal, newImagine); | ||
| } | ||
|
|
||
| @Override | ||
| public double calculateModulus() { | ||
| return Math.sqrt(Math.pow(real, 2) + Math.pow(imagine, 2)); | ||
| } | ||
|
|
||
| @Override | ||
| public double calculateArgument() { | ||
| if (imagine == 0 & real > 0) { | ||
| return Math.toRadians(0); | ||
| } | ||
|
|
||
| if (imagine == 0 & real < 0) { | ||
| return Math.toRadians(180); | ||
| } | ||
|
|
||
| if (real == 0 & imagine > 0) { | ||
| return Math.toRadians(90); | ||
| } | ||
|
|
||
| if (real == 0 & imagine < 0) { | ||
| return Math.toRadians(270); | ||
| } | ||
|
|
||
| return real >= 0 ? Math.atan(imagine / real) : Math.atan(imagine / real + Math.toRadians(90)); | ||
| } | ||
|
|
||
| @Override | ||
| public double getReal() { | ||
| return real; | ||
| } | ||
|
|
||
| @Override | ||
| public double getImagine() { | ||
| return imagine; | ||
| } | ||
|
|
||
| private ComplexNumber checkException(NewComplexNumberInterface someInstance) { | ||
| if (!(someInstance instanceof ComplexNumber)) { | ||
| throw new ComplexNumberException("Неправильный формат числа"); | ||
| } | ||
| return new ComplexNumber(getReal(), getImagine()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| String signOfImagine = imagine > 0 ? "+" : "-"; | ||
| return String.format("z = %.2f %s %.2f i", real, signOfImagine, Math.abs(imagine)); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| NewComplexNumberInterface f1 = new ComplexNumber(5, 14); | ||
| System.out.println(f1); | ||
| NewComplexNumberInterface f2 = new ComplexNumber(10, -6); | ||
| System.out.println(f2); | ||
| NewComplexNumberInterface sum = f1.add(f2); | ||
| System.out.println("сумма: " + sum); | ||
| NewComplexNumberInterface diff = f1.subtract(f2); | ||
| System.out.println("разность: " + diff); | ||
| NewComplexNumberInterface multi = f1.multiply(f2); | ||
| System.out.println("умножение: " + multi); | ||
| NewComplexNumberInterface div = f1.divide(f2); | ||
| System.out.println("деление: " + div); | ||
| NewComplexNumberInterface negative = f1.negate(); | ||
| System.out.println("негатив: " + negative); | ||
| double mod = f1.calculateModulus(); | ||
| System.out.println("модуль: " + mod); | ||
| double arg = f1.calculateArgument(); | ||
| System.out.println("аргумент: " + arg); | ||
|
|
||
| NewComplexNumberInterface f3 = new ComplexNumber(0, -6); | ||
| double arg3 = f3.calculateArgument(); | ||
| System.out.println("аргумент чисто мнимого числа: " + arg3); | ||
| NewComplexNumberInterface f4 = new ComplexNumber(10, 0); | ||
| double arg4 = f4.calculateArgument(); | ||
| System.out.println("аргумент действительного числа: " + arg4); | ||
|
|
||
| //Попытка передать невалидный аргумент | ||
| f1.add(new Abobus()); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/ru/milandr/courses/complexnumber/frolov/ComplexNumberException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package ru.milandr.courses.complexnumber.frolov; | ||
|
|
||
| public class ComplexNumberException extends RuntimeException { | ||
|
|
||
| public ComplexNumberException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1) Переопределяемый метод не может выбрасывать проверяемые исключения, которые выше по иерархии чем исключения в методе родительского класса. | ||
| 2) Конструктор дочернего класса должен включить в свой блок throws все классы исключений или их предков из блока throws конструктора родительского класса, к которому он обращается при создании объекта. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package ru.milandr.courses.farm.frolov; | ||
|
|
||
| import ru.milandr.courses.farm.frolov.animals.Cow; | ||
| import ru.milandr.courses.farm.frolov.animals.Goat; | ||
| import ru.milandr.courses.farm.frolov.animals.Pig; | ||
| import ru.milandr.courses.farm.frolov.goods.Cheese; | ||
| import ru.milandr.courses.farm.frolov.goods.Meat; | ||
| import ru.milandr.courses.farm.frolov.goods.Milk; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) throws InterruptedException { | ||
| FarmerAbobus tolyanchik = new FarmerAbobus("Толянчик"); | ||
|
|
||
| Pig pepa = new Pig("Пепа"); | ||
| Goat zuzya = new Goat("Зюзя"); | ||
| Cow zorka = new Cow("Зорька"); | ||
|
|
||
| tolyanchik.petAnAnimal(pepa); | ||
| tolyanchik.petAnAnimal(zuzya); | ||
| tolyanchik.petAnAnimal(zorka); | ||
|
|
||
| Milk milk = new Milk(); | ||
| Cheese cheese = new Cheese(); | ||
|
|
||
| Meat meat = (Meat) tolyanchik.collectGoods(pepa); | ||
|
|
||
| tolyanchik.collectGoods(milk); | ||
| tolyanchik.collectGoods(cheese); | ||
|
|
||
| System.out.println("Ждем 5,1 сек, чтобы молоко испортилось"); | ||
| Thread.sleep(5100); | ||
|
|
||
| tolyanchik.eatProduct(milk); | ||
| tolyanchik.eatProduct(cheese); | ||
| tolyanchik.eatProduct(meat); | ||
|
|
||
| System.out.println("Ждем еще 2,5 сек, чтобы мясо испортилось"); | ||
| Thread.sleep(2500); | ||
|
|
||
| tolyanchik.eatProduct(milk); | ||
| tolyanchik.eatProduct(cheese); | ||
| tolyanchik.eatProduct(meat); | ||
|
|
||
| tolyanchik.petAnAnimal(pepa); | ||
| tolyanchik.petAnAnimal(zuzya); | ||
| tolyanchik.petAnAnimal(zorka); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package ru.milandr.courses.farm.frolov; | ||
|
|
||
| import ru.milandr.courses.farm.Animal; | ||
| import ru.milandr.courses.farm.GenericFarmer; | ||
| import ru.milandr.courses.farm.Good; | ||
| import ru.milandr.courses.farm.frolov.animals.Pig; | ||
| import ru.milandr.courses.farm.frolov.goods.Meat; | ||
| import ru.milandr.courses.farm.frolov.goods.SuperGood; | ||
|
|
||
| public class FarmerAbobus implements GenericFarmer { | ||
| private final String name; | ||
|
|
||
| public FarmerAbobus(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public void collectGoods(Good good) { | ||
| ((SuperGood) good).getCollected(this); | ||
| } | ||
|
|
||
| public Good collectGoods(Pig pig) { | ||
| Good meat = pig.produceGoods(); | ||
| ((Meat) meat).getCollected(this); | ||
| return meat; | ||
| } | ||
|
|
||
| @Override | ||
| public void petAnAnimal(Animal animal) { | ||
| System.out.println(name + " погладил " + animal); | ||
| animal.produceSound(); | ||
| } | ||
|
|
||
| @Override | ||
| public void eatProduct(Good good) { | ||
| SuperGood realGood = (SuperGood) good; | ||
| if (realGood.isRotten()) { | ||
| System.out.println("не хочу есть испорченный " + realGood); | ||
| return; | ||
| } | ||
| System.out.println(name + " употребил " + realGood); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.milandr.courses.farm.frolov.animals; | ||
|
|
||
| import ru.milandr.courses.farm.Good; | ||
| import ru.milandr.courses.farm.frolov.goods.Milk; | ||
|
|
||
| public class Cow extends SuperAnimal { | ||
|
|
||
| public Cow(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| public void produceSound() { | ||
| System.out.println("му-му"); | ||
| } | ||
|
|
||
| @Override | ||
| public Good produceGoods() { | ||
| return new Milk(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Корова " + name; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Удалось найти не много информации. Буду показывать на примере