Our variables aren’t just simple numbers anymore. We’ve got complex objects like String and Scanner at our disposal. Today we’ll give you some more practice at using objects that others have made available.
Be sure to get your checkpoints from lab 2 checked off in the first 20 minutes of this lab. They should already be working and ready for approval.
The preassignment is due before Friday. Late work is not graded.
Checkpoint #1: please take this short self-check multiple choice quiz to see how your grasp of the Java language is coming along. Your answers will not be graded, but your completion of the quiz counts as this checkpoint. You do not need to let your instructor or TA know you’ve completed it.
When learning any language, you’ve got to work with it enough to see patterns before generating new ideas comes very easily. With objects, we make them do something using code like this:
thing.action(outside information needed for action);
In object-oriented programming, our instructions read a bit like English sentences. Our object is the subject, the method is the verb, and the arguments modify the action (adverbs? direct objects?). A sentence like “Barbara eats quickly” might translate into Java like this:
Human barbara = new Human(); barbara.eat(9); // on a scale of 1-10
Some methods give back information that we can print, store in a variable, or use in an expression, but the pattern is more or less the same:
String nickname = barbara.getNickname();
Follow the pattern and you will go far.
Create a package named lab3 in your cs145 project.
Complete three of the following five tasks. We suggest you create separate classes and main methods for each. You’ll need to browse through the String class documentation to find methods suitable to your needs. To see the documentation, declare a String variable, place your cursor in the type, and hit Shift-F2.
Checkpoint #2. Hit Control-Shift-F in Eclipse to format your code. Show your instructor or TA your three solutions.
The Random class is a source of random numbers. Such numbers have a lot of use, not the least of which is in games. You make a random number generator like you make any object:
Random generator = new Random();
Check out the documentation for Random (Shift-F2 with the cursor inside “Random”) and see how it can help you solve this problem:
Checkpoint #3. Hit Control-Shift-F in Eclipse to format your code. Show your TA or instructor your code.