Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an error-free Java program to do the following things Write a class called

ID: 3607032 • Letter: W

Question

Write an error-free Java program to do the following things Write a class called "Vec2D". The class has (at least) two private data members which correspond to the x and y coordinates of a 2D vector The class Vec2D should have several (methods) functions set the coordinates, display the vector in Cartesian (x.y) format, and display the vector in polar (r, theta) format. The main program should declare two objects of type Vec2D. It should set the coordinates of the first object to length = 5, angle =-/5 (that is, polar coordinates) and the coordinates of the second object to x -4.5, y- 10 (Cartesian coordinates) The main program should then print object 1 in a Cartesian (x.y) format and polar format (with theta in degrees) and object 2 in Cartesian and polar format. Good programming design is to make your objects and methods general in order to be used in multiple contexts. Thus, write your code so that the println statements are in the main module and it calls method(s) for vl and v2 to access the data. Output +Dv1 : x 4.045084971874737 y 2.938926261462366 v2 : x = 4.500000000000001 y = 10.0 ·Lv2: r - 10.965856099730654 th = 65.77225468204583

Explanation / Answer

import java.math.*;

public class Vec2d
{
   private double x,y,theta,r;

   String name;

   Vec2d(String n)
   {
    
      name=n;
   }
   public void setCartesianCordinates(double a, double b)
   {
      x=a;
      y=b;
      setPolarCordinates();
   }
   public void setPolarCordinates(double a, double b)
   {
      r=a;
      theta=b;
    
      setCartesianCordinates();
   }

    
    
   public void setCartesianCordinates()
   {
      double radiant = theta * (Math.PI/180);
    
      x = r * Math.cos(radiant);
      y = r * Math.sin(radiant);
   }
   public void setPolarCordinates()
   {
      double rad = Math.atan2(y, x);
      theta=rad * (180/Math.PI);
      r = Math.sqrt(x*x + y*y);
   }

   public void printCartesian()
   {
      System.out.println(name+": x = "+x+", y= "+y);
   }
   public void printPolar()
   {
      System.out.println(name+": r = "+r+", th= "+theta);
   }


   public static void main(String a[])
   {
      Vec2d v1= new Vec2d("V1");
    
      v1.setPolarCordinates(5, 180/5);
    
    
      Vec2d v2= new Vec2d("V2");
      v2.setCartesianCordinates(4.5, 10);
    
    

      v1.printCartesian();
      v1.printPolar();
    
      v2.printCartesian();
      v2.printPolar();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote