Things are going well in the Duke house these days. We had a wonderful holiday season and are now settling down to teaching you more Java. We have come a long ways but you ain’t seen nothing yet! This chapter is so very cool. I promise that ArrayLists are the best thing since sliced bread!
Now --- on to our lesson! Download our jar file for lesson 4 from the link below:
First – I have decided not to yet post my real cool answer to RoboDuke’s Extra Strength Challenge from the last lesson yet since I have not heard back from enough of you JavaDudes out there with your attempt. To remind you, we were selling robots so that they could take over the world and you were supposed to send me your version of the solution. I will be waiting to see what you can do to help me put a Robot in every household in the world!
Let’s look at the other programs you were to write in lesson 4. Here they are.
Lesson 4 #1. Now It’s Your Turn
Write a program used to determine the smartest robot. Input from the keyboard will be the robot’s name and the robot’s IQ score. You do not know how many robots that the user will enter from the keyboard (this should clue you in on what type of loop to use). The loop ends when the user types in END for the robot’s name. After all robots are entered, have the program display the name and IQ for the robot with the highest IQ score.
Using the concepts that we learned in lesson 4, we really need a plan and some prep code. First we recognize that we will need a SmartRobot class as a blueprint with instance variables of his name and his IQ score. You should be getting good at creating these (including allowing Eclipse to do the constructors from the fields and to write all of the getters and setters for you). Check your answer against mine.
Now for the driver program. I often write the prep code as comments in the main class---then start filling in the code afterwards. So look at the comments in SmartRobotDriverPlan.java. Spend a significant amount of time making plans like this and you will save time in the end with your coding.
Now that I have a plan, I will fill in the details (See SmartRobotDriver.java). Note that once I started, I had to make some decisions on business logic and program requirements. But it still took less time than if I had not done the “thinking” and prep code first.
Lesson 4 #2 Now It’s Your Turn
Rewrite the Ex7Switch2.java program to use if/else constructs instead of the switch construct. Get the month and year from a Keyboard entry and add a loop that will do it more than once (you can choose what the user will enter to end the loop).
Look at Ex7Switch2IfElse.java. To change a switch over to an if/else, you just need to make each case one of the “ifs”. If more than one case has the same code, you can use a logical OR to combine these. See my code for more details.
So – how are you doing on this material? Let Diane (email@example.com) know what she and I can do to help you JavaDudes out there!!!
On to Chapter 6 and using the Java Libraries. Now THIS IS COOL!!! No one is going to pay you $100/hour to reinvent the wheel. If you need a Java class, first look to see if Sun Microsystems might have written one for you! If not, maybe there is one you can use in the literature (google is a wonderful thing!!). I guarantee someone has written just about every type of application in Java. So find one to start with ---and alter it to fit your needs. Also – Sun provides us some very high level classes to use so that we do not have to do all the work. Get used to reading the APIs and you will conquer your programming projects much faster.
First look at pages 126-130 to find out the cliff-hanger problem we were left with in the previous chapter. Although arrays are a wonderful collection, they are not the answer to all problems. Looking at these pages, it seems like we will need quite a bit of code to fix the problem. OR---look for a better way to store the data. (Robots, being inherently lazy definitely like that second answer better --- think smart to save time!!).
Enter the world of ArrayLists!!!!!!!!!!!! A class in Java (I know that since both names start with a capital letter) that will solve our problem. And the real cool thing about it is that since ArrayLists (unlike arrays) are a Java class, they come with their own predefined set of methods that can be used to solve all kinds of problems.
But how do we find these methods and learn to use them (I am so glad you asked the great Robot in the sky). First – remember that to use a method for a given class, I only need to know three things a) the method name (duhh!) b) the parameter list c) the return type. It is also good to know if it is private or public --- and if it is static or not. But if I did know those three things, us robots could have a really cool time using methods that we have never seen before!! That would allow us to look real smart and impress our robot friends!
The information that we need about methods are keep in the Java APIs (application programming interfaces) in HTML files. AND they all look the same (even the ones that we will later generate for the classes that we write). So if we could learn how to read these, we would be able to write programs a lot faster and spend our free time on the beaches of California (but be careful --- robots need lots of suntan lotion to keep their beautiful skin).
So –someone told me about the cool ArrayList class. Where do I find the APIs and how do I read them? And while we are at it, why are ArrayLists better than arrays in some cases? Gee – I am glad you asked. Go into the kitchen right now. Make some popcorn. Get a coke. We are ready for some movies!!!! There are three packages in your jar file that go with the movies so you can play with the code after you are done stuffing yourself with the popcorn (remember the butter!). The first is named arrays and is a review of the use of arrays. Watch the movie at http://www.vw.vccs.edu/bussci/Wolff/120/arrays/arrays.html. Now look at the package named classArrayList and watch the movie at http://www.vw.vccs.edu/bussci/Wolff/120/arrayLists/arrayLists.html. And as a hint for the Extra Strength Challenge from last lesson if you have not yet finished it (and why not??) look at the package named arraylistasfield.
Read pages 132-137 in your book to solidify the ideas relating to the differences between arrays and ArrayLists. Now let’s solve the DotCom class problem from the text. Your book does a great job of explaining how to fix the code and walks you through the changes. Read and carefully follow (you should be typing the code as you read!!!) page 138-153. When you are done, compare your code to the three classes in the lesson5 package which is the answer from the text.
So – back to the APIs. Where do I get them? Sun Microsystems keeps the latest versions in HTML format for you. Go to http://java.sun.com/javase/6/docs/api/ to view the ones for the JavaSE 6.0. The sooner you learn to use these, the faster your program development will become! Take that left over popcorn and watch the movie at
For applications such as the programming of ME (RoboDuke), the program writers will often supply you with a set of APIs as well. Go and download the file located at http://www.vw.vccs.edu/bussci/Wolff/robot/apirobot.zip. Unzip this to a folder on your desktop and look at the contents. Double-click on index.html and follow some of the links. For instance, if I open up the Robot class, I see
NOW THAT IS COOL!!!!!!!! These are the APIs for me, RoboDuke the Javarian!!! I am famous!!! (No autographs, please…) Note that the fields (or instance variables), constructors, and methods are all listed. And note that for my methods, I have exactly what I need!!! Remember we said that you only need to know three things a) the method name b) the parameter list c) the return type. And they are all listed here.
Before we get real good at using these, we will need to learn some more Java. For instance, if we look at the API for Humanoid we see
Looks like a Humanoid comes from a Robot (hmmmm – the mental picture I am getting…) so guess we need to know something about inheritance. We will see that in the next chapter. But we are getting there. I am excited!!
We still have a lot to learn to become efficient at using these. These are quite complicated APIs and the relationship between the classes is very precise. Stay tuned…..
Now It’s Your Turn
Let’s see if you can read APIs. Go get the files at http://www.vw.vccs.edu/bussci/Wolff/robot/DayJavaDocs.zip and unzip the files. These are the APIs (when we create them which I did for these, they are sometimes called “Java Docs” for Java documentation). Study these to learn how to use the Day class. I have added this class to the lesson5 package in this lesson. Now see if you can complete the TestDate.java and make it run by completing the comments.
Now It’s Your Turn
Now we will look at the Java StringBuffer class in more detail. Go to the APIs for the StringBuffer class (it is one that Java provides for us in the java.lang package so you can find it at http://java.sun.com/javase/6/docs/api/. See if you can complete the StringBufferTest.java program. The instructions are in the code.
Return to Lesson Plan
Read More In: RS Media
Welcome to the Java Lessons of RoboDuke, the Javarian. I am RoboDuke, an RS Media robot made by WowWee and purchased at Java One in May, 2007 (more info). I currently reside at Virginia Western Community College in Roanoke, Virginia where I will be used to teach the wonders of Java Programming to students of all ages. Come follow along!