Join Peggy Fisher for an in-depth discussion in this video Solution: Simple calculation, part of Learning Java.
(gentle music) - I hope you had fun with that challenge but I also wanna say don't be discouraged if this is your first time and you found it a little difficult. What I'd like to do is show you one approach to solving this. Again, this is my approach and it might look a little different than yours. The first thing that I would do would be to declare my variables. There's quite a few in this program. Let's make them doubles and I'm gonna start by declaring the width for the house so I'm gonna make a w, the length L, and the height, H.
Next, I'm gonna take care of my windows. Again, I use double. We'll call it numWin to represent the number of windows and I need to know the window width and the window height. We need to do the same thing with the doors. Double numDoors, doorWidth, and doorHeight because we don't need paint for the doors or the windows. So, when we calculate our final surface area we're gonna subtract these values. Remember, our goal is to find the total surface area that needs to be painted so I am gonna go ahead and add another variable that will represent the surface area.
We want to make this program reusable so we wanna be able to ask the user for all the values so we can calculate the surface area for many different types of houses. Let's go ahead and add on line 5 our scanner class so that we can ask the user to enter the values. import java.util.Scanner. Okay, now I'm ready to fill in my values. I'm gonna do a System.out.println and I am going to ask the user to "Please enter the width, length, "and height of the house to be painted." Okay, and I'm gonna read those values in.
So, w is equal to in.nextDouble. L is equal to in.nextDouble, and H is equal to in.nextDouble. You can see that I forgot to define my variable in. So, let's go back and do that next. Scanner in equals new Scanner(system.in). This allows me to read data from the user. Okay, next we'll ask the user to enter in the number of windows and the width, and height of a sample window.
We're going to assume that all the windows are standard and have the same size. Okay, we read those values in to our numWin which equals in.nextDouble then we have winWidth and winHeight. We're gonna do the same thing for the doors. And we'll read them in to the door variables. numDoors is equal to in.nextDouble. doorWidth is equal to in.nextDouble.
And doorHeight is equal to in.nextDouble. Okay, we have all the information we need. Now, we need to figure out what the calculation looks like to calculate the total surface area. So, I'm gonna go ahead and start by defining surface area and set it equal to. We wanna first calculate the total area of the house and then we'll subtract out the surface area of the windows and doors. So, I'm gonna use parentheses and I'm gonna take the width of the house times the height of the house times two.
This'll represent the two sides of the house. I wanna add to that the length of the house times the height of the house times two. The times two is because we have a front and back and two sides. Okay, now once I get that calculation done now I wanna subtract the surface area of the windows and doors. So, this is gonna look like this. I take the number of windows times the window width times the window height and I'm going to add to that the number of doors times the door width times the door height.
Now, just a refresher, the order of operations would take care of a lot of this but it's really clear if you use parentheses to indicate that I wanna calculate the total surface area of the house first and then subtract out the surface area of the windows and doors. Now that I've calculated the surface area I wanna print this back out to tell my user so they can figure out how much paint they're going to need. Okay, we're not done yet. One of the most important things in programming is testing the code that you wrote. At this point, I don't have any syntax errors and I hope I don't have any logic errors but let's give it a try and test it out.
Okay, we wanna enter the width, the length, and the height of our house. So, if our house has a width of 20 feet and it has a length of 40 feet, and a height of 20 feet, now I need to provide it with the number of windows. So, let's say we have five windows and each window is 3 feet wide by 4 feet high. And finally, I need to tell it how many doors. One is gonna have a front and back door so we'll do two doors and the doors are 3 feet wide and let's make them 7 feet tall.
The total paintable surface area is 2,298 square feet. Okay, well, again I hope you enjoyed this challenge and were able to come up with a good solution. A copy of my solution is also available in the exercise files.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs