Question category:Experiment
Keywords:object, class, packaging, construction method, UML class diagram
Content requirements:
Step 1, write a class named RECTANGLE, to represent rectangles. Please write the class diagram according to the figure below. This topic assumes that the color of all rectangular objects is the same, so the color uses static members. The class is placed in the shape package.
indicates that in the class diagram, the symbols of the data domain and the method: -Ed the Private;+indicates that the public.
Step 2, write a class named Utility, put it in a shape package. The following requirements are defined as follows three static methods:
(1) public static int compare(Rectangle rect1, Rectangle rect2)
Function:
If the area of RECT1 is larger than the area of RECT2, the return value is 1
If the area of RECT1 is smaller than the area of RECT2, the return value is -1
If the area of RECT1 is the same as the area of RECT2, the return value is 0
(2) public static void sort(Rectangle[] rectangles)
Function:
From large to small according to the area of the rectangle, sort the array RECTANGLES.
(3) public static void output(Rectangle[] rectangles)
Function:
Output all the rectangles in the array RECTANGLES in order.
Each line outputs a rectangle, and the output format of the rectangle is: [width, height] -area, all retain 2 decimal numbers.
Step 3, write a main class named Main, put in the main package. Implement the following functions in the main method:
(1) Create an array consisting of 10 rectangular objects. The size of each rectangle is determined by itself, and the color is “red”.
(2) Output these 10 rectangles;
(3) This 10 rectangular objects are descended according to the area;
(4) 10 rectangles after output sorting.
Remarks
Packing is a jar document that can be executed, which contains the source program file.
Implementation code:
Rectangle class
package shape;
/**
* @author zg
*/
public class Rectangle {
/**
* The width of the rectangular
*/
private double width;
/**
* The height of the rectangle
*/
private double height;
/**
* The color of the rectangular, the color uses static members
*/
private static String color="BLACK";
/**
* Non -cherishing structure
*/
public Rectangle() {
width=1;
height=1;
}
/**
* There are ginseng structures
* @param width rectangular width
* @param Height rectangular height
*/
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/**
*
* Accessor and modifier
*/
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
Rectangle.color=color;
}
/**
*
* @Return rectangle area
*/
public double getArea(){
return height*width;
}
/**
*
* @Return rectangle's perimeter
*/
public double getPerimeter(){
return 2*(height+width);
}
}
Utility class:
package shape;
import java.util.Arrays;
/**
* @author zg
*/
public class Utility {
/**
*
* @param RECT1 rectangular object
* @param RECT2 rectangular object
* @Return If the area of RECT1 is larger than the area of RECT2, and the return value is 1
* If the area of RECT1 is smaller than the area of RECT2, the return value is -1
* If the area of RECT1 is the same as the area of RECT2, the return value is 0
*/
public static int compare(Rectangle rect1, Rectangle rect2){
if(rect1.getArea()>rect2.getArea()){
return 1;
}
if(rect1.getArea()==rect2.getArea()) {
return 0;
}
return -1;
}
/**
*
* @param Rectangles rectangular object array
* According to large to small according to the area of the rectangle, sort the array RECTANGLES
*/
public static void sort(Rectangle[] rectangles){
Arrays.sort(rectangles, (o1, o2) -> Double.compare(o2.getArea(),o1.getArea()));
}
/**
*
* @param Rectangles rectangular object array
* Output all the rectangles in the array RECTANGLES in order
* Each row outputs a rectangle, the output format of the rectangle is: [width, height] -area, all retain 2 decimal
*
*/
public static void output(Rectangle[] rectangles){
for (Rectangle rectangle : rectangles) {
System.out.printf("[%.2f,%.2f] - %.2f",rectangle.getWidth(),rectangle.getHeight(),
rectangle.getArea());
System.out.println();
}
}
}
main class:
package main;
import shape.Rectangle;
import shape.Utility;
import java.util.Random;
/**
* @author zg
*/
public class Main {
public static void main(String[] args) {
Random random=new Random();
Rectangle[] rectangles=new Rectangle[10];
int tenRec = 10;
// Create an array consisting of 10 rectangular objects. The size of each rectangle is determined by itself. The color is "red"
for(int i = 0; i< tenRec; i++){
// Randomly generate width and height (1 ~ 101)
rectangles[i]=new Rectangle(random.nextInt(100)+1,random.nextInt(100)+1);
// The color is "red".
rectangles[i].setColor("RED");
}
// Output these 10 rectangles
System.out.println("Before sorting, the width, height and area of 10 rectangles:");
Utility.output(rectangles);
System.out.println();
// Sequence of these 10 rectangular objects according to the area
Utility.sort(rectangles);
// 10 rectangles after output sorting
System.out.println("After sorting, the width, height and area of 10 rectangles:");
Utility.output(rectangles);
}
}
Run the output of the main method (random, as the effect display):