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

Write a definition of a class named Point that can be used to store and manipula

ID: 3697838 • Letter: W

Question


Write a definition of a class named Point that can be used to store and manipulate the location of a point in the 2D plane. Provide a constructor to initialize the private data with default values. You will need to declare and implement the following member functions: A member function set that sets the private data after an object of this class is created. A member function distance to find the distance of the point from the origin using the distance formula d= squareroot x^2 + yY^2. A member function to swap the X and Y values. Two functions to retrieve the current coordinates of the point (one for X and Y). Write a test program that requests data for several points from the user, creates the points, then calls the member functions.

Explanation / Answer

The Program is developed in java programming language. For understanding comments is given in the code itself.

import java.util.Scanner;

public class Point {
private int x;
private int y;
  
Point(){ // constructor
x = 0;
y = 0;
}
  
public void Set_coordinates(int a,int b){ //Set x and y
System.out.println("Setting coordinates");
x = a;
y = b;
}
  
public void swap(){ // swap x and y variables
System.out.println("Swapping Numbers");
int temp;
temp = x;
x = y;
y = temp;
}
  
public void distance(){ // Calculate the Distance
System.out.println("Calculating Distance");
double distance;
double num1 = x;
double two = 2;
double num2 = y;
double x_square = Math.pow(num1,two);
double y_square = Math.pow(num2,two);
  
distance = Math.sqrt(x_square + y_square);
System.out.println("Distance is :"+ distance);
}
  
public static void main(String []args){
Point obj = new Point();
  
System.out.println("Enter the value of X and Y coordinates");
Scanner reader = new Scanner(System.in);
int x = reader.nextInt();
int y = reader.nextByte();
  
obj.Set_coordinates(x,y); //set coordinates
  
obj.distance(); // Calculate Distance
  
obj.swap(); // Swal coordinates
  
obj.distance();//Calculate Distance
  
}
}

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