Write a definition of a class named Point that can be used to store and manipula
ID: 3697838 • Letter: W
Question
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
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.