Download presentation
Presentation is loading. Please wait.
Published byWinifred Page Modified over 9 years ago
1
Chapter 7 Arrays
2
A 12-element array
3
Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array –We can create arrays of objects too String b[] = new String[ 100 ];
4
Problem Develop a test case that create an array of 10 integers and print out the initial values of this array’s elements
5
Solution 1: Using the Conventional for Loop
6
Solution 2: Using the for-each Loop
7
Using an Array Initializer int n[] = { 10, 20, 30, 40, 50 }; –Creates a five-element array –Index values of 0, 1, 2, 3, 4
8
Problem Develop a test case that create an array of the following 10 integers 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 Print out the values of this array’s elements
9
Solution 1: Using the Conventional for Loop
10
Exercise Use the for-each loop to rewrite the previous test case
12
Problem Develop a test case that create an array of 10 even integers, starting from 2 Print out the values of this array’s elements
14
Problem Develop a test case that create an array of the following 10 integers 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Calculate the sum of all elements in the array
15
Solution 1
17
Question Can we make the sum() method to be the static one?
18
Solution 2: Using varargs
20
Problem Using bar charts to display array data (grade distribution) graphically Bar labels: “00-09”, “10-19”, …, “90-99”, and “100” See an example in the next slide
21
Example Input: Grade distribution data: 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 Output: The bar chart
24
Exercise Compare the following solution with the previous one
25
Using the Elements of an Array as Counters
26
Revisiting the Randomness Verification Problem Problem statement: To show that the numbers produced by nextInt() occur with approximately equal likelihood, develop an object that rolls 6000 times of a die. Thus, each integer from 1 to 6 should appear approximately 1000 times
28
Solution 1: Without Using Array
29
Solution 2: Using Arrays
32
Question Do a comparison between the Solution 1 and Solution 2
33
Using Arrays to Analyze Survey Results
34
Problem Develop a method that receives an array of responses about the rating of food quality and returns an array of counts corresponding to each rating 1-10 Rating scale: 1 mean awful, 10 means excellent See an example in the next slide
35
Example Input: 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 Expected output: 2, 2, 2, 2, 5, 11, 5, 7, 1, 3
38
Case Study: Card Shuffling and Dealing Simulation
39
Sub-Problem 1 A card has a face and a suit –A face can be one of the following 13 values: Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King –A suit can be one of the following 4 values: Hearts, Diamonds, Clubs, and Spades 1.Develop enums for Face and Suit 2.Develop a Card class with the toString() method
44
Sub-Problem 2 Develop the DeckOfCards class by making the following test pass
47
Sub-Problem 3 Develop a method that can shuffle a deck of cards by making the following test pass
49
Sub-Problem 4 Develop a method that can deal one card –Throw an NoSuchElementException if there is no card left
52
Case Study: Class GradeBook Using an Array/List to Store Grades
53
Problem Statement A grade book has a course name and a list of grades Develop a grade book class that can perform the following tasks 1.Constructing a grade book object with a given course name and a given array of grades 2.Returning the minimum grade 3.Returning the maximum grade 4.Returning the average grade 5.Drawing the bar chart of grades
54
Task 1: Constructor
55
Example Course name: CS101 Introduction to Java Programming Grades: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87
58
Task 2: Minimum Grade
60
Q: How can we use Collections.min( ) ? A: Use java.util.List instead of array
63
Task 3: Maximum Grade
66
Task 4: Average Grade
67
Example Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Output: 84.9
70
Task 5: Bar Chart
71
Example Input: 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 Output:
74
Multidimensional Arrays
75
Two-dimensional array with three rows and four columns
76
Multidimensional Arrays (Cont.) Arrays of one-dimensional array –Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; –1 and 2 initialize b[0][0] and b[0][1] –3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; –row 0 contains elements 1 and 2 –row 1 contains elements 3, 4 and 5
77
Multidimensional Arrays (Cont.) Two-dimensional arrays with rows of different lengths –Lengths of rows in array are not required to be the same E.g., int b[][] = { { 1, 2 }, { 3, 4, 5 } };
78
Multidimensional Arrays (Cont.) Creating two-dimensional arrays with array- creation expressions –Can be created dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row 1
79
Outline Use nested array initializers to initialize array1 Use nested array initializers of different lengths to initialize array2
80
Outline array[row].length returns number of columns associated with row subscript Use double-bracket notation to access two-dimensional array values
81
7.9 Multidimensional Arrays (Cont.) Common multidimensional-array manipulations performed with for statements –Many common array manipulations use for statements E.g., for ( int column = 0; column < a[ 2 ].length; column++ ) a[ 2 ][ column ] = 0;
82
Case Study: Class GradeBook Using a Two-Dimensional Array
83
Problem Statement A grade book has a course name and a list of grades. Each student has more than one grades Develop a grade book class that can perform the following tasks 1.Constructing a grade book object with a given course name and a given 2D-array of grades 2.Returning the minimum grade 3.Returning the maximum grade 4.Returning a list of average grades 5.Drawing the bar chart of grades
84
Examples Grades Test1Test2Test3 Student 1: 87 96 70 Student 2: 68 87 90 Student 3: 94 100 90 Student 4: 100 81 82 Student 5: 83 65 85 Student 6: 78 87 65 Student 7: 85 75 83 Student 8: 91 94 100 Student 9: 76 72 84 Student 10: 87 93 73
85
Task 1: Constructor
88
Task 2: Minimum Grade
91
Task 3: Maximum Grade
94
Task 4: Average Grades
97
Task 4: Drawing Bar Chart
101
Case Study: The Sudoku Puzzle
102
What You Have Learnt? To be filled
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.