Rahmadnet

Inner Class

Jul 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package Java_Innerclasses;
  2.  
  3. public class Test {
  4.  
  5.     // Please do not change anything.
  6.     public static void main(String[] args)
  7.     {
  8.         // TODO Auto-generated method stub
  9.        
  10.         Block b1 = new Block();
  11.        
  12.         Block.Flat f1 = b1.new Flat();
  13.         Block.Flat f2 = b1.new Flat();
  14.         Block.Flat f3 = b1.new Flat();
  15.        
  16.         Block.Flat.Room r1 = f1.new Room(5, 6, 2);
  17.         Block.Flat.Room r2 = f1.new Room(4, 6, 1);
  18.         Block.Flat.Room r3 = f1.new Room(5, 5, 2);
  19.        
  20.         Block.Flat.Room r4 = f2.new Room(5, 6, 2);
  21.         Block.Flat.Room r5 = f2.new Room(8, 8, 3);
  22.         Block.Flat.Room r6 = f2.new Room(4, 6, 1);
  23.         Block.Flat.Room r7 = f2.new Room(5, 5, 2);
  24.        
  25.         Block.Flat.Room r8 = f3.new Room(5, 6, 2);
  26.         Block.Flat.Room r9 = f3.new Room(8, 8, 3);
  27.         Block.Flat.Room r10 = f3.new Room(8, 12, 3);
  28.         Block.Flat.Room r11 = f3.new Room(5, 5, 2);
  29.        
  30.         int score = 0;
  31.        
  32.         if (Block.Process.getHeightRoom(r1) == 6)
  33.         {
  34.             score += 5;
  35.         }
  36.         if (Block.Process.getWidthRoom(r6) == 4)
  37.         {
  38.             score += 5;
  39.         }
  40.         if (Block.Process.getNumWindowRoom(r1) == 2)
  41.         {
  42.             score += 5;
  43.         }
  44.         if (Block.Process.isSquare(r3) == true)
  45.         {
  46.             score += 10;
  47.         }
  48.         if (Block.Process.getNumFlat(b1) == 3)
  49.         {
  50.             score += 15;
  51.         }
  52.         if (Block.Process.getNumRoom(f1) == 3)
  53.         {
  54.             score += 8;
  55.         }
  56.         if (Block.Process.getNumRoom(f2) == 4)
  57.         {
  58.             score += 8;
  59.         }
  60.         if (Block.Process.getTotalNumRoom(b1) == 11)
  61.         {
  62.             score += 20;
  63.         }
  64.        
  65.         Block.Flat.Room r = Block.Process.findMaxRoomArea(b1);
  66.         if (r != null && r.toString().equals("8/12/3"))
  67.         {
  68.             score += 24;
  69.         }
  70.         System.out.println("Your grade is " + score);
  71.  
  72.     }
  73.  
  74. }
  75.  
  76. package Java_Innerclasses;
  77.  
  78.  
  79. public class Block
  80. {
  81.     Flat[] flats;
  82.    
  83.     // inner class
  84.     class Flat
  85.     {
  86.         Room[] rooms;
  87.        
  88.         // inner class
  89.         class Room
  90.         {
  91.             private int width;
  92.             private int height;
  93.             private int numWindow;
  94.            
  95.             // costructor
  96.             // fill in the marked area
  97.             // Do not change anytjing else
  98.             // it should be added to array "rooms" when a room object is created
  99.             // Clearly, add new Room to end of array, Update the array
  100.            
  101.             public Room(int w, int h, int n)
  102.             {
  103.                 super();
  104.                 this.width = w;
  105.                 this.height = h;
  106.                 this.numWindow = n;
  107.                
  108.                 //******************MARKED AREA*****************
  109.                
  110.                 int i = 0; while (rooms[i] != null) i++;
  111.                 rooms[i] = this;
  112.                
  113.                 //*******************END_OF_MARKED_AREA**********
  114.             }
  115.            
  116.             // Please do not change anything in this method.
  117.             @Override
  118.             public String toString() {
  119.                 return width + "/" + height + "/" + numWindow;
  120.             }
  121.         }
  122.        
  123.         // constructor
  124.         // Fill in the marked area
  125.         // Do not change anything else
  126.         // it should be added to array, Update the array
  127.         public Flat()
  128.         {
  129.             super();
  130.             rooms =new Room[100];
  131.            
  132.             //***************MARKED AREA******************
  133.            
  134.             int i = 0; while(flats[i]!= null) i++;
  135.             flats[i] = this;
  136.            
  137.             //****************END OF MARKED AREA**********
  138.         }
  139.     }
  140.    
  141.     // static inner class
  142.     static class Process
  143.     {
  144.         // Fill in the blanks
  145.         // Return number of flats in given block object
  146.         // You must write it as one return command
  147.         static int getNumFlat(Block b)
  148.         {
  149.             int i = 0;
  150.            
  151.             while (b.flats[i]!=null)i++;
  152.            
  153.             return i;
  154.         }
  155.        
  156.         // Fill in the blanks
  157.         // Return number of rooms in given flat object
  158.         // You must write it as one return command
  159.        
  160.         static int getNumRoom(Flat f)
  161.         {
  162.             int i = 0; while (f.rooms[i]!=null) i++;
  163.             return i;
  164.         }
  165.        
  166.         // Fill in the blanks
  167.         // Return height of given room object
  168.         // You must write it as one return command
  169.         static int getHeightRoom(Flat.Room r)
  170.         {
  171.             return r.height;
  172.         }
  173.        
  174.         // Fill in the blanks
  175.         // Return width if given room object
  176.         // You must write it as one return command
  177.         static int getWidthRoom(Flat.Room r)
  178.         {
  179.             return r.width;
  180.         }
  181.        
  182.         // Fill in the blanks
  183.         // Return numWindow of given room object
  184.         // You must write it as one return command
  185.         static int getNumWindowRoom(Flat.Room r)
  186.         {
  187.             return r.numWindow;
  188.         }
  189.        
  190.         // Fill in the blanks
  191.         // Return total number of rooms in given block object
  192.         static int getTotalNumRoom(Block b)
  193.         {
  194.             int i = 0;
  195.             int sum = 0;
  196.             while (b.flats[i]!=null)
  197.             {
  198.                 Flat flat = b.flats[i];
  199.                 int j = 0;
  200.                 while (flat.rooms[j]!=null)
  201.                 {
  202.                     j++;
  203.                 }
  204.                 sum +=j;
  205.                 i++;
  206.             };
  207.             return sum;
  208.         }
  209.        
  210.         // Fill in the blanks
  211.         // Return true, if given room object is a square
  212.         // Return false, if else
  213.         // You must write it as one rturn command
  214.         static boolean isSquare(Flat.Room r)
  215.         {
  216.             return r.height == r.width ? true :false;
  217.         }
  218.        
  219.         // Fill in the blanks
  220.         // Scan all rooms in given block object
  221.         // Find the room which has maximum area
  222.         // Return it
  223.         static Flat.Room findMaxRoomArea(Block b)
  224.         {
  225.             Flat.Room biggestRoom = null;
  226.             int area = 0;
  227.            
  228.             int i = 0;
  229.            
  230.             while (b.flats[i]!=null)
  231.             {
  232.                 Flat flat = b.flats[i];
  233.                 int j = 0;
  234.                
  235.                 while (flat.rooms[j]!= null)
  236.                 {
  237.                     Flat.Room room = flat.rooms[j];
  238.                    
  239.                     if((room.width*room.height)>area)
  240.                     {
  241.                         area = room.width*room.height;
  242.                         biggestRoom = room;
  243.                     }
  244.                     j++;
  245.                 }
  246.                 i++;
  247.             };
  248.             System.out.println(biggestRoom);
  249.             return biggestRoom;
  250.         }
  251.     }
  252.     // constructor
  253.     // Please do not change anything in this constructor
  254.     public Block()
  255.     {
  256.         super();
  257.         flats = new Flat[100];
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment