Thursday, May 7, 2020

National Prayer Day Post (Lemay Lesson 2 Part 2: Object-Oriented Programming in Java)

Table of Contents

1. Introduction
2. Lesson 2 Part 2 -- Object-Oriented Programming in Java
3. Update on Downloading Java
4. Music on the Theremin and Hip-Hop Google Doodles
5. A Rapoport Probability Problem
6. Reblogging for Today
7. Conclusion

Introduction

Today is May 7th, also known as the National Day of Prayer. It is always held on the first Thursday in May, so that it sometimes, but not always, falls during Teacher Appreciation Week. And of course, many people are praying for the same thing this year -- for the coronavirus crisis to come to an end.

Meanwhile, three days from now is Mother's Day, in both the US and Mexico. American Mother's Day is always on Sunday, while the Mexican Dia de las Madres is always on the tenth. Thus in years when May 10th falls on a Sunday, the two holidays coincide. Notice that this always happens in years when the Mexican Cinco de Mayo coincides with the American Taco Tuesday.

As the school closure continues, I watched a few other math-related videos yesterday. I went back and watched my old Great Courses DVD's -- no, not Michael Starbird, but David Kung. If you recall from four years ago, this series is on paradoxes. I chose four of my favorite lectures to watch again:

  • Lecture 8 (Cantor's Infinity of Infinities)
  • Lecture 17 (Bending Space and Time)
  • Lecture 19 (Crazy Kinds of Connectedness)
  • Lecture 25 (The Paradox of Paradoxes)
All of these lectures involve topics (infinite sets, spherical geometry, topology) that I've discussed in other posts on the blog, not just the David Kung posts.


The other show I watched was Get the Math and Get the Math 2.0 -- two short shows about the usefulness of math in the real world. They aired yesterday on KLCS Channel 58 -- a PBS affiliate here in Southern California. This is what I saw:

  • Fashion: calculate price increases and discounts
  • Gaming: code video games in "Flash" using coordinate graphics
  • Music: estimate how many beats per minute will make a song fit in a given time
  • Cuisine: use a line of best fit to estimate future avocado prices
  • Basketball: find best release height, velocity to make free throws
  • Theater: determine how distance from source is related to light intensity
I wrote about this idea in my traditionalists' post earlier the week -- many students believe that there's no reason for them to learn math until they learn about these applications. Indeed, during the theater part, one actor specifically mentioned that traditionalists' math lessons were a turn-off.

During the cuisine part, the year 2012 was mentioned as "the current year." This suggests how long ago these two episodes were produced.

The gaming part mentions coding to create video games in a language called "Flash." But what I saw on the screen looked just like the Java that I've been learning from Laura Lemay's book. I have read that several languages are used for web development, such as Flash, JavaScript, and Java. Since I'm trying to learn Java now, I want to be able to recognize and understand code written in Java.

And that's exactly where I'm going to start today's post. Let's continue by returning to the second lesson of Java.

Lesson 2 Part 2 -- Object-Oriented Programming in Java


Here is the link to today's lesson:

http://101.lv/learn/Java/ch2.htm

Lesson 2 of Laura Lemay's Teach Yourself Java in 21 Days! is called "Object-Oriented Programming in Java." Here's where we left off:

Inheritance, Interfaces, and Packages

Now that you have a basic grasp of classes, objects, methods, variables, and how to put them all together in a Java program, it's time to confuse you again. Inheritance, interfaces, and packages are all mechanisms for organizing classes and class behaviors. The Java class libraries use all these concepts, and the best class libraries you write for your own programs will also use these concepts.

Ha -- Lemay actually admits that it's time to confuse us again. Unfortunately, we as math teachers are also guilty of this -- as soon as our students have a basic grasp of one topic, it's time to confuse them with a new topic. Fortunately, due to my familiarity with C++, I won't be as confused as readers who are new to coding.

We begin with inheritance:

With inheritance, all classes-those you write, those from other class libraries that you use, and those from the standard utility classes as well-are arranged in a strict hierarchy (see Figure 2.2). Each class has a superclass (the class above it in the hierarchy), and each class can have one or more subclasses (classes below that class in the hierarchy). Classes further down in the hierarchy are said to inherit from classes further up in the hierarchy.

Of course, I'm not going to post Figure 2.2 here -- you'll have to click the link above to find it.

Here's one thing I notice that differs from class hierarchies in C++:

At the top of the Java class hierarchy is the class Object; all classes inherit from this one superclass. Object is the most general class in the hierarchy; it defines behavior inherited by all the classes in Java. Each class further down in the hierarchy adds more information and becomes more tailored to a specific purpose. In this way, you can think of a class hierarchy as defining very abstract concepts at the top of the hierarchy and those ideas becoming more concrete the farther down the chain of superclasses you go.

There is no class called Object at the top of the C++ class hierarchy.

Anyway, below the superclass in a hierarchy are the subclasses:

Subclassing involves creating a new class that inherits from some other class in the class hierarchy. Using subclassing, you only need to define the differences between your class and its parent; the additional behavior is all available to your class through inheritance.

In the author's example, the Vehicle class is derived from Object, and two subclasses extend the Vehicle class depending on whether the vehicle is person- or engine-powered. Later on, the engine-powered vehicles have two subclasses depending on whether it has two or four wheels, and three subclasses of the two-wheeled class are Motorcycle, Scooter, and Biped.

The author explains that subclasses may override the methods of their superclasses:

Things get complicated when a subclass defines a method that has the same signature (name, number, and type of arguments) as a method defined in a superclass. In this case, the method definition that is found first (starting at the bottom and working upward toward the top of the hierarchy) is the one that is actually executed. Therefore, you can intentionally define a method in a subclass that has the same signature as a method in a superclass, which then "hides" the superclass's method. This is called overriding a method. You'll learn all about methods on Day 7, "More About Methods."

At this point, Lemay explains another difference between C++ and Java here:

In other object-oriented programming languages, such as C++, classes can have more than one superclass, and they inherit combined variables and methods from all those classes. This is called multiple inheritance. Multiple inheritance can provide enormous power in terms of being able to create classes that factor just about all imaginable behavior, but it can also significantly complicate class definitions and the code to produce them. Java makes inheritance simpler by being only singly inherited.

Indeed, Jesse Liberty -- the author of the C++ text at this same website -- likes to use animals in his examples of classes. For multiple inheritance, his example is a Pegagus class that derives from both the Horse class and the Bird class.

Returning to Lemay and Java, she explains that instead of multiple inheritance, Java has interfaces:

Although a single Java class can have only one superclass (due to single inheritance), that class can also implement any number of interfaces. By implementing an interface, a class provides method implementations (definitions) for the method names defined by the interface. If two very disparate classes implement the same interface, they can both respond to the same method calls (as defined by that interface), although what each class actually does in response to those method calls may be very different.

Finally, the author tells us about packages, or libraries. This is similar to C++, where libraries are included for input/output streams (iostream), strings, advanced math functions, and so on.

The example of inheritance given in this lesson is another applet. It's unlikely that I'll have access to a website where I can post an applet anytime soon, so I won't dwell on applet examples here. But I'll still read these sections myself, since I might have a job where I need to create applets someday:

Listing 2.4. The final version of HelloAgainApplet.java.
 1:import java.awt.Graphics;
 2:import java.awt.Font;
 3:import java.awt.Color;
 4:
 5:public class HelloAgainApplet extends java.applet.Applet {
 6:
 7:  Font f = new Font("TimesRoman",Font.BOLD,36);
 8:
 9:  public void paint(Graphics g) {
10:    g.setFont(f);
11:    g.setColor(Color.red);
12:    g.drawString("Hello again!", 5, 40);
13:  }
14:}

Just looking at this listing, we can figure out what it does. We begin by importing three packages, namely Graphics, Font, and Color. The paint method in this applet begins by setting the font to f, which is the TimesNewRoman font, bold, and 36 is probably the size. (Glancing back at the text, yes, 36 is definitely the size.) Then we set the color to red. Finally, we draw the text. It appears that 5 and 40 are the coordinates for where the string "Hello again!" should be drawn (although in the text, the coordinates are given as 5 and 25, likely a typo).

Of course, the whole purpose of these Java lessons isn't just for me to cut-and-paste sentences and code from the Lemay website -- that isn't learning anything. I need to be creating code myself.

In this lesson, I learn about inheritance and how classes can fit in a hierarchy. This is a Geometry website, and we already know of one hierarchy from the text -- the Quadrilateral Hierarchy. So let me try to implement a quadrilateral class:

class quadrilateral {

Quadrilaterals have four sides and four angles, so let those be our member variables (attributes):

class quadrilateral {
  int angleA;
  int angleB;
  int angleC;
  int angleD;
  int sideAB;
  int sideBC;
  int sideCD;
  int sideDA;
}

But hold on a minute -- I don't know whether Java even has a variable type called int ("integer") -- that's something I got from C++. In Lemay's example earlier she mentioned that Java has a type called boolean, while C++ calls it bool. Perhaps Java has a type called integer or something like that instead of int.

So there's not much I can do with this class until I learn more about Java. I see something coming up in Lesson 3 that might help me with this. Until then, let me just focus on inheritance rather than try to figure out how to implement anything.

Here are some subclasses in our Quadrilateral Hierarchy:

class kite extends quadrilateral {

class trapezoid extends quadrilateral {

class parallelogram extends trapezoid {

class isoscelesTrapezoid extends trapezoid {

Of course, here's where the limitations of single inheritance come in -- a rectangle is a parallelogram and an isosceles trapezoid. In Java, we're forced to make a choice here:

class rectangle extends parallelogram {
  angleA = 90;
  angleB = 90;
  angleC = 90;
  angleD = 90;

Hey, I found something to put in our Rectangle class -- we set all angles to 90. We might also choose to put in a method to find its area, which multiplies the length by its width. I don't wish to post it today, since I don't want to get into Java syntax that I haven't learned yet. (Is it int, or is it called integer? How do I return a value, since all we've seen so far are void functions?) I could assume that it's just like C++, but I'd rather wait until Lesson 3 after I've learned this stuff.

Update on Downloading Java

No, I haven't tried to download Java again yet. In the past few days, I've decided to download Zoom instead of Java. I'm going back and forth between downloading software that will help me in the teaching world (such as Zoom) and software that might help me in a non-teaching career (Java).

(And who's to say that Java won't help me with a teaching career? A teaching job might open up next week with four sections of Geometry and one section of AP Comp Sci! And for that matter, who's to say that Zoom won't help me outside of education?)

The Zoom download appears to be successful. At least that's working on my old computer!

Music on the Theremin and Hip-Hop Google Doodles

As I mentioned before, my old computer problems make it more difficult to download the new software I want, and it also makes it difficult to play the Fischinger player that Google brought back last week.

So instead, let me post some quick music using the other music Google Doodle -- the theremin. After the introduction, twelve notes become playable. They fit a tritave (or perfect twelfth) on the G major scale, from low G to high d':

G-A-B-c-d-e-f#-g-a-b-c'-d'

Let's set up one of my simpler songs for the theremin -- "Row, Row, Row Your Boat," whose tune is also used for "Same Sign, Add and Keep" and "Mode, Mode, Mode the Most":

G-G-G-A-B-B-A-B-c-d-g-g-g-d-d-d-B-B-B-G-G-G-d-c-B-C-A-G

Notice that even though the theremin is in the key of G major, it's also possible to play this particular song in the key of D major as well. (The seventh doesn't appear in this song, so the note C# isn't needed at all.)

d-d-d-e-f#-f#-e-f#-g-a-d'-d'-d'-a-a-a-f#-f#-f#-d-d-d-a-g-f#-e-d

For this particular song, D major is easier for me to sing than G major, so I'm more likely to sing along with this second version. (Some of my other songs are in G major, though.)

Also, another musical Google Doodle has been posted today -- the Hip-Hop Google Doodle. I tried combining some records to play along with my own "Packet Rap." Try Winston's Amen Brother on one side and Samples, Volume 2 by Google on the other side -- this combination sounds nice with the "Packet Rap."

A Rapoport Probability Problem

Today's Rapoport problem isn't on Geometry -- but it is on probability. I ought to do more probability questions from the Rapoport calendar, considering that probability is part of the Common Core Geometry curriculum here in California.

Today on her Daily Epsilon of Math 2020, Rebecca Rapoport writes:

What is the percent chance, rounded down, that among the top two cards of a shuffled deck, one is a black ace and the other is not?

I remember one day when subbing for a Geometry class that had a probability problem based on decks of cards, and some students were confused by how many cards are in a deck. To solve this problem, we must know there are 52 cards in a deck, including two black aces, the ace of clubs and ace of spades. (If I recall correctly, in that Geometry class the students were allowed a cheat sheet with all 52 cards listed there.)

There are two possibilities for the first card to be a black ace and the second to be something else, so that's 100 combinations. There are two possibilities for the second card to be a black ace and the second to be something else, so that's another 100 combinations. So there are 200 winning hands.

Of course, in total there are 52 possibilities for the first card and 51 left for the second card. So there are a total of 52 * 51 possible hands:

200/(52 * 51) = 0.07541...

Thus the probability is a little more than 7-and-a-half percent. The instructions tell us to round this down to the nearest percent, so the fact that there's a 5 after the 7 means nothing. Therefore the desired probability is 7 percent -- and of course, today's date is the seventh.

I'm also wondering whether we could have done 100/(52 choose 2) instead, since it really doesn't matter which card is the first card and which is the second. It works out in this problem, but there many be some similar probability problems where we do need to distinguish between the first and second cards.

Reblogging for Today

Last year, May 7th fell on a Tuesday and was the first day of my SBAC reblog. I could go back to a different year, or perhaps even post the old David Kung posts from 2016 since I did watch those lectures again last night.

But I will stick to SBAC, since once again, test prep comprised my most popular posts:

Here's how this will work -- when I cover the two daily questions on the blog, I'll write how the two students (whose packets I have) fared. These are one guy and one girl. Both are taking Pre-Calculus as juniors, so they should be doing well on these SBAC questions.

Question 1 of the SBAC Practice Exam is on factoring:

1. Select the equation that is equivalent to (m^2 - 25).
A) (m^2 - 10m + 25)
B) (m^2 + 10m + 25)
C) (m - 5)(m + 5)
D) (m - 5)^2

Yes, this is an Algebra I question -- but it's not a first semester Algebra I question. As a rule of thumb, the first semester of Algebra I is linear and the second semester is nonlinear. Thus as soon as we see an exponent, we know that it's from the second semester of Algebra I (if not Algebra II).

Notice that two of the answers, A) and B), aren't even logical. All we did there is add an extra term, either -10m or +10m, for no apparent reason. So I hope most students will choose either C) or D).

And of course, the key is factoring the difference of squares. Choice D) isn't a correct factorization of the difference of squares -- in fact, choice D) is the factorization of choice A). And so the correct answer is C).

Both the girl and the guy from the Pre-Calc class correctly answer C) for this question.

2. Select an expression that is equivalent to sqrt(3^8).
A) 3^(1/4)
B) 3^3
C) 3^4
D) 3^6

Again we have an exponent, so this isn't a first semester Algebra I problem. Indeed, I suspect that rational exponents -- the idea that the nth root of x^m is x^(m/n) -- doesn't appear until Algebra II.

Once we define rational exponents, the question is easy -- sqrt(3^8) = 3^(8/2) = 3^4. Therefore the correct answer is C).

Both the girl and the guy from the Pre-Calc class correctly answer C) for this question.

We know that the SBAC tests up to Algebra II. The idea is that most freshmen start with Algebra I, which gets them to Algebra II by the year of the test. Of course, traditionalists are upset that there's no Calculus on the test (but even if Common Core encouraged eighth grade Algebra I, that's only Precalculus by the time of the 11th grade test). Some traditionalists take it a step further and don't even accept the level of Algebra II on the SBAC, calling it "pseudo-Algebra II."

Anyway, our first two questions are beyond first semester Algebra I. Oh well -- let's hope that first semester Algebra I appears in tomorrow's questions.

Conclusion

OK, we'll be proceeding to Lesson 3 of Java in my next post. It remains to be seen whether I've downloaded Java by then.

No comments:

Post a Comment