-
Notifications
You must be signed in to change notification settings - Fork 23
Виктор Сенин, Число с размерностью #8
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: master
Are you sure you want to change the base?
Changes from all commits
fceb76f
f400c59
f76d261
225f7bb
74cc2ad
b8dda45
f31f0da
0298296
3a2ac40
c785c1d
5e04566
0c4a701
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package ru.spbstu.kspt.task1; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| /** | ||
| * Класс размерности | ||
| */ | ||
| public class Dimensions { | ||
|
|
||
| Dimensions(){ | ||
|
|
||
| } | ||
|
|
||
| private Map<String,Double> dimens = new HashMap<>(); | ||
|
|
||
| static Dimensions getLengthDimensions() { | ||
| return new Dimensions().addDimension("км",100000).addDimension("м",100). | ||
| addDimension("дм",10).addDimension("см",1); | ||
| } | ||
|
|
||
| static Dimensions getTimeDimensions() { | ||
| return new Dimensions().addDimension("с",1).addDimension("мин",60). | ||
| addDimension("ч",3600); | ||
| } | ||
|
|
||
| static Dimensions getWeightDimensions() { | ||
| return new Dimensions().addDimension("кг",1000000).addDimension("г",1000). | ||
| addDimension("мг",1); | ||
| } | ||
|
|
||
| Dimensions addDimension(String name, double coefficient) { | ||
| dimens.put(name, coefficient); | ||
| return this; | ||
| } | ||
|
|
||
| double getDimensionValue(String dimension) { | ||
| if (!dimens.containsKey(dimension)) { | ||
| throw new IllegalArgumentException("Данная разменость не содержится в данном Map"); | ||
| } | ||
| return dimens.get(dimension); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| Dimensions dimension = (Dimensions) o; | ||
|
|
||
| return dimens != null ? dimens.equals(dimension.dimens) : dimension.dimens == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return dimens != null ? dimens.hashCode() : 0; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Dimension{" + dimens + '}'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,7 @@ public static void main(String[] args) { | |
| System.out.println("Hello World!"); | ||
| } | ||
| } | ||
|
|
||
|
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. Внезапно откуда-то появившиеся пустые строки. |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package ru.spbstu.kspt.task1; | ||
|
|
||
|
|
||
| /** | ||
| * Класс числа с размерностью | ||
| */ | ||
| class NumberWithDimension implements Comparable<NumberWithDimension>, NumberWithDimensionInterface { | ||
|
|
||
| private double number; | ||
|
|
||
| private String dimension; | ||
|
|
||
| private Dimensions dimensionClass; | ||
|
|
||
| @Override | ||
| public double getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDimension() { | ||
| return dimension; | ||
| } | ||
|
|
||
| NumberWithDimension(double number, String dimension, Dimensions dimensionClass) { | ||
| this.number = number; | ||
| this.dimension = dimension; | ||
| this.dimensionClass = dimensionClass; | ||
| } | ||
|
|
||
| /** | ||
| * Дублирующий метод для вычисления чисел с размерностью. Используется во многих дальнейших функциях. | ||
| */ | ||
| private double calculation(NumberWithDimension other) { | ||
|
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(!dimensionClass.equals(other.dimensionClass)) { | ||
| throw new IllegalArgumentException("Размерности не совпадают"); | ||
| } | ||
| return other.number * (other.dimensionClass.getDimensionValue(other.dimension) | ||
| / dimensionClass.getDimensionValue(dimension)); | ||
| } | ||
|
|
||
| /** | ||
| * Сложение двух вещественных чисел с размерностью. | ||
| */ | ||
| @Override | ||
| public NumberWithDimension plus(NumberWithDimension other) { | ||
| return new NumberWithDimension(number + calculation(other), dimension, dimensionClass); | ||
| } | ||
|
|
||
| /** | ||
| * Вычитание двух вещественных чисел с размерностью. | ||
| */ | ||
| @Override | ||
| public NumberWithDimension minus(NumberWithDimension other) { | ||
| return new NumberWithDimension(number - calculation(other), dimension, dimensionClass); | ||
|
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. Мелочь, но всё же есть некоторое дублирование между этим и двумя следующими методами. |
||
| } | ||
|
|
||
| /** | ||
| * Умножение вещественного числа с размерностью на вещественное число. | ||
| */ | ||
| @Override | ||
| public NumberWithDimension multiply(double multi) { | ||
| return new NumberWithDimension(number * multi, dimension, dimensionClass); | ||
| } | ||
|
|
||
| /** | ||
| * Деление вещественного числа с размерностью на вещественное число. | ||
| */ | ||
| @Override | ||
| public NumberWithDimension divide(double div) { | ||
| return new NumberWithDimension(number / div, dimension, dimensionClass); | ||
| } | ||
|
|
||
| /** | ||
| * Деление двух вещественных чисел друг на друга. В результате получаем вещественное число. | ||
| */ | ||
| @Override | ||
| public double divideForDim(NumberWithDimension other) { | ||
|
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. Этот метод также можно назвать |
||
| return number / calculation(other); | ||
| } | ||
|
|
||
| /** | ||
| * Перевод вещественного числа с размерностью из одной размерности в другую. | ||
| */ | ||
| @Override | ||
| public NumberWithDimension translate(String other) { | ||
|
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.
|
||
| return new NumberWithDimension(number * | ||
| (dimensionClass.getDimensionValue(dimension) / dimensionClass.getDimensionValue(other)), | ||
| other, dimensionClass); | ||
| } | ||
|
|
||
| /** | ||
| * Сравнение двух вещественных чисел с размерностью. | ||
| */ | ||
| @Override | ||
| public int compareTo(NumberWithDimension other) { | ||
| return (number ) > (calculation(other)) ? 1 : (number) == (calculation(other)) ? 0 : -1; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| NumberWithDimension numbers = (NumberWithDimension) o; | ||
|
|
||
| if (Double.compare(numbers.number, number) != 0) return false; | ||
| if (dimension != null ? !dimension.equals(numbers.dimension) : numbers.dimension != null) return false; | ||
| return dimensionClass != null ? dimensionClass.equals(numbers.dimensionClass) : numbers.dimensionClass == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result; | ||
| long temp; | ||
| temp = Double.doubleToLongBits(number); | ||
| result = (int) (temp ^ (temp >>> 32)); | ||
| result = 31 * result + (dimension != null ? dimension.hashCode() : 0); | ||
| result = 31 * result + (dimensionClass != null ? dimensionClass.hashCode() : 0); | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return "" + number + " " + dimension; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ru.spbstu.kspt.task1; | ||
|
|
||
| interface NumberWithDimensionInterface { | ||
| /** | ||
| * Взятие вещественного числа из класса. | ||
| */ | ||
| double getNumber(); // | ||
| /** | ||
| * Взятие размерности из класса в строковой форме. | ||
| */ | ||
| String getDimension(); | ||
| /** | ||
| * Сложение двух вещественных чисел с размерностью. | ||
| */ | ||
| NumberWithDimension plus(NumberWithDimension other); | ||
| /** | ||
| * Вычитание двух вещественных чисел с размерностью. | ||
| */ | ||
| NumberWithDimension minus(NumberWithDimension other); | ||
| /** | ||
| * Умножение числа с размерностью на вещественное число. | ||
| */ | ||
| NumberWithDimension multiply(double multi); | ||
| /** | ||
| * Деление числа с размерностью на вещественное число. | ||
| */ | ||
| NumberWithDimension divide(double div); | ||
| /** | ||
| * Деление числа с размерностью на другое число с размерностью. | ||
| */ | ||
| double divideForDim(NumberWithDimension other); | ||
| /** | ||
| * Перевод числа с размерностью из одной величины в другую. | ||
| */ | ||
| NumberWithDimension translate (String other); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.spbstu.kspt.task1; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class DimensionsTest { | ||
| @Test | ||
| void getValueDimension() { | ||
| assertEquals(100000, new Dimensions().getLengthDimensions().getDimensionValue("км")); | ||
| assertEquals(1000000, new Dimensions().getWeightDimensions().getDimensionValue("кг")); | ||
| assertEquals(3600, new Dimensions().getTimeDimensions().getDimensionValue("ч")); | ||
| assertEquals(1, new Dimensions().getLengthDimensions().getDimensionValue("см")); | ||
| assertEquals(1000, new Dimensions().getWeightDimensions().getDimensionValue("г")); | ||
| assertEquals(60, new Dimensions().getTimeDimensions().getDimensionValue("мин")); | ||
| try { | ||
| assertEquals(60, new Dimensions().getTimeDimensions().getDimensionValue("оки")); | ||
| assertEquals(60, new Dimensions().getTimeDimensions().getDimensionValue("кауаку")); | ||
| assertEquals(60, new Dimensions().getTimeDimensions().getDimensionValue("минутки")); | ||
| } | ||
| catch (IllegalArgumentException e){ | ||
| System.out.println("Ошибка поймана."); | ||
| } | ||
| } | ||
|
|
||
| } |
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.
Лишний конструктор