Question:
1. Write a program which instantiates a new Rectangle object. Use appropriate methods to set its x and y coordinates to 15 and 35 respectively, and to set its width to 10 and its height to 20. Use appropriate methods to access the values of each instance variable, and output them to screen.
The x coordinate is: 15.0
The y coordinate is: 35.0
The height is: 10.0
The width is: 20.0
Here is my code:
// gman
// 3/2/2012
// practical 2B
import java.awt.Rectangle;
public class rectangleQ1
{
public static void main(String []args)
{
Rectangle r1 = new Rectangle(15,35,20,10);
double x = r1.getX();
double y = r1.getY();
double height = r1.getHeight();
double width = r1.getWidth();
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("height is " + height);
System.out.println("width is " + width);
}
}
Here is the second part of question:
2. Adapt this program to allow the user to enter values for the width, height, x coordinate and y coordinate of the Rectangle. Use appropriate methods to set the instance variables of the Rectangle and output the values of all its instance variables.
Thanks in Advance for help:
Gman


Sign In
Create Account


Back to top









