
<p>C5: Internet Programming J013 - MarkList(1)</p><p>Topics - Arrays. 12 public class MarkList 13 { Introduction 14 int [] mark; 15 Why can't the constructor just say A set of students' marks are stored in an array of type int. This example shows 16 public MarkList(int [] mark) how to list all of the elements of the array, find the largest element and the total. 17 { this.mark = mark; 18 this.mark = new int[mark.length]; Instructions 19 for (int i=0; i<mark.length; i++){ 20 this.mark[i] = mark[i]; 1. Create a new project called J013_MarkList(1) 21 }// end for i 22 } // end constructor MarkList() 2. Enter and compile the source code for the MarkList and Test classes. 23 3. Run the main( ) method of the Test class. 24 public void printList() 25 { Questions 26 System.out.println("The list of elements in the array are:"); 1. What function is carried out by the MarkList constructor? 27 for (int i=0; i<mark.length; i++){ 28 System.out.print("mark["+i+"] = "); 2. Why do the for loops in MarkList lines 8 and 16 start with int i=0 but the 29 System.out.println(mark[i]); for loop in line 29 starts with int i=1 ? 30 } //end for 31 } //end method printList() 3. Which line silently invokes the constructor for int arrays? 32 Test 33 public void printMax() 4. Change the array initialisation ( , line 4) so it contains the additional values 34 { 10, 42 and 20. 35 int maxValue, maxPosition; 5. Run with this new set of values. What is wrong with the results for 36 maxValue = mark[0]; main() 37 maxPosition = 0; the maximum value? 38 System.out.println("Finding the largest element in the array"); 39 Exercises 40 for (int i=1; i<mark.length; i++){ 1. Change the program so that it handles multiple occurrences of the maximum. 41 if (maxValue < mark[i]) { 42 maxValue = mark[i]; maxPosition=i; 2 Change the program so it also prints out the minimum value or values. 43 }// end if 44 } //end for 3. Change the program so that it calculates the average mark. 45 System.out.println("Max value = "+maxValue+" at position = 4. Make it print out all the marks greater than the average mark. "+maxPosition); 46 } // end method printMax() 5. Make it prints out the largest mark that is just less than the average. 47 48 public void printTotal() 1 public class Test 49 { 2 { 50 System.out.println("Finding the total mark"); 3 public static void main(String args []){ 51 int total, count; 4 int mark [] = {17,21,18,42,16,19,21}; // set up an array 52 total = 0; // initialise running total MarkList aMarkList; 53 count = 0; // initialise count 5 aMarkList = new MarkList(mark); 54 for (int i=0; i<mark.length; i++){ 6 aMarkList.printList(); 55 total = total + mark[i]; 7 aMarkList.printMax(); 56 count++; 8 aMarkList.printTotal(); 57 } // end for 9 }// end method main() 58 System.out.println("The total of "+count+" values is "+total); 10 }// end class Test 59 } // end method printTotal() 11 60 © Faculty of Computing, Information and English Page 1 of 1 Version 1.1, July 2002 087016fe37746053e6b1a548e6a85386.doc C5: Internet Programming J013 - MarkList(1)</p><p>61 }// end class MarkList</p><p>© Faculty of Computing, Information and English Page 2 of 1 Version 1.1, July 2002 087016fe37746053e6b1a548e6a85386.doc</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-