/**
* 3.(10 pts.) Implement class StrangeArray.
* StrangeArray has one field: theArr, an array of ints.
* There are three internal methods:
*setSize() asks the user to enter a size and allocates an int array of that size to theArr
*loadArr() for each element in the array, asks the user to enter an int.
*If the number is negative, ask the user to re-enter.
*Otherwise set the value of that array element to the int.
*printArr() print the contents of the array.
*
*There is one static method: changeArr() takes an integer and returns the integer squared plus 37.
* This method should be public, as everyone will want to use it.
*StrangeArray should have a main method that does the following:
*call setSize() to size theArr
*call loadArr() to get values into theArr
*print the contents of theArr with printArr()
*changeArr() every element of theArr
*print the contents again
*
*Here's the output from a sample run:
*$ java StrangeArray
*(setSize) Enter the size of the array: 5
*(loadArr) Enter element 0: 4
*(loadArr) Enter element 1: 27
*(loadArr) Enter element 2: -3
*(loadArr) Please enter a positive number
*(loadArr) Enter element 2: 12
*(loadArr) Enter element 3: 94
*(loadArr) Enter element 4: 17
*(printArr) The array now contains:
*(printArr) [0]: 4
*(printArr) [1]: 27
*(printArr) [2]: 12
*(printArr) [3]: 94
*(printArr) [4]: 17
*(main) changeArr...
*(printArr) The array now contains:
*(printArr) [0]: 53
*(printArr) [1]: 766
*(printArr) [2]: 181
*(printArr) [3]: 8873
*(printArr) [4]: 326
*$
*
* @see
* <A xhref="/Web">
* Java source code for this class. </A>
* @author Ahmet Faruk Bişkinler
* <A xhref="mailto:
Bu e-Posta adresi istek dışı postalardan korunmaktadır, görüntülüyebilmek için JavaScript etkinleştirilmelidir
"> mail to Author</A>
* @version November 29,2005
*/
import java.util.*;
public class StrangeArray {
public static int theArr[];
public static void main
(String[] args
) { System.
out.
println("Enter the size of the array: "); int size = in.nextInt();
// the method to be implemented
setSize(size);
loadArr();
printArr();
changeArr();
printArr();
}
/**
* Asks the user to enter a size and allocates an int array of that size to theArr
* @param size size of theArr
*/
public static void setSize(int size){
theArr = new int[size];
}
/**
* for each element in the array, asks the user to enter an int.
* If the number is negative, ask the user to re-enter.
* Otherwise set the value of that array element to the int.
* */
public static void loadArr(){
for (int i=0 ; i<theArr.length ; i++ ){
System.
out.
printf("Enter element %d: \n",i
); theArr[i] = in.nextInt();
while (theArr[i] < 0 ){
System.
out.
println("Please enter a positive number :"); System.
out.
printf("Enter element %d: \n",i
); theArr[i] = in.nextInt();
}
}
}/**
*print the contents of the array.
*/
public static void printArr(){
System.
out.
printf("\nThe array now contains: \n\n"); for (int i=0 ; i < theArr.length ; i++ ){
System.
out.
printf("The %d. Element is %d \n",i+
1,theArr
[i
]); }
}
/**
* Takes an integer and returns the integer squared plus 37.
* This method should be public, as everyone will want to use it.
* @return the integer squared plus 37.
* */
public static int changeArr(){
for (int i=0 ; i < theArr.length ; i++ ){
theArr[i] =theArr[i] * theArr[i] + 37;
}
return 0;
}
}