Tuesday, April 28, 2020

Lemay Lesson 1: An Introduction to Java Programming

Table of Contents

1. Introduction
2. Lesson 1 -- An Introduction to Java Programming
3. Our First Java Application and Applet
4. An Interesting Rapport Problem
5. Reblogging for Today
6. Conclusion
7. EDIT: April 29th Update

Introduction

This is my first post for my big spring/summer project -- learning to code in the Java language. As I explained in my previous post, I'm learning Java as we go along. Everything that I'm posting here is something I'm seeing and learning for the very first time.

Here is a link to the website where I'm learning about Java:

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

Notice that Laura Lemay, the primary author of this website, calls her lessons "Days." The full title of her book is Teach Yourself Java in 21 Days! Thus she's expecting us to read one chapter of her book per day and become Java experts after three weeks. Oh, and there's actually 28 chapters in the book, since there's also a bonus fourth week of lessons.

Well, I could follow the suggested timetable, since I do wish to learn Java quickly. Indeed, I could wait until Friday to start so that the chapters match the dates in May, and then I'd be done with the book by May 21st or 28th. And I can even post my progress here everyday -- so it would be almost like that "post 30 times in May" challenge from four years ago.

But I won't do that for two reasons. First, I don't wish to post that often, especially when I'm posting on a summer schedule of once or twice per week. And second, I want to move slowly to make sure that I'm actually learning the language rather than rushing through the book just to meet an arbitrary 21- or 28-day deadline.

Therefore, I'm posting these as "Lessons," not "Days." I prefer "Lessons" to "Chapters" to emphasize that I'm trying to learn something here. Today we'll work on Lesson 1. Since this lesson is short, I'll spend only one blog post on it, but for most lessons we'll take two posts to complete it.

By the way, if there are any AP Comp Sci teachers or students out there looking for something to read to review Java before the upcoming AP exam, please feel free to use this blog post as a resource, since I'm learning Java along with you. But keep in mind that I'm moving very slowly, so I won't be anywhere near done with the text by the day of the exam.

Meanwhile, today there is a 4 PM board meeting in my new district regarding student grades due to the coronavirus closure. (My old district announced its grading policy two and a half weeks ago.)

Even though that meeting should be complete by the time I click "Publish" on this blog post, there's no way for me to know what decisions have been made in time for me to mention it here. Most likely, the grading policy will be emailed and posted on the district website early tomorrow morning -- but that's too late to make today's blog post.

Perhaps I should have waited until tomorrow to make this post. But I'd already planned on posting today, and I don't wish to delay my study of Java an extra day just so I can post about grades. Still, I am curious about what sorts of grades the students I subbed for will be earning.

Oh, and before we start Java, I want to mention that classic Google Doodles are returning to help entertain us during the virus closures. Yesterday's was the first, and it was on coding! It looks like making a bunny hop, but it's really a thinly disguised version of Logo turtle graphics. (It even mentioned "Logo 2017" in the URL!)

Lesson 1 -- An Introduction to Java Programming

Since this text is written online, there's no need for me to quote large portions of the text (as opposed to our usual side-along reading books). If you want to follow along, you can just click the link to see what I'm talking about.

Lesson 1 of Laura Lemay's Teach Yourself Java in 21 Days! is called "An Introduction to Java Programming," and here's how it begins:

Hello and welcome to Teach Yourself Java in 21 Days! Starting today and for the next few weeks you'll learn all about the Java language and how to use it to create programs that run inside Web pages (called applets) and programs that can run on their own (called applications).

That's the overall goal for the next couple weeks. Today, the goals are somewhat more modest, and you'll learn about the following:
  • What exactly Java is, and its current status
  • Why you should learn Java-its various features and advantages over other programming languages
  • Getting started programming in Java-what you'll need in terms of software and background, as well as some basic terminology
  • How to create your first Java programs-to close this day, you'll create both a simple Java application and a simple Java applet!
Once again, I'm only quoting major lines here. Let's begin with what exactly Java is:

More specifically, Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end UNIX workstations. Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating systems, both at the source and at the binary level, which means that Java programs (applets and applications) can run on any machine that has the Java virtual machine installed (you'll learn more about this later).

As I mentioned in my last post, I'm familiar with C++ since I learned it at UCLA. If Java is indeed modeled after C++, then this should help me learn it.

Here's the most important part for me -- what to do about Java software:

Currently, to program in Java, you'll need a Java development environment of some sort for your platform. Sun's JDK works just fine for this purpose and includes tools for compiling and testing Java applets and applications. In addition, a wide variety of excellent Java development environments have been developed, including Sun's own Java Workshop, Symantec's Café, Microsoft's Visual J++ (which is indeed a Java tool, despite its name), and Natural Intelligence's Roaster, with more development tools appearing all the time.

This website is clearly old, as it continually refers to Netscape as a Java-enabled browser to view Java applets in. Still, the important thing is that I need a Java development environment.

Here I learn that compiling Java code is different from compiling C++ code:

Things are different when you write code in Java. The Java development environment actually has two parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program and, instead of generating machine codes from your source files, it generates bytecodes. Bytecodes are instructions that look a lot like machine code, but are not specific to any one processor.

And as for the interpreters, I read:

In the future, however, the Java bytecode interpreter will most likely come with every new operating system-buy a Windows machine, and you'll get Java for free.

Well, this is definitely the future (nearly a quarter-century later), and I have a Windows machine. But I definitely do not have a Java interpreter on my computer. This must have been just wishful thinking on Lemay's part.

Meanwhile, here's a comparison between Java and the language I'm familiar with, C++:

Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you because you have most of the foundation already. (In fact, you may find yourself skipping through the first week of this book fairly rapidly. Go ahead; I won't mind.)

Note: I'm certainly not skipping through the first week rapidly. I want to make sure that I'm learning Java thoroughly.

At this point, the author tells us how to install Java on our system. I will skip over this part, since this won't matter until I actually decide to download it. But note -- this might be sooner than later, since I do wish to get down and write some actual code.

Our First Java Application and Applet

In this lesson, there are two programs -- one an application, the other an applet. Let me post both of those right here.

First is the application:

Listing 1.1. Your first Java application.
1: class HelloWorld {
2:     public static void main (String args[]) {
3:         System.out.println("Hello World!");
4:     }
5: }

Lemay warns us that the line numbers aren't part of the program -- they're included so that she can refer to the lines more easily in the book. This isn't BASIC where every line needs to be numbered.

It is traditional for coding texts to begin with the "Hello World" program. Indeed, I remember subbing in a C++ coding class one day, and sure enough, the students were working on their own "Hello World" program.

Lemay doesn't explain how exactly this program works -- most likely, she'll tell us all about the individual words and commands in subsequent chapters. So let me try to figure out some of the words myself, based on what I already know about C++ and other programming languages,

In Line 2, the most important word is main. Every complete program in C++ has a main function, and it appears that the same is true for Java applications. The main function in C++ and Java corresponds to the program in Pascal.

The word void appears just before main. OK, there are void functions in C++ as well. It means that this function does not return a value (it might print something, but that's not the same as the return value of the function). All procedures in BlooP return a value, but this isn't true for most other coding languages. In fact, a Pascal procedure doesn't return a value, while a function does.

But in our Java program, there are two more words before main -- public and static. I've seen these words in C++ before, but in more complex programs, not the simple "Hello World" program.

Oh, and there's one more word in Line 2 to analyze -- String args[]. Actually, there's something similar in C++, called argc and argv. It refers to parameters that we would put on the command line when starting the program, in case we need to give main some extra information. In C++, the "Hello World" program doesn't need argc or argv, but I assume that in Java, we have to mention the args parameter whether we need it or not.

That takes care of Line 2. Line 3 is obviously where we actually print "Hello World!" I notice that the command that actually does the printing is println. This reminds me of print in both BASIC and Logo. And those extra two letters ln appear in Pascal, where we see writeln. Those two letters stand for line, so I assume that in Java, println prints the message on its own line -- without ln, the next message (if there is one) would print on the same line.

Before println, I see the word out. This reminds me of cout in C++ -- it refers to the output "stream" that actually prints the message. I also see the word System there, which has no equivalent in C++ or any other language I know. I suppose this means that it's the "system" that's sending the message to the output stream to be printed.

Oh, and there's one more thing I notice about this short program. HelloWorld isn't the name of the program, but the name of a class in Line 1. In C++, classes refer to objects -- indeed, that's where the name "object-oriented programming" comes from. But the "Hello World" program in C++ doesn't require any classes -- and in C++, main isn't the member of any class. Apparently in Java, even the main function must be the member of a class. Oh, and that also explains why main is public -- in C++, only members of classes are public. Since main is a member of a class in Java, we're able to declare it public.

Now let's look at the first applet that Lemay writes for us:


Listing 1.2. The Hello World applet.

1: import java.awt.Graphics;
2: 
3: public class HelloWorldApplet extends java.applet.Applet {
4: 
5:     public void paint(Graphics g) {
6:        g.drawString("Hello world!", 5, 25);
7:    }
8:}

In Line 3, we see that HelloWorldApplet is the name. I see  java.applet.Applet at the end of this line, which indicates that it's a Java applet. Unlike the previous class, this class is itself declared as public -- probably because applets appear on the Internet, so of course we expect the "public" to be able to see it.

In Line 5, this class has one member function, but I notice that it isn't main. This likely means that unlike applications, applets don't need to have a main function. The function is called paint, and it has one parameter, a Graphics object. So this applet has something to do with graphics.

In Line 6, the function drawString (a member of Graphics) is called. This makes sense, as "graphics" means "drawing something on a screen." The object to be drawn is a string -- likely the string "Hello world!" that's listed as the first parameter. I'm not sure what the other parameters, 5 and 25, are for. Well, drawings have sizes, so maybe 5 and 25 are the length and width (or other dimensions) of our drawing. Or maybe they stand for color numbers, since drawings have colors (but I can't be sure, since the link on the Lemay page to what the applet looks like is in black and white).

That leaves us with Line 1, and for this I must think back to C++ again. To import something means to bring it in, or "include" it in our program. C++ programs often begin with #include, as in the line #include <iostream.h>. The file being included is java.awt.Graphics -- I don't know what awt is, but I know what java and Graphics are. Thus by importing this file, we're saying that this applet will include Java graphics.

And that's completes our first lesson. Some readers might wonder, is that it? What's the point of breaking down the two Hello World programs, when it would be easier for the readers just to click on Lemay's website and learn Java directly from there, leaving me out? And if Lemay doesn't explain what all the parts of the program means, it's because she doesn't want us to know yet. She'll explain all the parts of the program in due time.

Well, here I am the one who's learning Java. And since I haven't installed Java yet, the one thing I can do to help me learn Java is compare it to C++ and the other languages I know. This is all part of my personal learning process.

No, I don't need to post a learning process on the blog. But blogging all of this helps keep me accountable to myself -- if I say I'm going to blog something, then I have to blog it, or otherwise there are potential readers out there who'll wonder why I'm not blogging it. If you don't wish to read about my Java learning process, then you don't have to read it. You can skip all the Java sections of my posts and read the rest.

But by posting my own process in learning something new such as Java, it also illustrates to me what a learning process looks like. If I struggle to learn Java, it will help me appreciate what happens when my students struggle to learn math in my math classes. Recall that Java is still just Plan B -- Plan A is to return to the classroom as a math teacher. If Plan A succeeds, I don't need a Plan B -- but writing out the steps to Plan B (my learning process in Java) can ultimately help me in Plan A (the kids' learning process in math).

An Interesting Rapport Problem

I'm burying the Rapoport problem here, so clearly it's not a Geometry problem.

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

Let [e_0; e_1, e_2, ...] be the infinite simplified continued fraction representation of the mathematical constant e. What is e_41?

I've written about continued fractions on the blog before, going back to the Ogilvy book. Let me reblog a little of what I wrote about them in my final 2018 post:

At this point, Ogilvy writes about the main purpose of continued fractions. It's not to find fractions to approximate other fractions, but rather to find fractions to approximate irrational numbers.

His first example is sqrt(2). He begins by adding 1 -- that is, floor(sqrt(2)) -- and then subtract it back:

sqrt(2) = 1 + sqrt(2) - 1

We had good luck inverting before, so we try it again:

sqrt(2) = 1 + 1/(1/(sqrt(2) - 1))

Now we perform the Algebra II trick -- rationalize the last denominator by multiplying it and the numerator by its conjugate:

sqrt(2) = 1 + 1/((sqrt(2) + 1)/((sqrt(2) - 1)(sqrt(2) + 1)))
            = 1 + 1/((sqrt(2) + 1)/(2 - 1))
            = 1 + 1/(1 + sqrt(2))

At this point, we have that sqrt(2) equals something on the right hand side -- and that something itself contains a sqrt(2). So we substitute the entire RHS in for sqrt(2):

sqrt(2) = 1 + 1/(1 + (1 + 1/(1 + sqrt(2))))
            = 1 + 1/(2 + 1/(1 + sqrt(2)))

And the RHS still has a sqrt(2), so we substitute in RHS again and again ad infinitum:

sqrt(2) = 1 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + ...

Just as rational numbers have finite continued fraction expansions, it turns out that irrational numbers have infinite continued fraction expansions.

Returning to 2020, I notice that Ogilvy discusses continued fractions for several famous irrational numbers, including e. But instead of just looking it up, let's generate it on the TI calculator. We begin by entering the number -- let's just try sqrt(2) again to be sure that it works. Since the integer part is 1, we already know to start the continued fraction with a 1

1.414213562 [1;

Now we type in the following sequence:

fPart(Ans)^-1

and keep pressing the ENTER key over and over. Each time we get an answer, we append its integer part to our continued fraction:

2.414213562 [1; 2,
2.414213562 [1; 2, 2,
2.414213562 [1; 2, 2, 2,
2.414213562 [1; 2, 2, 2, 2,

and so on. So our continued fraction is [1; 2, 2, 2, 2, ...] -- which exactly is what we obtained earlier.

The sequence won't always become constant as it did above, but many irrational numbers do produce certain patterns in their continued fractions. Let's try sqrt(3) and pi first, before finally tackling e:

sqrt(3)
1.732050808 [1;
1.366025404 [1; 1,
2.732050808 [1; 1, 2,
1.366025404 [1; 1, 2, 1,
2.732050808 [1; 1, 2, 1, 2,
1.366025404 [1; 1, 2, 1, 2, 1,
2.732050808 [1; 1, 2, 1, 2, 1, 2,
1.366025404 [1; 1, 2, 1, 2, 1, 2, 1,
2.732050808 [1; 1, 2, 1, 2, 1, 2, 1, 2,

And so sqrt(3) = [1; 1, 2, 1, 2, 1, 2, 1, 2, ...]. Since our continued fractions are based on fractional parts (fPart), as soon as two numbers in the sequence have the same fractional part, the sequence must completely repeat.

pi
3.141592654 [3;
7.062513306 [3; 7,
15.99659441 [3; 7, 15,
1.003417231 [3; 7, 15, 1,
292.6345986 [3; 7, 15, 1, 292,
1.575799191 [3, 7, 15, 1, 292, 1,
1.736716576 [3; 7, 15, 1, 292, 1, 1,
1.3573741     [3; 7, 15, 1, 292, 1, 1, 1,
2.798188227 [3; 7, 15, 1, 292, 1, 1, 1, 2,

There's no obvious pattern here, especially since that 292 sticks out like a sore thumb. We're hoping that e will produce something promising:

e
2.718281828 [2;
1.392211191 [2; 1,
2.549646778 [2; 1, 2,
1.819350244 [2; 1, 2, 1
1.220479286 [2; 1, 2, 1, 1,
4.535573476 [2; 1, 2, 1, 1, 4,
1.867157439 [2; 1, 2, 1, 1, 4, 1,
1.153193129 [2; 1, 2, 1, 1, 4, 1, 1,
6.527707921 [2; 1, 2, 1, 1, 4, 1, 1, 6,
1.894987663 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1,
1.117333838 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1,
8.522690623 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8,
1.913177616 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1,
1.095077214 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1,
10.51776712 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10,
1.931370227 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1,
1.073686888 [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1,

And there we have it, a clear pattern. Excluding the initial e_0 = 2, all of the coefficients are 1 except for every third value:

e_2 = 2
e_5 = 4
e_8 = 6
e_11 = 8
e_14 = 10

We see that each of these indices is one less than a multiple of 3 -- if we add that one back and then take 2/3 of it, the answer is the correct value in the continued fraction.

Adding one to 41 gives 42, and then two-thirds of 42 is 28. Therefore e_41 = 28 -- and of course, today's date is the 28th.

Reblogging for Today


Each day during the coronavirus closure, I've been reblogging an old post based on the date. And since today is April 28th, I'll go back to an old post from that date.

Four years ago today, I was officially hired to work at the old charter school. The letter I received notifying me of this was dated April 28th, 2016. But I didn't check my email until after I completed my April 28th post, so I didn't mention my new job on the blog until my April 29th post that year.

The last time that April 28th wasn't on the weekend (and hence was a posting day) was three years ago, just after I left the old charter school. That day, I posted an old trig lesson that I'd found from another teacher, Micaela AKA "Alternative Math":

Today's idea comes from an anonymous Washington State teacher who only goes by the username "Alternative Math" -- named for the alternative high school to which this teacher is assigned.

https://alternativemath.wordpress.com/2016/02/05/geometry-constraints-and-trig/

Here is the original post:

I have been busy planning out a unit on trigonometric ratios for my Geometry B course. I have been trying to balance the open ended exploration and project based learning that I prefer with the more typical questions that students will eventually see on state tests or future math classes.
Here is the [Common Core] standard I’m addressing with this lesson: G-MG.3 Apply geometric methods to solve design problems (with a focus on constraints).
I introduce trig with the slope ratio, proportions, and physically measuring before I ever tell them the word tangent. I’m leaning toward using [High Priestess] Kate Nowak’s Introduction to Trig and then running a few Labs where calculate heights and distances of physical things outside before offering this [worksheet].
Afterwards I might show a few ramp fails before giving them a more open ended design problem. I’m still working on the actual formatting piece, but it will be a blueprint showing a door/stoop 5 ft high, but due to size of parking lot also has a restriction on length. Students will figure out it is not possible to use one ramp in that space and will have to figure out how to use two or more ramps to fit the constraints.
Nothing too mind blowing or exciting here, but I figure it gets at what I’m hoping they understand.
Notice that this teacher attributes this activity to yet another teacher -- New Yorker Kate Nowak. Even though I myself found this activity on the Alternative Math page, in today's post I will credit Kate Nowak as the originator of the idea. This is due to the anonymity of the Alternative Math website -- it's far easier for me to write "Nowak" than "the author of Alt Math," and it's easier to write "she" (referring to Nowak) than to write "he or she" over and over. We already know who Nowak is -- I mentioned her blog that same week and explained why she's known as the "High Priestess."

This is what Nowak writes about this activity on her own website:

The children understand that sin, cos, and tan are side ratios. The children! They understand! They are not making ridiculous mistakes, and they can answer deeper understanding questions like, "Explain why sin(11) = cos(79)." I think right triangle trig is a frequent victim of the "First ya do this, then ya do this" treatment -- where kids can solve problems but have no idea what is going on. There's often not a ton of time for it, and it responds well to memorized procedures (in the short term). So, if your Day One of right triangle trig involves defining sine, cosine, and tangent, read on! I have a better way, and it doesn't take any longer.

I don't need to keep posting the rest of this project. The main idea from both Micaela and Nowak is that students are often turned off when they hear words like "trigonometry," "cosine," and so on -- and after the unit is over, they forget what trig is, and think of "cosine" as a calculator button and nothing more. The project, therefore, is to get students thinking about the concepts without getting hung up with the vocabulary. How many non-mathematicians, decades after they took trig, remember even that trig has something to do with triangles?

This is the intent of my "threenooklore" (or is it "threesidelore"?) project that I announced on the blog a few posts back. It seeks to replace complicated words like "trig" (or "trigonometry") with hopefully simpler words that help students remember the concepts. It goes hand-in-hand with Nowak/Micaela's triangle activity -- and I'll start this project at some point this summer.

Conclusion

Today, California Governor Gavin Newsom mentioned the possibility that the school year might start in July, if the coronavirus is sufficiently under control by then. This would allow students to make up some of what they missed during the virus closure.

Normally, as a sub, I wouldn't want to be anywhere near a classroom in July (unless it's as a summer school teacher, as I tried to sign up for two years ago). But when that stimulus money starts to run out and I have an empty bank account in July, I'll be itching to return to the classroom then.

I'm still deciding what my blogging schedule will look like if schools in either of my two districts open in July. For the most part, I'll cross that bridge when I get to it. I will start some of my summer projects (threenooklore) a little earlier if I hear that either of my districts plans on reopening early. (It will have meant that "summer break" is running from April-June this year.)

Of more immediate concern is, will I purchase and download any software with the money I earned in the stimulus? I'm considering both Zoom (for a possible teaching career) and Java (for a possible non-teaching career), and I might make a decision in time for my next post.

EDIT: April 29th Update

No, the main reason I'm editing this post on April 29th because I wanted to post the grading system in my new district -- though I will post it anyway. In my new district, elementary students will not get letter grades at all. In middle and high schools, the grades will be given as "credit/incomplete," but students who want  A's, B's, or C's can still opt in to letter grades. (Does this mean that D grades now count as incomplete?) Grades can be no lower than what they were at the time the schools closed.

But actually, I edit this not to write about A's, B's, and C's, but about C's, D's, E's, and G's. Yes, that's right -- I already mentioned the return of the Google Doodle for coding in Logo, and today the Fischinger player is back!

Unfortunately, there's a problem -- the Fischinger player is running very slowly on my computer, even when compared to the other Doodles this week. My computer is about four years old, which is considered over the hill by computer standards. I already mentioned how the Visual Basic CD failed to run on my computer, and now I've having trouble with the Fischinger player.

I did create a quick new song for Fishinger player before it stopped working. Recall that the lowest four rows on the player are g', c', d', and e' on bottom row. Enter the following melody:

Melody: d'-d'd'd'e'g'c'g'-c'c'g'g'd'e' (a dash - means skip a column)

To this, I add a walking bass line. I've generated the melody at random, but I choose bass notes that sound good with this melody. Our walking bass line will use the top row (C) and fourth row (G), and will use only every fourth column, starting with the first column:

Bass: G---G---C---C---

I'm barely able to enter these notes and have the song repeat a few times before my computer grinds to a halt.

With my old computer struggling to play the Fischinger songs, this is discouraging me from trying to download more complex programs such as Zoom or Java, as I mentioned earlier. I'll still wait until my next post to discuss my software plans in full.

No comments:

Post a Comment