0% found this document useful (0 votes)
6 views4 pages

Java 2D Point and Resizable Rectangle Classes

The document describes a Java program with two main classes: MyPoint, which models a 2D point and includes methods for distance calculations, and Rectangle, which implements a Resizable interface to allow resizing of its dimensions. The MyPoint class includes constructors, setters, getters, and distance methods, while the Rectangle class provides methods to resize width and height. The main methods demonstrate the functionality of both classes with example outputs.

Uploaded by

Hitesh hitesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Java 2D Point and Resizable Rectangle Classes

The document describes a Java program with two main classes: MyPoint, which models a 2D point and includes methods for distance calculations, and Rectangle, which implements a Resizable interface to allow resizing of its dimensions. The MyPoint class includes constructors, setters, getters, and distance methods, while the Rectangle class provides methods to resize width and height. The main methods demonstrate the functionality of both classes with example outputs.

Uploaded by

Hitesh hitesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A class called MyPoint, which models a 2D point with x and y coordinates, is designed as follows:

class MyPoint

private int x;

private int y;

public MyPoint()

this.x = 0;

this.y = 0;

public MyPoint(int x, int y)

this.x = x;

this.y = y;

public void setXY(int x, int y) {

this.x = x;

this.y = y;

public int[] getXY()

return new int[] { x, y };

@Override

public String toString()

return "(" + x + ", " + y + ")";

public double distance(int x, int y)

int dx = this.x -x;


int dy = this.y -y;

return [Link](dx * dx + dy * dy);

public double distance(MyPoint another)

return distance (another.x, another.y);

public double distance()

return distance(0, 0);

public int getX()

return x;

public int getY()

return y;

public class Program4

public sta c void main(String[] args)

MyPoint p1 = new MyPoint();

[Link]("Point p1: " + p1); // Should print (0, 0)

MyPoint p2 = new MyPoint(3, 4);

[Link]("Point p2: " + p2); // Should print (3, 4)

[Link](5, 6);

[Link]("A er se ng p1 to (5, 6): " + p1); // Should print (5, 6)

int[] coordinates = [Link]();


[Link]("Coordinates of p2: (" + coordinates[0] + ", " + coordinates[1] + ")"); // Should
print (3, 4)

[Link]("Distance from p1 to (1, 1): " + [Link](1, 1)); // Should print the calculated
distance

[Link]("Distance from p1 to p2: " + [Link](p2)); // Should print the calculated


distance

[Link]("Distance from p1 to origin: " + [Link]()); // Should print the calculated


distance

Develop a JAVA program to create an interface Resizable with methods resize Width(int width) and
resize Height(int height) that allow an object to be resized. Create a class Rectangle that implements
the Resizable interface and implements the resize methods.

interface Resizable

void resizeWidth(int width); // Method to resize the width

void resizeHeight(int height); // Method to resize the height

class Rectangle implements Resizable {

private int width; // Store the width of the rectangle

private int height; // Store the height of the rectangle

public Rectangle(int width, int height) {

[Link] = width;

[Link] = height;

@Override

public void resizeWidth(int width) {

[Link] = width;

[Link]("New width: " + [Link]);

@Override

public void resizeHeight(int height) {

[Link] = height;
[Link]("New height: " + [Link]);

public void displayDimensions() {

[Link]("Current dimensions -> Width: " + [Link] + ", Height: " + [Link]);

public class P7 {

public sta c void main(String[] args) { BCS306A

Rectangle rect = new Rectangle(10, 20);

[Link]();

// Resize the width and height using the implemented methods

[Link](30);

[Link](40);

// Display the updated dimensions

[Link]();

*******Output*******

Display method in outer class

Display method in inner class

You might also like