Programming 101
By Way of Java
Instructor: T. Boutell
Transcript for 11/14/96
Stimulants AnonymousWelcome to the Java programmers' roasteria. A wide variety of stimulating beverages are consumed among the whirring virtual machines.
You say, "the homework assignment for tonight was to finish up the calculator applet, resulting in a four-function calculator. If you completed that, page me the URL as I'd like to see, or tell all five of us."
You say, "kristen doesn't mind showing hers around. So, for everyone's benefit, that URL is: http://205.161.59.119/java/demo/Calculator/Calc.html"
You say, "kristen, that's a lovely applet you have there."
kristen says, "thanks. whee!"
kristen says, "i got to admit the logistics of how to make it work are not as fun as ading buttons. You say, "kristen, what's the URL of the source? Let me know if you'd rather not have it pawed over."
kristen says, "paw away."
kristen says, "i didn't add sufficient comments but i can explain what i did."
Tom nods and waits for the file to reach his box. OK to put the source up here with a convenient mime type for viewing in netscape?
jsam confesses: "I haven't done my homework."
Tom nods. There's a lot of that going around, although a few people have incomplete noble efforts.
You say, "okay, folks: http://boutell.com/~boutell/class/Calc.java"
You say, "that's the source to kristen's very pretty calculator."
kristen resists the urge to make really really large buttons.
You say, "kristen: I gather you might like to describe what you did?"
Tom has his own implementation as well, but is enjoying kristen's version.
kristen says, "sure."
You say, "okay. Go ahead."
kristen says, "the basic mechanics are the same for all of the operations: i have two main variables, 'preventered' and 'lastentered'. Oh, and 'runningtotal'. "
kristen says, "each time I hit a button, an event happens, and i set some variables. one variable (temp) determines which operaation (add, subtract, etc.) happens. all of the action happens in a method called 'processOperation'."
kristen says, "so when i press a number button, it basically appends a string representing that number in the text field. the variable determining that operation might be stored already as 4."
kristen says, "the operation (subtract, add, whatever) gets processed the NEXT time you hit an operation button. so, pressing the add button is the same as pressing the equal button, in that both do the calculation upon pressing the button."
Tom nods to kristen. This is the key.
kristen says, "at all times, i keep three numbers: the last entered number (captured from the text entry area), the previously entered number (called preventered), and the running total."
kristen says, "and i threw in another variable to tell me that the person hasn't pressed enough buttons to do a fulloperation yet. that is the 'repeat' variable."
You say, "interesting."
kristen says, "the order of sequence is: do operation; replace 'previously entered number' with 'running total' so that the first number in the equation is always the up-to-date total; remember what the next operation is; and so on, sort of loop-like."
jsam nods.
You say, "that makes sense."
kristen says, "and i use variables to tell me the exceptions to this loop: the very first keypress (not enough to make a full equation), and also clear and equals."
kristen says, "the 'operation' variable is sort of an exception to the loop, in that i wanted a way to know when someone had pressed an operation key (plus, minus) more than once without adding new numbers. this allows the user to just keep pressing 'plus plus plus' to add the last thing he added over and over again, before doing the last-entered-previously-entered-shuffle thing."
You say, "nice feature. I call that an error but it makes more sense to allow it your way."
jsam says, "it is normal calculator behavior, Tom."
kristen says, "i was in the middle of trying to get the equals button to do the same thing but i lost track. basically all the buttons do almost exactly the same thing, but the equals button displays the running total, while the add/subtract buttons remember but don't display."
kristen says, "in the original program, 'addToCurrent' took care of remembering the last entered number. i replaced that with generic operation thingies, and the three variables keep track of the numbers."
Tom nods.
kristen says, "okay, what else. i use a 'displaystring' to keep track of the number that people are actually typing (or pressing). it is appended to until i specifically clear it (by pressing an operation key)."
Tom nods.
You say, "'displaystring' has the slight flaw that it behaves disagreeably if you start typing in a number yourself and then use the number buttons."
You say, "another way would be to get the contents of the text area, append the new digit, and set the text area again."
kristen says, "hm. i am not doing this?"
kristen says, "appendNumber resets the text to be the latest version of the displaystring."
kristen says, "it grabs from the text area and resets the text area. (converting the string to a number also)."
You say, "kristen: if you were, then keeping displaystring around would not be necessary."
You say, "kristen: what I'm suggesting is that you pass the digit string to appendNumber, get the current contents of the result field, append the digit string, and set the current contents of the result field."
kristen lags. Hold on, let me think about that.
kristen says, "ah. hm."
kristen says, "Yes, passing, the digit as an arg would be neater as well."
You say, "hmmm, we have about 40 minutes of class time left. Folks, would you like to continue looking at this applet or move on to new material?"
You say, "kristen: not to take away from your nifty work in any way."
kristen also did GridLayout to set the buttons up.
Tom nods. Which is quite nice, and we should look at how you did it at another time if not now.
kristen nods.
Derrick says, "If I may ask a question?"
You say, "sure, Derrick."
Derrick says, "This is way off base, but I was asked this at work. Is Java capable of knowing when the window has been resized? A yes or no is sufficient and we can go on to something else."
You say, "Derrick: if it is an applet inside a web page, the answer is no. If the window is an 'independent' window, including an independent frame popped up by an applet, the answer is yes."
Derrick says, "For example, redraw itself if the viewer has been changed."
Derrick says, "Gotcha. Thanks."
You say, "let's take a look now at my own four-function calculator. It is not as attractive as kristen's. I focused on making it behave consistently, with as few changes to the original as possible."
You say, "the URL is: http://boutell.com/~boutell/class/FourCalculator.html"
You say, "okay. My solution is similar to kristen's, in that I have replaced addToCurrent with a more general method, completeOperation."
You say, "I remember the previous operation the user clicked on, and I carry out that operation when the next operation is clicked."
You say, "I remember which operation was requested last by setting the variable lastOperation, which is of the Button class."
You say, "this way, I can just say 'lastOperation = (Button) event.target;' to record that information."
You say, "later, I compare this variable to each of the buttons to find out which operation to carry out."
You say, "the (Button) syntax 'casts' event.target from class Object to class Button."
You say, "you can't 'cast' things arbitrarily. The cast will fail if the object doesn't turn out to be of the class you are casting to."
You say, "recording which operation was carried out last by keeping a reference to the button object is convenient, but kristen's solution using a specific number for each operation works as well."
You say, "if you like to use integers to represent one of a small set of possible values, you can save confusion in your code by declaring an 'int' variable with each value, and using those instead of the raw numbers."
You say, "so you can write, for instance, 'int multiplication = 1;'"
soren says to tom, "can you have enumerated types in java?"
You say, "soren: not quite as in C, no."
soren says to tom, "some variation thereof?"
You say, "soren: you'll see momentarily."
You say, "if you write 'int multiplication = 1;' and then refer to 'multiplication', this is readable, but it has one small problem: a copy of that integer is kept in every object of the class, even though the value never changes again."
You say, "fortunately, as we discussed last week, it's possible to have methods -- *and* variables -- which exist in the class but don't have a separate value for every object of the class."
You say, "so we can write this: static int multiplication = 1;"
You say, "there's only one copy of the integer for all objects this way."
You say, "of course, someone could still try to assign a new value to it, with strange and unwanted results."
You say, "we can forbid that using the 'final' keyword."
You say, "so we write something you may have seen in some of the standard java classes:"
You say, "static final int multiplication = 1;"
jkcohen says, "And this is how you declare a constant?"
You say, "jkc: yes."
You say, "this is a variable which cannot be changed, and which has only one value for all objects of the class."
kristen doesn't quite get the casting mechanism.
You say, "kristen: as you know, the target member of the Event class is of the Object class."
kristen says, "okay."
You say, "kristen: it's done that way because 'Object' is the ultimate parent of all classes. So the java programmers were able to write 'event.target = anythingRelevant;' with abandon."
You say, "kristen: when you assign an object of class, say, Button to a variable of class Object, the compiler doesn't complain, because all Buttons are objects -- there's no possibility that you're doing something nonsensical."
kristen says, "hm. okay."
You say, "kristen: however, if you try to assign an object of class Object to a variable of class Button, you are required to be explicit about it, by writing out the cast like this: lastOperation = (Button) event.target;"
You say, "kristen: and when that code runs, Java will check to see if that object is *really* a Button or not. If it isn't, your applet will get pulled over by the java cops."
kristen laughs.
You say, "okay. We only have a few minutes left, so I don't think I'm going to leap into a discussion of exceptions. Next class will have no homework assignment, other than completing this one."
You say, "feel free to continue asking questions."
Barbie says "java is HARD."
Tom laughs.
kristen thwaps Barbie.
kristen says, "so did anyone try dividing by zerio on my calculator?!?!?!"
kristen overdoes it as usual.
Tom thinks he did, but it was already acting oddly by that point.
Tom tries it afresh.
You say, "oh, kristen, that's hilarious."
kristen laughs.
You say, "now my calculator is permanently evil!"
j.j says, "what does it do? what does it do?"
kristen says, "yeah, I forgot to set it back."
You say, "attendance was generally non-stellar tonight."
You say, "I was grateful to have a few newcomers at the start to make things interesting."
jkcohen says, "Well, that's the way of all flesh when it comes to classes where the first homework is due. I remember my English teaching days."
jsam smiles.
Tom hmmms. He needs to send some friendly email about where the course is going. Demoralization isn't his intent.
jsam mmms.
jkcohen says, "Well, this stuff is HARD. When I took a Pascal class in college, we had to read half the book before we had enough semantic background to write a single program."
jkcohen says, "A real program, that is -- not just Hello World."
jkcohen says, "Here, the problems are syntactic, semantic, and lexical."
jkcohen says, "For example..."
jkcohen says, "I didn't know that java.lang.NumberFormatException even existed. I was going about figuring out whether the number was a number iterating through the string and using string.IsDigit on each digit."
Tom nods. And he wants to cover exceptions, believe me. He'll do that first thing next week since this week was weird.
jkcohen says, "At some point, Tom, we should hang out in the Roasteria and just go over how I pass parameters to routines and how I get other parameters back."
Tom nods. Whenever you're up for it.
Tom notes that, for instance, void add(int x) is a legitimate method declaration.
You say, "or, say, int addAndReturnSum(int x) ."
jkcohen says, "Thanks! That was one of my biggest obstacles in writing the actual code. I realized halfway through that I should get the math operations out of the event loop so that I can call them individually in case I hit an equals sign."
kristen says, "this was my favorite class so far. i hate theory, i like hands-on."
Tom nods to kristen. He likes to avoid theory but couldn't help starting out with some.
jkcohen says, "I did figure out something neat about stacks, though."
jkcohen says, "Stacks are descended from Lists, which I knew, but what I didn't realize was that Stacks are simply Lists with two operations disabled through the public interface."
jkcohen says, "They're lists minus AddToBack and RemoveFromBack."
You say, "sure."
kristen prefers changing colors to figuring out how to add the numbers.
Tom laughs.
Tom closes his log.
Next | Up to the Index
Follow us on Twitter | Contact Us
Copyright 1994-2012 Boutell.Com, Inc. All Rights Reserved.
