Exercise: ArrayList Practice - Create a Netbeans or Ecplise Java project.| (A).
ID: 3889601 • Letter: E
Question
Exercise: ArrayList Practice - Create a Netbeans or Ecplise Java project.| (A). the Rectangle class contains two data fields, length and width: type is int. an initializer constructor, an equals() method as usual, a toString() method: it returns "L: "+length+" W: "+width (B). the RectangleList class contains a data field named boxes, type ArrayList such that it stores Rectangle objects a no-arg constructor such that it instantiates boxes a loadBoxes() method which takes and int type parameter for the list size: the method adds size many Rectangle objects to the list such that for each added Rectangle, length is a randomly selected integer number between 1 and 3, width is also a randomly selected integer number between 11 and 13 a reduce() method such that for every given element of the list the method removes all the additional occurrences of the given element. That is, after reduction every element occurs once in the list. the method is void. Use a pair of nested for loops, the equals method of the Rectangle class and local variable(s) as needed, a toString() method such that it returns the toString() value of each list element (C). the Applications class contains the main method. In the method declare and instantiate a RectangleList object named list call loadBoxes() to add 18 Rectangles to the list test loadBoxes: use toString() to print the list to the console call reduce() test reduce(): print list to the console again and see if every element occurs once. Note that the reduced list can contain no more than 9 rectanglesExplanation / Answer
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author Sam
*/
public class Rectangle {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Rectangle other = (Rectangle) obj;
if (this.length != other.length) {
return false;
}
if (this.width != other.width) {
return false;
}
return true;
}
@Override
public String toString() {
return "L: " + length + " W: " + width;
}
}
class RectangleList {
private ArrayList<Rectangle> boxes;
public RectangleList() {
boxes = new ArrayList<>();
}
public void loadBoxes(int n) {
Random r = new Random(System.currentTimeMillis());
for(int i = 0; i < n; i++) {
int len = r.nextInt(3) + 1;
int width = r.nextInt(3) + 11;
boxes.add(new Rectangle(len, width));
}
}
public void reduce() {
for (int i = 0; i < boxes.size(); i++) {
System.out.println(boxes.get(i));
for (int j = i + 1; j < boxes.size(); j++)
if (boxes.get(i).equals(boxes.get(j))) {
boxes.remove(j);
j--;
}
}
}
@Override
public String toString() {
return boxes.toString();
}
}
class Application {
public static void main(String[] args) {
RectangleList list = new RectangleList();
list.loadBoxes(18);
System.out.println("List loaded: " + list);
list.reduce();
System.out.println("List reduced to: " + list);
}
}
let me know if you like the code!! :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.