Write a NetBeans solution that creates two points from the Point class, and calc
ID: 3697176 • Letter: W
Question
Write a NetBeans solution that creates two points from the Point class, and calculates the diagonal of the rectangle (the formula is diagonal = length2 + width2), the perimeter of the rectangle, the area of the rectangle, the number of points created , and the number of rectangles created. The solution should contain two Java classes called TestRectangle.java (which includes the main method), and Point.java. You should use the showInputDialog method to get the coordinates of both points, and showMessageDialog method to put the calculated results. Both methods are from the JOptionPane class, located in the javax.swing package. To convert the input from Strings to ints, you can use the Integer.parseInt method. For example:
int x1 = Integer.parseInt(JOptionPane.showInputDialog("Enter x1:"));
To output the calculated results to a single showMessageDialog box, you can build the output by concatenating the results into an output variable. For example, the following statements will yield output = “abcdef”:
String output = "abc";
output += "def";
JOptionPane.showMessageDialog(null, output);
After this output, set new values to the two points that double the values of the original coordinates, and put out another output dialog box that has the recalculated results. The Point class should have “private” modifiers for data fields x and y, and “private static” for numberOfPoints and numberOfRectangles. You will need to use “getter” methods to access these variables and “setter” methods to modify these private variables.
Explanation / Answer
public class Rectangle
{
double length, height, rectArea; // assigning variables for length, height and area of the rectangle
Point ul; // assigning variables for all corners
Point ur;
Point lr;
Point ll;
Rectangle(Point p_ul, Point p_ur, Point p_lr, Point p_ll) { //constructor assigning Point variables
ul = p_ul;
ur = p_ur;
lr = p_lr;
ll = p_ll;
}
Rectangle(int x_ul, int y_ul, int x_ur, int y_ur, int x_lr, int y_lr, int x_ll, int y_ll) { //constructor assigning coordinates for each corner Point variable
ul = new Point (x_ul, y_ul);
ur = new Point (x_ur, y_ur);
lr = new Point (x_lr, y_lr);
ll = new Point (x_ll, y_ll);
}
public double rectArea() {
double length = Math.sqrt( (ur.getX()-ul.getX()) * (ur.getX()-ul.getX()) ) + ( (ur.getY()-ul.getY()) * (ur.getY()-ul.getY()) ); //calculates length
double height = Math.sqrt( (ur.getX()-lr.getX()) * (ur.getX()-lr.getX()) ) + ( (ur.getY()-lr.getY()) * (ur.getY()-lr.getY()) ); //calculates height
return(length * height);
}
void displayInfo() { //Print statements
System.out.println("Rectangle Info: ");
System.out.println("Upper Left Point (x,y) : (" + ul.getX() + "," + ul.getY() + ")");
System.out.println("Upper Right Point (x,y) : (" + ur.getX() + "," + ur.getY() + ")");
System.out.println("Lower Left Point (x,y) : (" + ll.getX() + "," + ll.getY() + ")");
System.out.println("Lower Right Point (x,y) : (" + lr.getX() + "," + lr.getY() + ")");
System.out.println("Area: " + rectArea);
System.out.println("Length: " + length);
System.out.println("Height: " + height);
System.out.println("ur-lr(Y): " + (ur.getY()-lr.getY()));
System.out.println("ur-lr(X): " + (ur.getX()-lr.getX()));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.