We're now going to start the next lab on creating objects. Now, in this lab we're going to be creating a new class to use what we put into our Box.java. We're going to create a test class. We're going to then instantiate objects using instructors, and we're going to use our get and set methods, the other business methods we put into a Box.java, and then we'll fix up the Box.java class, or the Box class in Box.java. First things first, let's go ahead and create our new class named Box.driver. So there's Box.driver, and this one we do want to have a main method. We're going to create two box objects named box1 and box2. Let's get to it. First exercise, I'll go ahead and just put that in here. The first part is creating Box and we shall call it box1. It's going to be a new instance of Box with five. Whoops. Sorry, I didn't mean to click there. Five.six.seven as my parameters, and Box, box2, it's going to be a new Box. Were just taking one parameter because that's going to be a cube. Now these are not being used yet. That's fine. Let's go ahead and use them. The system out print line, box1. Length is plus box1 dot. I want to use my get length. You'll notice Eclipse allowed me to finish that without me having to put anything in. I want to have length, width, and height for box1 and box2. Length, width and height. So width, height. I want to have that for box one and box two. So that should return my values successfully, and that would be exercise one. If I go ahead and run it, let's actually see if it works. Looks like it. By length, width, height 5, 6, 7 and length, width, height, all 10. Perfect, just where I wanted. So now let's do exercise two and then exercise two, we're going to go ahead and call our setters and getters. So box1, and where's my set? Here it is. So set length, set height, set width. I'm going to set that to be three, and box1, set width to four, and box1 set height to five. Having done that, now I'm going to go ahead and just do a system out. Print on. Say box box1, length equals plus box1 dot get length. Plus width equals plus. We're going to say, width equals plus box1, dot get width plus height equals plus box1 dot get height, and I'm going to use the format capability. Just to make sure that's, well, a better format. Now, I've done that print statement. Let's do the next one. System.out.print line. The volume of box 1, or called it box 1. Volume of box box 1. My name is not wonderful, but that's okay. Is going box 1. and getVolume and surface area. I also want to use that print box mechanism, so box1.printBox. Let's say we go ahead and run that just to start. That looks pretty good, box length, width, and height, 3, 4 and 5 because we set it to that for exercise 2. I should make it clear that is a separate exercise, and volume 60, which is 3 times 4 times 5. Surface area looks right, and again, length, width, height being 3, 4, 5, volume 60 surface area. All the things match. Everything works the way it's supposed to. All right. Tested some of the business methods Now, we're up to step 7, having done step 6 and executed the code, make sure everything's working correctly. Now we're going to change the length to minus five and execute that print box method again. Box 1 and set length or setting it to minus five. Which should throw an error, because we don't want it to be minus five. When we do our printBox, I'm just going to copy and paste that. That should throw something for us. Setting the width, box1, setWidth minus 10. Box1, setHeight to zero. Now these are not valid, so our print box should indicate that. If I go ahead and run this, I get that box, it contains invalid attributes twice, which is what I expected from those two print boxes, here and here. All right. Good. Now, we don't want to set attributes to invalid values, so now we're going to go ahead and recode box, so that it won't accept values that are less than or equal to zero. If we try to put in a value that's less than or equal to zero, we want to get an error message saying that the length must be greater than zero, where the height must be greater than zero, or whatever we're trying to set. Now we go back to Box.java. We're going to fix this up so that we're only going to allow values that are going to be at least greater than zero. We don't have to worry about the gets. The getters or just returning what it is, we need to worry about the setters, so where I have here setLength, I want to only set that if length is greater than zero. If length is greater than zero, then I'm fine, else I want to do a system out println. Length must be greater than zero. Okay? All right. There we go. That's better. Okay. That looks good and having done that, I now want to do the same thing for setting the width. Okay. So width is greater than zero, then I'm good, and I want to do the same thing for height. If I save that and I rerun this, instead of seeing this, what am I going to see now? Not what I was expecting. We set width and set height. Now there it is. Width must be greater than zero, height must be greater than zero. It is what I was expecting. Excellent. There was "the length must be greater than zero." Very good. Okay? But now I have to go ahead and check to see if there's more to do. Yes. Step nine was fine. Step ten is the challenge exercise and that is, to make sure that the new boxes can only be created with valid attributes. Okay? You would use a default value of one if the constructor detects an invalid parameter. Let's go back to our Box.java, and we'll initialize these two be one. In our constructor, and we really don't need that super but it's a constructor, they're all super. I don't want to set length, width, and height, unless they're non-negative which means positive. Really, got to be greater than zero. So if length is greater than zero, then I can set this length to length. If it's not, I don't want to. If width is greater than zero, if height is greater than zero, okay. Now if they're not greater than zero, then they're going to have the default value of one because that's what I just did. Let's see what happens if I rerun this. No problems. But, let me go ahead and add a test case for my challenge exercise. So let me do a Box box3 equals new, Box of 7, minus 4, and zero, and box3.print box. Save and run, all right. So the width and height are both set to one because that's minus 4 and that's zero, and the constructor won't allow the negative values. Folks, that [inaudible] off this exercise.