Logout

5.1.5 - JSR PART 2 - FULL EXAMPLES

Construct algorithms using two-dimensional arrays.

Teaching Note:

Sample Question:

From Sample Paper 1 - 2014:

JSR Notes:

Part of the above past paper question:

```1    public class LakeTemperatures {
2
3        static OurList<Integer> temperaturesList = new OurList<Integer>();
4        static int [][] temperaturesArray = new int [24][7];
5
6        public static void main(String[] args) {
7            populateList();
8            putListTo2DTable();
9            print2DTable();
10       }
11
12       public static void print2DTable(){
13           for(int row = 0; row < temperaturesArray.length; row++){
14               for(int col = 0; col < temperaturesArray[0].length; col++) {
15                   System.out.print(temperaturesArray[row][col] + " ");
16               }
17               System.out.println();
18           }
19       }
20
21       public static void putListTo2DTable(){
22           temperaturesList.resetNext();
23           for(int row = 0; row < temperaturesArray.length; row++){
24               for(int col = 0; col < temperaturesArray[0].length; col++){
25                   if(temperaturesList.hasNext()) {
26                       temperaturesArray[row][col] = temperaturesList.getNext();
27                   }
28               }
29           }
30       }
31
32       public static void populateList(){
33           temperaturesList.resetNext();
34           for(int i = 0; i < 168; i++){
35               temperaturesList.addItem((int)(Math.random() * 8 + 22));
36           }
37       }
38   }
39   ```

Below, a preliminary 2D array exploratory class, including some blue commented general code (commented out) at the top.

```1    public class TwoDArrays {
2        public static void main(String[] args) {
3            /*boolean bb = false;
4            if(bb){
5                System.out.println("sfasa");
6            }
7            int [][] twoDarray = new int[2][4];
8            int [] a = {23, 43, 7733, 342};
9            int [] b = {234, 545, 664, 233};
10           twoDarray[0] = a;
11           twoDarray[1] = b;
12
13           System.out.println(twoDarray[1][3]);
14
15            */
16           int [][] relayArr = relayRaceTableCreation();
17           printOutRelayTeamSeedTimes(relayArr);
18
19       }
20
21       public static int [][] relayRaceTableCreation(){
22           int [][] relayTeamsTimes = new int[10][4];
23           for(int row = 0; row < relayTeamsTimes.length; row++) {
24               for (int col = 0; col < relayTeamsTimes[0].length; col++) {
25                   relayTeamsTimes[row][col] = (int)(Math.random() * 5 + 10);
26               }
27           }
28           return relayTeamsTimes;
29       }
30
31       public static void printOutRelayTeamSeedTimes(int [][] arr){
32           System.out.println("This prints out a table of ten relay teams' average times.");
33           System.out.println("Along with which team has the lower average time.");
34           int lowestTime = 9999999;
35           for(int row = 0; row < arr.length; row++){
36               int total = 0;
37               for(int col = 0; col < arr[0].length; col++){
38                   System.out.print(arr[row][col] + " ");
39                   total += arr[row][col];
40               }
41               if(total < lowestTime){
42                   lowestTime = total;
43               }
44               System.out.println("Team " + row + 1 + " has a total average time of " + lowestTime);
45               System.out.println();
46           }
47           System.out.println("So the lowsetTime, as you can see is " + lowestTime);
48       }
49
50   }
51   ```

Below, a straight forward but very full example, and including lists:

```1    public class IBScores {
2
3        static int[][] ibScores = new int[1200][6];
4        static OurList<Integer> highestScores = new OurList<Integer>();
5        static int theHighestScore = -999;
6
7        public static void main(String[] args) {
8            makeIbScoresArray();
9            printOutScoresTable();
10           //highestSubject();
11           theHighestScore = highestScore();
12           System.out.println("The highest score anyone got " + theHighestScore);
13           OurList<Integer> studentsWithHighestScoreList = findHighestStudents();
14
15           studentsWithHighestScoreList.resetNext();
16           if(!studentsWithHighestScoreList.ourIsEmpty()){
17               System.out.println("Here are all the indices which have a top score: ");
18               while(studentsWithHighestScoreList.hasNext()){
19                   System.out.print(studentsWithHighestScoreList.getNext() + "  ");
20               }
21           }
22       }
23
24       public static OurList<Integer> findHighestStudents(){
25
26           for(int row = 0; row < ibScores.length; row++) {
27               int total = 0;
28               for (int col = 0; col < ibScores[0].length; col++) {
29                   total += ibScores[row][col];
30               }
31               if(total == theHighestScore){
33               }
34           }
35           return highestScores;
36       }
37
38       public static int highestScore(){
39           int highestTotal = 0;
40           int highestTotalRowNumber = 0;
41           for(int row = 0; row < ibScores.length; row++){
42               int total = 0;
43               for(int col = 0; col < ibScores[0].length; col++){
44                   total += ibScores[row][col];
45               }
46               if(total > highestTotal){
47                   highestTotal = total;
48                   highestTotalRowNumber = row;
49               }
50           }
51           //System.out.println("The highest IB score total was " + highestTotal);
52           //System.out.println("And it was found at row number " + highestTotalRowNumber);
53           return highestTotal;
54       }
55
56
57       public static void printOutScoresTable(){
58           for(int row = 0; row < ibScores.length; row++){
59               for(int col = 0; col < ibScores[row].length; col++){
60                   System.out.print(ibScores[row][col] + "     ");
61               }
62               System.out.println();
63           }
64       }
65
66       public static void makeIbScoresArray(){
67           for(int row = 0; row < ibScores.length; row++){
68               for(int col = 0; col < ibScores[0].length; col++){
69                   ibScores[row][col] = (int)(Math.random() * 6 + 2);
70               }
71           }
72       }
73   }
74   ```