import java.awt.Rectangle; public class RectangleDemo { public static void main(String[] args) { Rectangle rect = new Rectangle(10, 20, 30, 40); System.out.println("Width of the Rectangle: " + rect.getWidth()); } }
import java.awt.Rectangle; public class RectangleDemo { public static void main(String[] args) { Rectangle rect = new Rectangle(); rect.setBounds(10, 20, 80, 60); int width = rect.width; System.out.println("Width of the Rectangle: " + width); } }This code creates a new Rectangle object with the dimensions (0, 0, 0, 0) and then sets its bounds to (10, 20, 80, 60) using the setBounds() method. The width of the rectangle is then accessed using the width attribute and the value is printed to the console. In both of these examples, the java.awt package is used, which is a part of the AWT library.