Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

Створити клас Vector3d, що задається трійкою координат. Реалізувати методи: додавання і…

  1. User

    Створити клас Vector3d, що задається трійкою координат. Реалізувати методи: додавання і віднімання векторів, скалярний добуток векторів, множення на скаляр, порівняння векторів, обчислення довжини вектора, порівняння довжини вектора.Написати задачу на мові JAVA

  2. Assistant
    public class Vector3d {
        private double x, y, z;
    
        public Vector3d(double x, double y, double z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    
        public Vector3d add(Vector3d other) {
            return new Vector3d(this.x + other.x, this.y + other.y, this.z + other.z);
        }
    
        public Vector3d subtract(Vector3d other) {
            return new Vector3d(this.x - other.x, this.y - other.y, this.z - other.z);
        }
    
        public double dotProduct(Vector3d other) {
            return this.x * other.x + this.y * other.y + this.z * other.z;
        }
    
        public Vector3d scalarMultiply(double scalar) {
            return new Vector3d(this.x * scalar, this.y * scalar, this.z * scalar);
        }
    
        public double length() {
            return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
        }
    
        public boolean isEqual(Vector3d other) {
            return this.x == other.x && this.y == other.y && this.z == other.z;
        }
    
        public int compareTo(Vector3d other) {
            double thisLength = this.length();
            double otherLength = other.length();
            if (thisLength < otherLength) {
                return -1;
            } else if (thisLength > otherLength) {
                return 1;
            } else {
                return 0;
            }
        }
    
        public String toString() {
            return "[" + this.x + ", " + this.y + ", " + this.z + "]";
        }
    
        public static void main(String[] args) {
            Vector3d v1 = new Vector3d(2, 3, 4);
            Vector3d v2 = new Vector3d(1, 2, 3);
    
            System.out.println("v1 = " + v1);
            System.out.println("v2 = " + v2);
    
            System.out.println("v1 + v2 = " + v1.add(v2));
            System.out.println("v1 - v2 = " + v1.subtract(v2));
            System.out.println("v1 * v2 = " + v1.dotProduct(v2));
            System.out.println("v1 * 2 = " + v1.scalarMultiply(2));
    
            System.out.println("Length of v1: " + v1.length());
            System.out.println("Comparing v1 and v2: " + v1.compareTo(v2));
        }
    }
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.