Creating a Hole Object3:38 with Ben Deitch
In this video I create the Hole object and initialize an array of Holes in MainActivity.
Now that I've created both of the layouts, 0:00 I'm going to head back into main activity and see where we stand. 0:03 There's not a lot going on here, 0:07 but one thing that would be nice to have would be an array of 18 holes. 0:08 Before I can do that, it would be nice to have a whole object. 0:14 The whole object is going to have. 0:19 Both the label and the stroke count for that particular hole. 0:21 It's not a complicated object, but it makes things a little easier to see. 0:25 Going to create a new java class, called hole, and it's gonna have two fields. 0:32 The first one is a string for label and 0:40 the second one is an integer for StrokeCount. 0:43 Then I'm gonna create a constructor for our hole, and 0:50 it's going to take in two parameters, one for each of the fields. 0:53 And we'll set the fields to the values we're passing in. 1:08 All right. 1:17 Then I'm just gonna click up here, Alt+Enter, 1:19 create getters and setters, Alt+Enter, create getter and setter. 1:23 And now I have my whole object, looks good. 1:28 Heading back into main activity, I'm going to create the array of 18 holes. 1:32 This is going to be private. 1:40 Array of holes. 1:43 So Hole. 1:44 We'll call it mHoles and 1:46 it's going to equal a new array of holes with 18 entries. 1:51 Each of these entries will start as nothing, so we'll need to initialize them. 1:58 I'm going to do that down here. 2:03 So, for each hole, we wanna give it its label and 2:09 the number of strokes we've taken. 2:13 Keeping in mind that at some point we're probably going to need to use 2:18 a shared preference, or some other way of saving data, 2:21 to keep track of the scores when the activity is destroyed. 2:24 Let's create a variable for that right here. 2:29 We won't set it to anything or use it with MShared preferences or 2:35 anything like that yet. 2:38 But that's coming. 2:40 So with each hole. 2:42 So, four int I equals zero. 2:44 While I is less than mHoles.length. 2:49 Increment I by one. 2:54 We will get access to the hole at that index. 2:57 And we will set it equal to a new hole, and then we'll pass in our label of hole, 3:01 then we need to add the number of the hole which is I plus one. 3:10 Because our index starts at 0, but the holes start at one. 3:17 And then I'm going to add a space and a colon, 3:22 just for good formatting. 3:27 And then we need to say how many strokes we took on that hole. 3:33 Looks good. 3:36
You need to sign up for Treehouse in order to download course files.Sign up