Національний технічний університет України
«Київський політехнічний інститут ім. Ігоря Сікорського»
Кафедра цифрових технологій в енергетиці
ЗВІТ з виконання лабораторної роботи №5з дисципліни «Програмування на мові Java»
«КЛАСИ І ЇХНЄ ДОКУМЕНТУВАННЯ»
Варіант 13
Завдання 2
package org.example;import java.util.Objects;import java.util.Scanner;/** * Representing an ellipse in the Cartesian coordinate system. */class Ellipse { private double a; private double b; /** * Constructor for the Ellipse class. * * @param a Length of axis "a" of the ellipse. * @param b Length of axis "b" of the ellipse. * @throws IllegalArgumentException If both lengths are less than 0. */ public Ellipse(double a, double b) { if (a < 0 && b < 0) { throw new IllegalArgumentException("It's not an ellipse!"); } this.a = a; this.b = b; } // (x^2 / a^2) + (y^2 / b^2) = 1 /** * Calculates y for a given x in the ellipse. * * @param x x for calculating y. * @return y for the specified x. */ public double calculateY(double x) { return b * Math.sqrt(1 - Math.pow(x / a, 2)); } /** * Calculates x for a given y in the ellipse. * * @param y y for calculating x. * @return x for the specified y. */ public double calculateX(double y) { return a * Math.sqrt(1 - Math.pow(y / b, 2)); } public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } /** * Порівнює два еліпси на рівність. * * @param obj Об'єкт для порівняння. * @return true, якщо еліпси рівні; false, в іншому випадку. */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Ellipse ellipse = (Ellipse) obj; return Double.compare(ellipse.a, a) == 0 && Double.compare(ellipse.b, b) == 0; } /** * Генерує хеш-код для еліпса. * * @return Хеш-код еліпса. */ @Override public int hashCode() { return Objects.hash(a, b); } /** * Повертає рядкове представлення еліпса. * * @return Рядкове представлення еліпса. */ @Override public String toString() { return "Ellipse: a=" + a + ", b=" + b; }}/** * Represents a Task2. */public class Task2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input a: "); double a = scanner.nextDouble(); System.out.print("Input b: "); double b = scanner.nextDouble(); Ellipse ellipse = new Ellipse(a, b); System.out.println(ellipse); double x = ellipse.calculateX(1.5); System.out.printf("X = %.5f\n", x); double y = ellipse.calculateY(2); System.out.printf("Y = %.5f\n", y); }}
/
Тести:
package org.example;import java.util.Objects;import static org.junit.jupiter.api.Assertions.*;class Task2Test { @org.junit.jupiter.api.Test void calculateY() { Ellipse ellipse = new Ellipse(3.0, 2.0); double x = 2.0; double y = ellipse.calculateY(x); assertEquals(1.49071, y, 0.00001); } @org.junit.jupiter.api.Test void calculateX() { Ellipse ellipse = new Ellipse(3.0, 2.0); double y = 1.5; double x = ellipse.calculateY(y); assertEquals(1.73205, x, 0.00001); } @org.junit.jupiter.api.Test void testEquals() { Ellipse ellipse1 = new Ellipse(3.0, 2.0); Ellipse ellipse2 = new Ellipse(3.0, 2.0); boolean result = ellipse1.equals(ellipse2); assertTrue(result); } @org.junit.jupiter.api.Test void testHashCode() { Ellipse ellipse1 = new Ellipse(3.0, 2.0); Ellipse ellipse2 = new Ellipse(3.0, 2.0); assertEquals(ellipse1.hashCode(), ellipse2.hashCode()); } @org.junit.jupiter.api.Test void testToString() { Ellipse ellipse1 = new Ellipse(3.0, 2.0); String expected = "Ellipse: a=3.0, b=2.0"; String actual = ellipse1.toString(); assertEquals(expected, actual); }}
/
Завдання 14
package org.example;import java.util.Objects;import java.util.Scanner;/** * Represents a triangle with three sides. */class Triangle { private double a; private double b; private double c; /** * Constructs a Triangle with specified side lengths. * * @param a Length of side A. * @param b Length of side B. * @param c Length of side C. * @throws IllegalArgumentException If the provided side lengths do not form a valid triangle. */ public Triangle(double a, double b, double c) { if (!isTriangle(a, b, c)) { throw new IllegalArgumentException("It's not a triangle!"); } this.a = a; this.b = b; this.c = c; } /** * Checks if the given side lengths can form a valid triangle. * * @param a Length of side A. * @param b Length of side B. * @param c Length of side C. * @return True if the side lengths form a valid triangle, false otherwise. */ static boolean isTriangle(double a, double b, double c) { return a + b > c && a + c > b && b + c > a && a > 0 && b > 0 && c > 0; } public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } /** * Calculates the perimeter of the triangle. * * @return The perimeter of the triangle. */ public double calculatePerimeter() { return a + b + c; } /** * Calculates the area of the triangle using Heron's formula. * * @return The area of the triangle. */ public double calculateArea() { double p = calculatePerimeter() / 2; return Math.sqrt(p * (p - a) * (p - b) * (p - c)); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; Triangle triangle = (Triangle) obj; return Double.compare(triangle.a, a) == 0 && Double.compare(triangle.b, b) == 0 && Double.compare(triangle.c, c) == 0; } @Override public int hashCode() { return Objects.hash(a, b, c); } @Override public String toString() { return "Triangle: " + "a=" + a + ", b=" + b + ", c=" + c; }}/** * Represents a Task14. */public class Task14 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input a: "); double a = scanner.nextDouble(); System.out.print("Input b: "); double b = scanner.nextDouble(); System.out.print("Input b: "); double c = scanner.nextDouble(); Triangle triangle = new Triangle(a,b,c); System.out.println(triangle); double perimeter = triangle.calculatePerimeter(); System.out.println("Triangle Perimeter: " + perimeter); double area = triangle.calculateArea(); System.out.println("Triangle Area: " + area); }}
/
Тести:
package org.example;import org.junit.jupiter.api.Test;import java.util.Objects;import static org.junit.jupiter.api.Assertions.*;class Task14Test { @Test void testInvalidTriangle() { assertThrows(IllegalArgumentException.class, () -> new Triangle(1, 1, 3)); } @Test public void testIsTriangle() { assertTrue(Triangle.isTriangle(3, 4, 5)); assertFalse(Triangle.isTriangle(5, 12, 2)); } @Test void calculatePerimeter() { Triangle triangle = new Triangle(3, 4, 5); double expected = 12; assertEquals(expected, triangle.calculatePerimeter()); } @Test void calculateArea() { Triangle triangle = new Triangle(3, 4, 5); double expected = 6; assertEquals(expected, triangle.calculateArea()); } @Test void testEquals() { Triangle triangle1 = new Triangle(3, 4, 5); Triangle triangle2 = new Triangle(3, 4, 5); assertEquals(triangle1, triangle2); } @Test void testHashCode() { Triangle triangle1 = new Triangle(3, 4, 5); Triangle triangle2 = new Triangle(3, 4, 5); assertEquals(triangle1.hashCode(), triangle2.hashCode()); } @Test void testToString() { Triangle triangle = new Triangle(3, 4, 5); String expected = "Triangle: a=3.0, b=4.0, c=5.0"; String actual = triangle.toString(); assertEquals(expected, actual); }}
/
Завдання 28
package org.example;import java.util.Objects;import java.util.Scanner;/** * Represents a complex number with real and imaginary parts. */class Complex { private double re; private double im; /** * Constructs a complex number with the specified real and imaginary parts. * * @param re The real part of the complex number. * @param im The imaginary part of the complex number. */ public Complex(double re, double im) { this.re = re; this.im = im; } public double getRe() { return re; } public void setRe(double re) { this.re = re; } public double getIm() { return im; } public void setIm(double im) { this.im = im; } @Override public String toString() { return "(" + re + ", " + im + ")"; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Complex other = (Complex) obj; return Double.compare(re, other.re) == 0 && Double.compare(im, other.im) == 0; } @Override public int hashCode() { return Objects.hash(re, im); }}/** * The ExponentialComplex class extends the Complex class and represents a complex number * in its exponential form. */class ExponentialComplex extends Complex { /** * Constructs an exponential complex number with the specified real and imaginary parts. * * @param re The real part of the complex number. * @param im The imaginary part of the complex number. */ public ExponentialComplex(double re, double im) { super(re, im); } /** * Calculates and returns the exponential form of the complex number. * * @return The exponential form of the complex number. */ public double calculateExponential() { double r = Math.sqrt(Math.pow(this.getRe(),2)+Math.pow(this.getIm(),2)); double phi = Math.atan2(this.getIm(), this.getRe()); return Math.exp(r * Math.cos(phi)); }}/** * Represents a Task28. */public class Task28 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input real: "); int re = scanner.nextInt(); System.out.print("Input imaginary: "); int im = scanner.nextInt(); Complex complex = new Complex(re, im); System.out.println(complex); ExponentialComplex expComplex = new ExponentialComplex(complex.getRe(), complex.getIm()); System.out.println("Standard form: " + expComplex); System.out.println("Exponential form: " + expComplex.calculateExponential()); }}
/
Тести:
package org.example;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;class Task28ComplexTest { @Test void testToString() { Complex complex = new Complex(2, 3); assertEquals("(2.0, 3.0)", complex.toString()); } @Test void testEquals() { Complex complex1 = new Complex(2, 3); Complex complex2 = new Complex(2, 3); assertTrue(complex1.equals(complex2)); } @Test void testHashCode() { Complex complex1 = new Complex(2, 3); Complex complex2 = new Complex(2, 3); assertEquals(complex1.hashCode(), complex2.hashCode()); }}class Task28ExponentialComplexTest { @Test void testCalculateExponential() { ExponentialComplex expComplex = new ExponentialComplex(2, 3); double expected = 7.38905; assertEquals(expected, expComplex.calculateExponential(), 0.0001); }}
/
/
Завдання 4
package org.example;import java.util.Objects;import java.util.Scanner;/** * Represents a triangle with three sides. */class Triangle38 { private double side1; private double side2; private double side3; /** * Constructs a Triangle38 with given side lengths. * * @param side1 The length of the first side. * @param side2 The length of the second side. * @param side3 The length of the third side. * @throws IllegalArgumentException If the given side lengths do not form a valid triangle. */ public Triangle38(double side1, double side2, double side3) { if (!isTriangle(side1, side2, side3)) { throw new IllegalArgumentException("It's not a triangle!"); } this.side1 = side1; this.side2 = side2; this.side3 = side3; } /** * Checks if given side lengths form a valid triangle. * * @param a The length of the first side. * @param b The length of the second side. * @param c The length of the third side. * @return True if the given side lengths form a valid triangle, false otherwise. */ static boolean isTriangle(double a, double b, double c) { return a + b > c && a + c > b && b + c > a && a > 0 && b > 0 && c > 0; } public double calculateArea() { double p = (side1 + side2 + side3) / 2; return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Triangle38 triangle = (Triangle38) o; return Double.compare(triangle.side1, side1) == 0 && Double.compare(triangle.side2, side2) == 0 && Double.compare(triangle.side3, side3) == 0; } @Override public int hashCode() { return Objects.hash(side1, side2, side3); } @Override public String toString() { return "Triangle:" + "side1=" + side1 + ", side2=" + side2 + ", side3=" + side3; }}/** * Represents a quadrangle, extending the Triangle38 class. */class Quadrangle extends Triangle38 { private double side4; private double diagonal; /** * Constructs a Quadrangle with given side and diagonal lengths. * * @param side1 The length of the first side. * @param side2 The length of the second side. * @param side3 The length of the third side. * @param side4 The length of the fourth side. * @param diagonal The length of the diagonal. */ public Quadrangle(double side1, double side2, double side3, double side4, double diagonal) { super(side1, side2, side3); this.side4 = side4; this.diagonal = diagonal; } /** * Checks if given side lengths form a valid triangle. * * @param side1 The length of the first side. * @param side2 The length of the second side. * @param side3 The length of the third side. * @param side4 The length of the fourth side. * @param diagonal The length of the diagonal. * @return True if the given side lengths form a valid quadrangle, false otherwise. */ private boolean isValidQuadrangle(double side1, double side2, double side3, double side4, double diagonal) { return side1 + side2 > diagonal && side3 + side4 > diagonal; } public double getSide4() { return side4; } public void setSide4(double side4) { this.side4 = side4; } public double getDiagonal() { return diagonal; } public void setDiagonal(double diagonal) { this.diagonal = diagonal; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; Quadrangle that = (Quadrangle) o; return Double.compare(that.side4, side4) == 0 && Double.compare(that.diagonal, diagonal) == 0; } @Override public int hashCode() { return Objects.hash(super.hashCode(), side4, diagonal); } @Override public String toString() { return "Quadrangle: " + "side1=" + this.getSide1() + ", side2=" + this.getSide2() + ", side3=" + this.getSide3() + ", side4=" + side4 + ", diagonal=" + diagonal; }}/** * Represents a Task38. */public class Task38 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Input a: "); double a = scanner.nextDouble(); System.out.print("Input b: "); double b = scanner.nextDouble(); System.out.print("Input b: "); double c = scanner.nextDouble(); Triangle38 triangle = new Triangle38(a, b, c); System.out.println("Triangle Area: " + triangle.calculateArea()); System.out.println(triangle); System.out.print("Input d: "); double d = scanner.nextDouble(); System.out.print("Input diagonal: "); double diagonal = scanner.nextDouble(); Quadrangle quadrangle = new Quadrangle(a, b, c, d, diagonal); System.out.println(quadrangle); }}
/
Тести:
package org.example;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;class Task38TriangleTest { @Test public void testIsTriangle() { assertTrue(Triangle38.isTriangle(3, 4, 5)); assertFalse(Triangle38.isTriangle(3, 4, 10)); } @Test public void testCalculateArea() { Triangle38 triangle = new Triangle38(3, 4, 5); assertEquals(6, triangle.calculateArea()); } @Test void testEquals() { Triangle38 triangle1 = new Triangle38(3, 4, 5); Triangle38 triangle2 = new Triangle38(3, 4, 5); assertEquals(triangle1.hashCode(), triangle2.hashCode()); } @Test void testHashCode() { Triangle38 triangle1 = new Triangle38(3, 4, 5); Triangle38 triangle2 = new Triangle38(3, 4, 6); assertNotEquals(triangle1.hashCode(), triangle2.hashCode()); } @Test void testToString() { Triangle38 triangle = new Triangle38(3, 4, 5); assertEquals("Triangle:side1=3.0, side2=4.0, side3=5.0", triangle.toString()); }}class Task38QuadrangleTest { @Test public void testEquals() { Quadrangle quadrangle1 = new Quadrangle(3, 4, 5, 6, 7); Quadrangle quadrangle2 = new Quadrangle(3, 4, 5, 7, 7); assertNotEquals(quadrangle1, quadrangle2); } @Test public void testHashCode() { Quadrangle quadrangle1 = new Quadrangle(3, 4, 5, 6, 7); Quadrangle quadrangle2 = new Quadrangle(3, 4, 5, 6, 7); assertEquals(quadrangle1.hashCode(), quadrangle2.hashCode()); } @Test public void testToString() { Quadrangle quadrangle = new Quadrangle(3, 4, 5, 6, 7); String expected = "Quadrangle: side1=3.0, side2=4.0, side3=5.0, side4=6.0, diagonal=7.0"; assertEquals(expected, quadrangle.toString()); }}
/
/