Using netbeans java The purpose of this exercise is to effec

\"Using netbeans java\"

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of the object of that class, even it is against the intentions of the programmer who implemented the class. In order to prevent such an intrusion, accessors of reference type fields should not return the field itself, but rather a copy of it.

1. The ArrayPractice class has two private data fields, both are arrays. Declare the fields as

-- an array to store integer numbers of type int named numbers

-- an array to store Rectangle references (type is Rectangle) named boxes

\"Do not instantiate the fields at the declaration!\"

2. Each field has an associated accessor method as usual

3. There is a second accessor method associated to boxes named as getCopyOfBoxes, see the definition later.

4. Define a method named loadArrays( ). The method is void and takes no parameter. The method assigns the entries of the integer array and those of the Rectangle array such that for each array it

--runs a for loop to the length of the array

--entries of the int array assigned a random integer between 1 and 100

--each entry of the Rectangle array is assigned a new Rectangle object with random integer length and width values. Select the integers again between 1 and 100. Note that the individual Rectangle objects do not have names.

5. The class has a default constructor and an initializer constructor such that it takes one parameter, an int type value named size. The constructor

-instantiates the fields numbers and boxes such that the array length is size for both

-calls the loadArray() method

6. Define a method named copyNumbers( ). The method takes no parameter and returns an array integers, a copy of the numbers field. In the method declare and instantiate an array (a local variable) named say, temp, with the same type and to the same length as those of the field. The method runs a for loop to load the temp array such that it assigns each temp entry the corresponding numbers entry index-by-index. The method returns temp.

7. Define a method named copyBoxes( ). This method is entirely analogous to the previous method copyNumbers except in the for loop of we do NOT assign boxes[k] to temp[k], but rather a new Rectangle which is a copy of the boxes[k]. This copy must be instantiated by the copy constructor you added to the Rectangle class in Exercise 1 above. That is, the value assigned to temp[k] will be new Rectangle(boxes[k])

8. Complete the definition of the second accessor method getCopyOfBoxes() mentioned above so as to return copyBoxes( ) rather than boxes itself.
\"Using netbeans java\"

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of the object of that class, even it is against the intentions of the programmer who implemented the class. In order to prevent such an intrusion, accessors of reference type fields should not return the field itself, but rather a copy of it.

1. The ArrayPractice class has two private data fields, both are arrays. Declare the fields as

-- an array to store integer numbers of type int named numbers

-- an array to store Rectangle references (type is Rectangle) named boxes

\"Do not instantiate the fields at the declaration!\"

2. Each field has an associated accessor method as usual

3. There is a second accessor method associated to boxes named as getCopyOfBoxes, see the definition later.

4. Define a method named loadArrays( ). The method is void and takes no parameter. The method assigns the entries of the integer array and those of the Rectangle array such that for each array it

--runs a for loop to the length of the array

--entries of the int array assigned a random integer between 1 and 100

--each entry of the Rectangle array is assigned a new Rectangle object with random integer length and width values. Select the integers again between 1 and 100. Note that the individual Rectangle objects do not have names.

5. The class has a default constructor and an initializer constructor such that it takes one parameter, an int type value named size. The constructor

-instantiates the fields numbers and boxes such that the array length is size for both

-calls the loadArray() method

6. Define a method named copyNumbers( ). The method takes no parameter and returns an array integers, a copy of the numbers field. In the method declare and instantiate an array (a local variable) named say, temp, with the same type and to the same length as those of the field. The method runs a for loop to load the temp array such that it assigns each temp entry the corresponding numbers entry index-by-index. The method returns temp.

7. Define a method named copyBoxes( ). This method is entirely analogous to the previous method copyNumbers except in the for loop of we do NOT assign boxes[k] to temp[k], but rather a new Rectangle which is a copy of the boxes[k]. This copy must be instantiated by the copy constructor you added to the Rectangle class in Exercise 1 above. That is, the value assigned to temp[k] will be new Rectangle(boxes[k])

8. Complete the definition of the second accessor method getCopyOfBoxes() mentioned above so as to return copyBoxes( ) rather than boxes itself.
\"Using netbeans java\"

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of the object of that class, even it is against the intentions of the programmer who implemented the class. In order to prevent such an intrusion, accessors of reference type fields should not return the field itself, but rather a copy of it.

1. The ArrayPractice class has two private data fields, both are arrays. Declare the fields as

-- an array to store integer numbers of type int named numbers

-- an array to store Rectangle references (type is Rectangle) named boxes

\"Do not instantiate the fields at the declaration!\"

2. Each field has an associated accessor method as usual

3. There is a second accessor method associated to boxes named as getCopyOfBoxes, see the definition later.

4. Define a method named loadArrays( ). The method is void and takes no parameter. The method assigns the entries of the integer array and those of the Rectangle array such that for each array it

--runs a for loop to the length of the array

--entries of the int array assigned a random integer between 1 and 100

--each entry of the Rectangle array is assigned a new Rectangle object with random integer length and width values. Select the integers again between 1 and 100. Note that the individual Rectangle objects do not have names.

5. The class has a default constructor and an initializer constructor such that it takes one parameter, an int type value named size. The constructor

-instantiates the fields numbers and boxes such that the array length is size for both

-calls the loadArray() method

6. Define a method named copyNumbers( ). The method takes no parameter and returns an array integers, a copy of the numbers field. In the method declare and instantiate an array (a local variable) named say, temp, with the same type and to the same length as those of the field. The method runs a for loop to load the temp array such that it assigns each temp entry the corresponding numbers entry index-by-index. The method returns temp.

7. Define a method named copyBoxes( ). This method is entirely analogous to the previous method copyNumbers except in the for loop of we do NOT assign boxes[k] to temp[k], but rather a new Rectangle which is a copy of the boxes[k]. This copy must be instantiated by the copy constructor you added to the Rectangle class in Exercise 1 above. That is, the value assigned to temp[k] will be new Rectangle(boxes[k])

8. Complete the definition of the second accessor method getCopyOfBoxes() mentioned above so as to return copyBoxes( ) rather than boxes itself.
\"Using netbeans java\"

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of the object of that class, even it is against the intentions of the programmer who implemented the class. In order to prevent such an intrusion, accessors of reference type fields should not return the field itself, but rather a copy of it.

1. The ArrayPractice class has two private data fields, both are arrays. Declare the fields as

-- an array to store integer numbers of type int named numbers

-- an array to store Rectangle references (type is Rectangle) named boxes

\"Do not instantiate the fields at the declaration!\"

2. Each field has an associated accessor method as usual

3. There is a second accessor method associated to boxes named as getCopyOfBoxes, see the definition later.

4. Define a method named loadArrays( ). The method is void and takes no parameter. The method assigns the entries of the integer array and those of the Rectangle array such that for each array it

--runs a for loop to the length of the array

--entries of the int array assigned a random integer between 1 and 100

--each entry of the Rectangle array is assigned a new Rectangle object with random integer length and width values. Select the integers again between 1 and 100. Note that the individual Rectangle objects do not have names.

5. The class has a default constructor and an initializer constructor such that it takes one parameter, an int type value named size. The constructor

-instantiates the fields numbers and boxes such that the array length is size for both

-calls the loadArray() method

6. Define a method named copyNumbers( ). The method takes no parameter and returns an array integers, a copy of the numbers field. In the method declare and instantiate an array (a local variable) named say, temp, with the same type and to the same length as those of the field. The method runs a for loop to load the temp array such that it assigns each temp entry the corresponding numbers entry index-by-index. The method returns temp.

7. Define a method named copyBoxes( ). This method is entirely analogous to the previous method copyNumbers except in the for loop of we do NOT assign boxes[k] to temp[k], but rather a new Rectangle which is a copy of the boxes[k]. This copy must be instantiated by the copy constructor you added to the Rectangle class in Exercise 1 above. That is, the value assigned to temp[k] will be new Rectangle(boxes[k])

8. Complete the definition of the second accessor method getCopyOfBoxes() mentioned above so as to return copyBoxes( ) rather than boxes itself.

Solution

// Array Practice.java

import java.util.Random;


public class ArrayPractice {
   // int array
   private int numbers[];
   // Rectangle array
   private Rectangle boxes[];
  
   //accessor method to get the numbers
   public int[] getNumbers() {
       return numbers;
   }

   //accessor method to get the boxes
   public Rectangle[] getBoxes() {
       return boxes;
   }
  
   //load Arrays method
   public void loadArrays(){
       for(int i=0;i<numbers.length;i++){
           //generating random numbers
           Random r = new Random();
           int rand=r.nextInt((100 - 1) + 1) + 1;
           numbers[i]=rand;
          
           int len= r.nextInt((100 - 1) + 1) + 1;
           int width=r.nextInt((100 - 1) + 1) + 1;
          
           boxes[i]=new Rectangle(len, width);
          
       }
   }
   //default constructor
   public ArrayPractice() {
       // TODO Auto-generated constructor stub
   }
  
   //initializer constructor to assign the size of the numbers and boxes
   public ArrayPractice(int size){
       numbers= new int[size];
       boxes= new Rectangle[size];
       loadArrays();
   }
  
   // copy integers to an temp array index by index
   public int [] copyNumbers(){
       int [] temp= new int[numbers.length];
       for(int i=0;i<numbers.length;i++){
           temp[i]=numbers[i];
       }
       return temp;
      
   }
   // copy Rectangle not by index by index. instead creating a new rectangle object
   public Rectangle[] copyBoxes(){
       Rectangle[] temp= new Rectangle[boxes.length];
       for(int i=0;i<boxes.length;i++){
           temp[i]= new Rectangle(boxes[i].getLength(), boxes[i].getWidth());
       }
       return temp;
      
   }
  
   public Rectangle[] getCopyOfBoxes(){
       return copyBoxes();
   }


   /**
   * @param args
   */
   // test main program to drive the above example
   public static void main(String[] args) {
       // to initialzing the size of int and rectangle
       ArrayPractice a= new ArrayPractice(3);
       a.getCopyOfBoxes();
      
   }

}

---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------

// Rectangle.java
public class Rectangle {
  
   private int length;
   private int width;
  
   //default constructor
   public Rectangle() {
       // TODO Auto-generated constructor stub
   }
  
   //constructor to initialise the length and width
   public Rectangle(int length, int width){
       this.length=length;
       this.width=width;
   }
  
   public int getLength() {
       return length;
   }

   public void setLength(int length) {
       this.length = length;
   }

   public int getWidth() {
       return width;
   }

   public void setWidth(int width) {
       this.width = width;
   }

}

--------------------------------------------------
Steps to execute
1) copy the ArrayPractice.java file to the net beans
2) copy the Rectangle.java file to the net beans
3) execute the ArrayPractice.java


Note: Please feel free to ask question. God bless you!

 \
 \
 \
 \
 \
 \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site