Roboduke, is there a place where all of your lessons are posted? Thanks
What would Juliet have said if she had been a Java programmer?
Romeo romeo = new Romeo();
And a definition you can use in this lesson:
PROGRAM (pro’-gram) – [n] A magic spell cast over a computer allowing it to turn one’s input into error messages ---- or [vi] to engage in a pastime similar to banging one’s head against the wall, but with fewer opportunities for reward.
Let’s look first at the package named answerslesson2. In Lesson 2, you completed a number of programs (you did complete them, didn’t you??) The first one was Ex4Expressions.java. Look at the solution and see if you have any questions. That one was not too bad! Now, how about GuessingGame.java. More complicated---but if you just follow my directions contained in the comments one line of code at a time, you can do it!! The only real hard part was figuring out how to generate a random number between 1 and 10 since we had not discussed that. We did generate random numbers in PhraseOMatic but not starting at 1. When this occurs, a great approach is to write yourself a little stand alone program (see my TestRandomNumbers.java) and see if you can get the line to work. Play with it, make changes, and test it until it does what you want it to do. Once it does, copy it into your program and it will run! Watch for the Scanner problem as always.
The other program you were to write was the DogDriver.java. Bet you think it isn’t fair that I asked you to write the loops when we have not even discussed them yet. But to become a proficient programmer, you need to be able to look at code that is similar to what you need ---and beg, borrow, steal, and alter it for your needs. Google is one of your very most important tools in writing Java. So – to write the loops needed here, I looked at the print method in the RobotFriendsDriver.java to see how to use for loops with arrays. I used similar code for getting the Dog information and for printing them out. And it worked!!!!
Now on to Chapter 4 and Lesson 3. We will look at methods, method, and more methods. Methods are the only thing there really is in Java!! We know that we have two components for a Java class – instance variables and methods. But the instance variables are private (your book explains this and starts using it on pages 80-81). This means that only the blueprint class can use the instance variables -- called encapsulation (great word!!!). Therefore – it has to be the methods where all the action takes place! (no pun intended!). To become proficient in Java, we need to become proficient in writing methods.
There are two concepts about methods that we need to discuss – method definitions and method calls. The definition defines what the method does and the method call then uses the method.
Go to http://www.vw.vccs.edu/bussci/Wolff/robot/RDMethodNutshell.doc. I know ----- you do not have to tell me ---I made a real cool document that summarizes method use!!! You do not have to thank me ---just send money! Use this as a reference. Study the four ways to use Java methods. Run these samples (they are in your lesson3.methodshandout package). First run First.java – this has a method call from the same class with no return value. Then run Second.java – it has a method call from the same class with a return value. Then run EmpRun.java that calls methods on an Employee object that is in a different class.
Now look at the lesson3.overload package. Note that you can write different versions of the same method as long as the parameters are different (we have already done that with the constructors). Java knows which method to call since the arguments in the method call must match the parameters in the method definition.
Look at the lesson2.passValue package. See also page 77 – 78 in your text. It is critical to realize what Java does with values that are passed as parameters. Look at the comments in the PassValue.java class. From this example you should realize that Java passes primitives by value. This means that if the value is changed in the method call, the actual variable outside of the method is not changed. Java makes a copy of the value for the method to use and changes that only. However, if you pass in an object (such as an instance of the Number class in this example), the address is actually passed in by value. This means that the address cannot change. However, the fields of the object can be changed in the method and when you return to the original call, the fields will remain changed. Note the exception for Strings. Strings are immutable and therefore cannot be changed.
Your book describes the all important concept of getters and setters on page 79. But I know you are already an expert at this!! Look at the ElectricGuitar.java and RDGuitarDriver.java in the lesson3 package. Yes – I, RoboDuke can play the electric guitar!! Read the comments carefully. Remind yourself about Java’s policy of initializing instance variables but not local variables on pages 84-85. Also look at the discussion of comparing variables on page 86. Look at the equals() method in the ElectricGuitar.java blueprint, used to compare reference data types and the use of == within the method to compare primitive data types.
Finish reading the chapter and completing the exercises in your text. I will wait for you while you call Sherlock Holmes to help you with the Five-Minute Mystery on page 92.
Let’s look at another blueprint and driver (you can never see too many of them!!!!). Study the Invoice.java and InvoiceDriver.java in the lesson3 package. Note the equals() method for this one. Note we were comparing the item’s names which are Strings (reference data types) so the method uses the equals() method from the String class inside. So – to create the equals() method for the Invoice, we use the equals() method already available in the String class.
Now It’s Your Turn
Look in the lesson3 package. Fill in the comments for the Harvest.java and HarvestDriver.java and see if you can get the programs to run.
Now It’s Your Turn
Get the document from http://www.vw.vccs.edu/bussci/Wolff/robot/RDThoughtsOnClasses.doc. My suggestion is that you open up Eclipse and type (or copy and paste) with me as I mesmerize and impress you as I explain classes and methods yet again. Work through this example.
Now It’s Your Turn
In this exercise, you will write a class that sells robots.
- Write the RobotSales class assuming a sale of a robot object is described by three instance variables: robotName (a String), numSold (an integer that represents the number of that type of robot sold), and priceEach (a double that is the price of each robot). I only want three instance variables!! The class should have the following methods:
- A constructor that has two parameters – a String containing the name of the robot and a double containing its price. The constructor should set the other instance variable to zero.
- An empty constructor
- All getters and setters
- A method updateSales() that takes a single integer parameter representing the number of additions robots of that type sold. The method should add this number to the numSold instance variable and return nothing
- A method totalValue() that takes no parameters and returns the total value of that robot inventory
- A toString() method that returns a string containing the name of the robot and the number sold, the price each, and the total value
- Write a RobotSalesDriver program that uses RobotSales objects to tract the sales of three types of robots for two different stores.
- Read in the name and price of three types of robots from the keyboard and create three RobotSales objects.
- Prompt for the number of each type of robot sold for each of the two stores. You can use loops if you like or just six sets of prompts/read statements (we will work on the loops later). After each prompt, call the updateSales() method to update the number of robots sold.
- After all the data has been read in call the toString() method for each of the robots to print out their information.
RoboDuke’s Extra Strength Example
Get a glass of wine (or one of those 99 bottles of beer on the wall) and study my program for renting cars. It is in the rentawreck package. Start with Auto.java. Read the comments and understand the code. Then look at Customer.java and Rental.java. Finally I will create some Rental instances in the RentAWreck.java class so I can get rich! This is the driver program. Look at this code and run the example. And I bet you did not know that robots could drive!!!!
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!