Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.util.Comparator;
  2. public class RoomComporator implements Comparator<Room> {
  3.  
  4. public RoomComporator(){}
  5.  
  6.  
  7. @Override
  8. public int compare(Room r1, Room r2 ) {
  9.  
  10. return r1.square- r2.square;}
  11.  
  12. }
  13.  
  14. import java.util.List;
  15. import java.util.Collections;
  16. import java.util.LinkedList;
  17.  
  18. public class Room {
  19.  
  20. int length;
  21. int width;
  22. int square;
  23.  
  24.  
  25. public int getWidth(){
  26. return this.width;
  27. }
  28.  
  29. public void setWidth(int width){
  30. this.width = width;
  31. }
  32.  
  33. public int getLength(){
  34. return this.length;
  35. }
  36.  
  37. public int setLength(int length){
  38. return this.length = length;
  39. }
  40. public Room(int l, int w) {
  41. this.length = l;
  42. setWidth(w);
  43. }
  44. void square(){
  45. int l, w;
  46. l = length;
  47. w = width;
  48.  
  49. int s = w*l;
  50. System.out.println(s);
  51.  
  52. }
  53.  
  54. Room (int square){
  55. this.square=square;
  56. }
  57.  
  58.  
  59. void printRoom(){
  60. System.out.println(length + " " + width);
  61. }
  62.  
  63. public static void main(String[] args){
  64.  
  65. List<Room> rooms = new LinkedList <Room>();
  66. int numRooms = 20;
  67. int maxLength = 9;
  68. int maxWidth = 9;
  69.  
  70.  
  71.  
  72. for( int i = 0; i < numRooms; i++)
  73. {
  74. int length = 1 + (int)(Math.random()*maxLength);
  75. int width = 1 + (int)(Math.random()*maxWidth);
  76. Room r = new Room(length, width);
  77. System.out.println("Room " + (i + 1));
  78. r.printRoom();
  79.  
  80. r.square();
  81. rooms.add(r);
  82.  
  83. }
  84.  
  85.  
  86. for( int i = 0; i < numRooms; i++){
  87. Collections.sort(rooms, new RoomComporator());
  88. System.out.println(rooms.get(i).square);}
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement