Skip to content

Ahmet Faruk Bişkinler



CENG102Homework3Q3.java PDF Yazdır e-Posta
(0 votes, average 0 out of 5)
Lessons
Administrator tarafından yazıldı.   
Pazar, 03 Aralık 2006 22:24
Etiketler:

 

CENG 102 Assignment #3

 

Assigned: 22/11/05, Tuesday

Due: 30/11/05, Wednesday at 14:00

 

Homeworks must be delivered at the deadline date and hour.

 

 

(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:

 

text
  1.  
  2. $ java StrangeArray
  3. (setSize) Enter the size of the array: 5
  4. (loadArr) Enter element 0: 4
  5. (loadArr) Enter element 1: 27
  6. (loadArr) Enter element 2: -3
  7. (loadArr) Please enter a positive number
  8. (loadArr) Enter element 2: 12
  9. (loadArr) Enter element 3: 94
  10. (loadArr) Enter element 4: 17
  11. (printArr) The array now contains:
  12. (printArr) [0]: 4
  13. (printArr) [1]: 27
  14. (printArr) [2]: 12
  15. (printArr) [3]: 94
  16. (printArr) [4]: 17
  17. (main) changeArr...
  18. (printArr) The array now contains:
  19. (printArr) [0]: 53
  20. (printArr) [1]: 766
  21. (printArr) [2]: 181
  22. (printArr) [3]: 8873
  23. (printArr) [4]: 326
  24.  
http://www.biskinler.com
CENG102Homework3Q3.java
  1.  
  2.  
  3. /**
  4.  * 3.(10 pts.) Implement class StrangeArray.
  5.  * StrangeArray has one field: theArr, an array of ints.
  6.  * There are three internal methods:
  7.  *setSize() asks the user to enter a size and allocates an int array of that size to theArr
  8.  *loadArr() for each element in the array, asks the user to enter an int.
  9.  *If the number is negative, ask the user to re-enter.
  10.  *Otherwise set the value of that array element to the int.
  11.  *printArr() print the contents of the array.
  12.  *
  13.  *There is one static method: changeArr() takes an integer and returns the integer squared plus 37.
  14.  * This method should be public, as everyone will want to use it.
  15.  *StrangeArray should have a main method that does the following:
  16.  *call setSize() to size theArr
  17.  *call loadArr() to get values into theArr
  18.  *print the contents of theArr with printArr()
  19.  *changeArr() every element of theArr
  20.  *print the contents again
  21.  *
  22.  *Here's the output from a sample run:
  23.  *$ java StrangeArray
  24.  *(setSize) Enter the size of the array: 5
  25.  *(loadArr) Enter element 0: 4
  26.  *(loadArr) Enter element 1: 27
  27.  *(loadArr) Enter element 2: -3
  28.  *(loadArr) Please enter a positive number
  29.  *(loadArr) Enter element 2: 12
  30.  *(loadArr) Enter element 3: 94
  31.  *(loadArr) Enter element 4: 17
  32.  *(printArr) The array now contains:
  33.  *(printArr) [0]: 4
  34.  *(printArr) [1]: 27
  35.  *(printArr) [2]: 12
  36.  *(printArr) [3]: 94
  37.  *(printArr) [4]: 17
  38.  *(main) changeArr...
  39.  *(printArr) The array now contains:
  40.  *(printArr) [0]: 53
  41.  *(printArr) [1]: 766
  42.  *(printArr) [2]: 181
  43.  *(printArr) [3]: 8873
  44.  *(printArr) [4]: 326
  45.  *$
  46.  *
  47.  * @see
  48.  * <A xhref="/Web">
  49.  * Java source code for this class. </A>
  50.  * @author Ahmet Faruk Bişkinler
  51.  * <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>
  52.  * @version November 29,2005
  53.  */
  54.  
  55.  
  56. import java.util.*;
  57. public class StrangeArray {
  58.  
  59. public static int theArr[];
  60. Scanner in = new Scanner(System.in);
  61.  
  62. public static void main(String[] args) {
  63. System.out.println("Enter the size of the array: ");
  64. Scanner in = new Scanner(System.in);
  65. int size = in.nextInt();
  66. // the method to be implemented
  67. setSize(size);
  68. loadArr();
  69. printArr();
  70. changeArr();
  71. printArr();
  72. }
  73. /**
  74.   * Asks the user to enter a size and allocates an int array of that size to theArr
  75.   * @param size size of theArr
  76.   */
  77. public static void setSize(int size){
  78. theArr = new int[size];
  79. }
  80. /**
  81.   * for each element in the array, asks the user to enter an int.
  82.   * If the number is negative, ask the user to re-enter.
  83.   * Otherwise set the value of that array element to the int.
  84.   * */
  85. public static void loadArr(){
  86. Scanner in = new Scanner(System.in);
  87.  
  88. for (int i=0 ; i<theArr.length ; i++ ){
  89. System.out.printf("Enter element %d: \n",i);
  90. theArr[i] = in.nextInt();
  91. while (theArr[i] < 0 ){
  92. System.out.println("Please enter a positive number :");
  93. System.out.printf("Enter element %d: \n",i);
  94. theArr[i] = in.nextInt();
  95. }
  96. }
  97. }/**
  98.   *print the contents of the array.
  99.   */
  100. public static void printArr(){
  101. System.out.printf("\nThe array now contains: \n\n");
  102. for (int i=0 ; i < theArr.length ; i++ ){
  103. System.out.printf("The %d. Element is %d \n",i+1,theArr[i]);
  104. }
  105. }
  106. /**
  107.   * Takes an integer and returns the integer squared plus 37.
  108.   * This method should be public, as everyone will want to use it.
  109.   * @return the integer squared plus 37.
  110.   * */
  111. public static int changeArr(){
  112. for (int i=0 ; i < theArr.length ; i++ ){
  113. theArr[i] =theArr[i] * theArr[i] + 37;
  114. }
  115. return 0;
  116. }
  117. }
  118.  
  119.  
http://www.biskinler.com
Etiketler:
Son Güncelleme: Cumartesi, 27 Şubat 2010 12:13
 

Yorum ekle


Güvenlik kodu
Yenile