Advertisement
yko0

Method makes nums[i] *=1.1

Nov 27th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. /* Напишите метод, который будет увеличивать заданный элемент массива на 10% */
  2.  
  3. class numsum {
  4.        
  5.     private double nums[];
  6.        
  7.     numsum (int size) {
  8.         nums = new double[size];
  9.         double k = 1.0;
  10.         for (int i=0; i<nums.length; i++) {
  11.             nums[i] = k;
  12.             k += 1.0;
  13.         }
  14.     }
  15.                
  16.     void show () {
  17.         for (int j=0; j<nums.length; j++)
  18.             System.out.print(nums[j] + "   ");
  19.             System.out.println();
  20.     }
  21.        
  22.     void change (int p) {
  23.         try {
  24.             nums[p] *= 1.1;
  25.         } catch (ArrayIndexOutOfBoundsException e) {
  26.             System.out.println("[change-index is out of array size]");
  27.         }
  28.     }
  29. }
  30. //comment 1 string above+below - would be 1 class instead of 2
  31. class test {
  32.     public static void main(String args[]) {
  33.         numsum ob1 = new numsum(10);
  34.         ob1.show();
  35.         ob1.change(5);
  36.         ob1.show();
  37.         numsum ob2 = new numsum(5);
  38.         ob2.show();
  39.         ob2.change(5); //would lead to exception
  40.         ob2.change(2);
  41.         ob2.show();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement