In Java Define a class called Comp3 or something similar The

In Java:

Define a class called “Comp3” (or something similar). The class has three private data members (and no public data members): the x, y, and z components of a point in 3D space.

Comp3D should have two constructor functions. The default constructor should set all of the components to zero. The second constructor should have three parameters (say, x1, x2 and x3) and should set the x, y, z components equal to x1, x2, x3, respectively.

The main program should declare three objects of type Comp3 in the following manner:

Comp3D A = new Comp3D();
Comp3D B = new Comp3D(3, -4.5, 1.7);
Comp3D C = new Comp3D();

In the main program, prompt the user for new x, y, z components for A and set the data elements of A equal to the user inputs. Display the components of A after they have been set.

The main program should then set C = A + B. The function that you use can be called “add”, “plus” or some other relevant name. The add method should add the x, y, z components of one object to the x, y, z components of the other object.

The program should scale the components of A by 2.5. In other words, all the components for A are multiplied by 2.5. Display the components of A after they have been scaled.

The main program should then determine which point (B or C) has the greatest magnitude (greatest distance from origin) and then print out the (x,y,z) components of the furthest point. (The components should be printed out from the main program.) If B and C are the same distance, then print the components from either point.

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Comp3D
{
   private double x;
   private double y;
   private double z;
  
   public Comp3D() //default constructor
   {
       x=0;
       y=0;
       z=0;
   }
   public Comp3D(double x1,double x2,double x3) //parameterized constructor
   {
       x=x1;
       y=x2;
       z=x3;
   }
   public void setX(double x1) //set methods for x,y and z
   {
   x = x1;
   }
   public void setY(double x2)
   {
   y = x2;
   }
   public void setZ(double x3)
   {
   z = x3;
   }
   public void display() //display method
   {
   System.out.println(\" x = \" + x + \" y = \" + y + \"z = \" +z);  
   }
   public static Comp3D add(Comp3D A,Comp3D B) //add method to add two class objects
   {
       Comp3D C = new Comp3D();
       C.x = A.x + B.x;
       C.y = A.y + B.y;
       C.z = A.z + B.z;
       return C;
   }
   public Comp3D scale(double scalingFactor) //scale method to multiply by scaling factor
   {
   this.x = x*scalingFactor;
   this.y = y*scalingFactor;
   this.z = z*scalingFactor;
       return this;
   }
   public static Comp3D farthest(Comp3D A,Comp3D B) //farthest method to find the farthest point from origin
   {
       Comp3D result = new Comp3D();
       if(Math.abs(A.x) > Math.abs(B.x) && Math.abs(A.y) > Math.abs(B.y) && Math.abs(A.z) > Math.abs(B.z))
       result = A;
       else
       result = B;
       return result;
   }
}
class TestComp3D
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in);
       double x,y,z;
    Comp3D A = new Comp3D();
       Comp3D B = new Comp3D(3, -4.5, 1.7);
       Comp3D C = new Comp3D();
       Comp3D F = new Comp3D();
      
       System.out.println(\"Enter the values of x,y and z for A\");
       x = scan.nextDouble();
       y = scan.nextDouble();
       z = scan.nextDouble();
      
       A.setX(x);
       A.setY(y);
       A.setZ(z);
      
       System.out.println(\"A:\");
       A.display();
      
       System.out.println(\"B:\");
       B.display();
      
   C = Comp3D.add(A,B);
   System.out.println(\"C = A + B:\");
       C.display();
      
       A.scale(2.5);
       System.out.println(\"A after scaling(2.5) :\");
       A.display();
      
       F = Comp3D.farthest(A,B);
       System.out.println(\"Farthest from origin :\");
       F.display();
      
      
   }
}

output:

Success time: 0.06 memory: 711680 signal:0

In Java: Define a class called “Comp3” (or something similar). The class has three private data members (and no public data members): the x, y, and z components
In Java: Define a class called “Comp3” (or something similar). The class has three private data members (and no public data members): the x, y, and z components
In Java: Define a class called “Comp3” (or something similar). The class has three private data members (and no public data members): the x, y, and z components

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site