Thursday, May 14, 2020

Lemay Lesson 3 Part 2 -- Java Basics

Table of Contents

1. Introduction
2. Update on Virus & Closures
3. Lesson 3 Part 2 -- Java Basics
4. Some Rapoport Geometry Problems
5. Reblogging for Today
6. Conclusion

Introduction

It's been ages since I've posted on back-to-back days. I decided that I might as well finish Lesson 3 right away. I've already downloaded Zoom and Java, and with all my downloading complete, I need to get through these lessons quickly.

Update on Virus & Closures

I've heard that the stay-at-home order, which was set to expire tomorrow, is supposed to last for three more months. An exact date hasn't been announced -- some sources say July 31st, while other claim it's August 31st. To me, it's logical just to add three months to tomorrow's date to get August 15th -- which is, to me, uncomfortably close to the first day of school.

Notice that this order applies only to the County of Los Angeles, not the State of California. Recall that my two districts are in two different counties. (I mentioned this back when it appeared that an initiative to divide California into three new states would make the ballot -- my two districts would have ended up in two different states under the proposal.) Then again, the state budget proposal that Governor Gavin Newsom released today suggests cuts throughout the budget, including to schools.

Oh, and speaking of LA County, another thing that won't happen is the LA County Fair. This is the fair that the old charter school visited every year and inspired my fair song. I'm sorry, but this year you won't be able to meet me in Pomona, Mona.

I'll wait to see how this affects the first day of school in my districts -- if a stay-at-home order extends past the start of the new school year. Meanwhile, I've heard that the California State University system is set to have all online classes for the fall semester. Some people think it might be awkward for college students to be home but for K-12 students to be in classrooms, thereby suggesting that all K-12 schools remain online only through the end of December as well. This will be very problematic for us subs -- thus making the need for me to learn Java and escape being a sub all the more urgent.

And speaking of online education, I've heard that the online AP exams are causing problems for some students on their computers. The AP Calculus exam was on Tuesday, and those who have older, outdated software were unable to submit. As the owner of a five-year-old computer, I can certainly relate to these students. We'll see whether the AP website is working in time for the AP Computer Science A exam tomorrow.

As for the SAT exam, I've heard that the UC system might not require the SAT test not just for the upcoming Class of 2021, but for 2022 and 2023 as well. This means that none of the students I taught at the old charter school will need to take the exam. Again, the dean at my old school tried to get my students to behave by pressing the need to do well on the SAT -- how could he have known that a coronavirus would wipe out the SAT for all of those students?

Lesson 3 Part 2 -- Java Basics

Here is the link to today's lesson:

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

Lesson 3 of Laura Lemay's Teach Yourself Java in 21 Days! is called "Java Basics." Here's where we left off:

More About Assignment

Variable assignment is a form of expression; in fact, because one assignment expression results in a value, you can string them together like this:
x = y = z = 0;
Once again, my point of emphasis will be on comparisons to C++. And right here in explaining variable assignment, Lemay writes:

The right side of an assignment expression is always evaluated before the assignment takes place. This means that expressions such as x = x + 2 do the right thing; 2 is added to the value of x, and then that new value is reassigned to x. In fact, this sort of operation is so common that Java has several operators to do a shorthand version of this, borrowed from C and C++. Table 3.4 shows these shorthand assignment operators.

Once again, I won't show you Table 3.4, but notice that I did use one of those shorthand assignment operators in the "two-to-the-three-to-the" method from yesterday:

double twotothethreetothe(int n) {
     int cell0 = 1;
     for (int i=1; i<=n; i++)
          cell0 *= 3;
     double cell1 = 1.0;
     for (int i=1; i<=cell0; i++)
          cell1 *= 2.0;
     return cell1;
}

Here cell0 *= 3; is shorthand for cell0 = cell0 * 3. The original BlooP doesn't have this shorthand, and so Hofstadter had to write CELL(0) <= 3 X CELL(0);.

We move on now to incrementing and decrementing:

As in C and C++, the ++ and -- operators are used to increment or decrement a variable's value by 1. For example, x++ increments the value of x by 1 just as if you had used the expression x = x + 1. Similarly x-- decrements the value of x by 1. (Unlike C and C++, Java allows x to be floating point.)

That's interesting, though I can't think of a need to increment or decrement a float as of now. And of course, I've already used i++ in yesterday's program posted above.

Like C++, Java distinguishes between prefix ++i and postfix i++ notation. The next listing demonstrates how it works:


Listing 3.2. Test of prefix and postfix increment operators.
 1: class PrePostFixTest {
 2: 
 3: public static void main (String args[]) {
 4:     int x = 0;
 5:     int y = 0;
 6: 
 7:     System.out.println("x and y are " + x + " and " + y );
 8:     x++;
 9:     System.out.println("x++ results in " + x);
10:     ++x;
11:     System.out.println("++x results in " + x);
12:     System.out.println("Resetting x back to 0.");
13:     x = 0;
14:     System.out.println("------------");
15:     y = x++;
16:     System.out.println("y = x++ (postfix) results in:");
17:     System.out.println("x is " + x);
18:     System.out.println("y is " + y);
19:     System.out.println("------------");
20: 
21:     y = ++x;
22:     System.out.println("y = ++x (prefix) results in:");
23:     System.out.println("x is " + x);
24:     System.out.println("y is " + y);
25:     System.out.println("------------");
26: 
27: }
28: }
In Line 15, postfix x++ assigns 0 to y and then increments x to 1. In Line 21, prefix ++x increments x to 2 and then assigns 2 to y.

At this point other operators are listed, and they work just like C++. The key ones are == for equals (since = is assignment -- again, equality and assignment are distinguished in every language except for Mocha BASIC) and != for does not equal. I already used <= for less than or equal in the program from yesterday.

There are also logical operators, namely && for and, || for or. I already used && in the Goldbach method from yesterday. There are also "bitwise" logical operators:

Finally, here's a short summary of the bitwise operators in Java. Most of these expressions are inherited from C and C++ and are used to perform operations on individual bits in integers. This book does not go into bitwise operations; it's an advanced topic covered better in books on C or C++. Table 3.6 summarizes the bitwise operators.

I won't give Table 3.6 or explain the bitwise operators here.

We learn about "operator precedence," which is essentially Java's version of PEMDAS.

We learn about "string arithmetic," where + is used to concatenate strings. Other languages, including BASIC, use + to concatenate strings. In BASIC, we can't add strings to non-strings, but we learn that in Java, we can.

Lemay wraps up the chapter with a little Q&A. One of the questions here mentions C++:

Q:
Why does Java have all these shorthand operators for arithmetic and assignment? It's really hard to read that way.
A:
The syntax of Java is based on C++, and therefore on C. One of C's implicit goals is the capability of doing very powerful things with a minimum of typing. Because of this, shorthand operators, such as the wide array of assignments, are common.

Some Rapoport Geometry Problems

Yes, there's finally a week full of Geometry problems on the Rapoport calendar. Unfortunately, today isn't one of them.

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

What is the difference between the prime factors of 8051?

This is a tricky one since it's not easy to find the factors of the number in question. The number 8051 clearly doesn't have 2 or 5 as factors, nor does is it a multiple of 3 as its digit-sum is 14 (omega rule) or a multiple of 11 as its alternating digit-sum is 4 (alpha rule). Trial division by 7 reveals that 8050 is a multiple of 7, but not 8051.

This might take some time, so let's try estimating the size of the factors. The way the question is worded -- "the difference between the prime factors" -- suggests that this number is a semiprime or the product of exactly two primes. One of these two factors must be less than the square root of 8051 while the other must be greater. And the square root is 8051 is almost 90, since 90^2 = 8100.

Hmm -- 8051 is exactly 49 less than the perfect square 8100. In other words, 8051 = 90^2 - 7^2. This gives us an idea:

8051 = 90^2 - 7^2
         = (90 + 7)(90 - 7)
         = (97)(83)

And as both 97 and 83 are prime, we are done. Subtracting them gives 97 - 83 = 14. Therefore the desired difference is 14 -- and of course, today's date is the fourteenth. Using the difference of squares is much more efficient than trial division, where it would have taken us forever to find the prime factors 97 and 83.

(In fact, as soon as we saw that 8051 is 7^2 shy of a perfect square, we know that the difference between the factors is twice 7 or 14 without finding the factors. But of course, we might as well find the factors anyway to make sure that both are prime.)

So this isn't a Geometry problem, but there are several Geometry problems coming up within the upcoming week on the Rapoport calendar. Let's look at these problems in a random order -- yes, I'm doing it this way so you'll have to do the problems rather than cheat by just giving the date.

Problem #1: Find x.

[Here is the given info from the diagram: it's a right triangle with legs x and 21 and hypotenuse 29.]

This is a straightforward Pythagorean Theorem problem:

x^2 + 21^2 = 29^2
x^2 + 441 = 841
x^2 = 400
x = 20

Therefore the desired leg length is 20 -- and of course, this problem will be for the twentieth.

Problem #2: Regular polygon.

[Here is the given info from the diagram: it's a decagon with interior angle measure 8x.]

A decagon has 10 sides, an so we use the Polygon-Sum Theorem from Lesson 5-7:

8x = (10-2)180/10
8x = (8)180/10
x = 180/10 = 18

Therefore the desired value is 18 -- and of course, this problem will be for the eighteenth.

Problem #3: A regular pentagon is enclosed with 90 evenly spaced posts, with a post at each vertex. How many posts are on each side of the pentagon?

This would appear to be a case for the Equilateral Polygon Perimeter Formula of Lesson 8-1:

p = ns

Plugging in the values give:

90 = 5s
s = 18

Thus each side of the pentagon is 18 units. But hold on a minute -- we already did the problem from the eighteenth, so the desired answer can't be 18.

As it turns out, the posts at each vertex are counted as part of two different sides of the pentagon. So there are actually 19 posts on each side -- multiplying by 5, this would give 95 posts with five of them double-counted, for a final total of 90 posts. Therefore the actual correct answer is 19 posts -- and of course, this problem will be for the nineteenth.

This is the classic "fence post error" that often shows up in problems involving fences. (I know, the original problem said "posts," not fence posts -- but that's what they usually mean.) The fence post error often shows up in computer coding. Here's an example from BlooP:

DEFINE PROCEDURE "PRIME?" [N]:
BLOCK 0: BEGIN
     IF N=0 THEN:
     QUIT BLOCK 0;
     CELL(0) <= 2;
     LOOP AT MOST MINUS [N,2] TIMES:

And let's cut it off here. Why do we loop at most n - 2 times? It's because we want the loop to start with 2 at the first trial divisor  -- the value of CELL(0)-- and n - 1 as the last trial divisor. Due to the fence post error, we might think this is (n - 1) - 2 = n - 3 iterations, but in reality it's n - 2 iterations.

Once again, we haven't quite reached loops in Java, but notice that the for loop of Java or C++ avoids the loop-counting fence post error since we specify the start and end values, not the total number of iterations:

boolean isprime(int n) {
     if (n<2)
          return false;
     for (int i=2; i<n; i++)
          if (n%i==0)
               return false;
     return true;

}

Then again, whether the loop condition should be i<n or i<=n (that is, i less than or equal to n) may also be considered a fence post error. We know that it needs to be i<n since if i=n, we'd always get a divisor because n always divides n, so we'd always get false and never find any primes. This is the "itself" part of the definition "a prime number has no factors other than one and itself."

Fence-post errors are usually neither compile- nor run-time errors -- the program still runs, but we'll get invalid answers (such as no numbers are prime). They might cause a run-time error, for example, if we have i>=0 instead of i>0 and attempt to divide by i. (A good compiler might be able to catch a fence-post error if the loop involves the index of an array, but we haven't reached arrays yet.)

Problem #4: The distance between (32, 12, 5) and (20, 3, 13)

We obviously must use the Distance Formula in Three Dimensions, from Lesson 11-6:

d = sqrt((20 - 32)^2 + (3 - 12)^2 + (13 - 5)^2)
   = sqrt(12^2 + 9^2 + 8^2)
   = sqrt(144 + 81 + 64)
   = sqrt(289) = 17

Therefore the desired distance is 17 -- and of course, this problem will be for the seventeenth.

Problem #5: Find x.

[Here is yet another unlabeled diagram, but unlike the other problems, I need to give you labels in order for it to make sense to you. In Triangle ABC, Angle C is right, AC = 36, BC = 45, D is on BC such that Ray AD bisects Angle A, BD = x.]

This problem starts out as another Pythagorean Theorem problem:

BC^2 + 36^2 = 45^2
BC^2 + 1296 = 2025
BC^2 = 27
BC = 27

But no, this problem isn't dated the 27th. That's because we're asked to find BD, not BC.

This problem requires a theorem that has appeared on our mathematical calendars in the past, but not in the U of Chicago text. Because it involves angle bisectors, it's often referred to as the Angle Bisector Theorem:

https://mathbitsnotebook.com/Geometry/Similarity/SMAngle.html

Angle Bisector Theorem:
An angle bisector of a triangle divides the opposite side into two segments that are proportional to the other two sides of the triangle.

The link above gives a proof of this theorem. Of course, the word "proportional" is a huge giveaway that the proof will have something to do with similarity (not to mention "similarity" in the URL).

Notice that this theorem applies to all triangles, not just right triangles -- though the particular triangle we're applying it to happens to be right:

x/45 = (27 - x)/36
36x = (45)(27) - 45x
81x = (45)(27)
x = (45)(27)/81
x = 45/3 = 15

Therefore the desired length is 15 -- and of course, this problem will be for tomorrow, the fifteenth.

Reblogging for Today

Unlike yesterday, there's not much to reblog in last year's May 14th post. I did sub in a special ed math class that day, but I don't need to post that again. So instead, I reblog only the SBAC questions:

Question 11 of the SBAC Practice Exam is on statistics:

Click above the numbers to create a line plot for the given percent chances of rain in different cities.

65, 65, 70, 70, 80, 80, 80, 80, 85, 95, 95, 95, 100

This is a statistics question -- and as I mentioned in my last post, stats, if it's to be taught in Algebra I at all, is covered in the second semester.

As with many Common Core Statistics questions, this is the first time that I've ever seen a "line plot." Apparently, it's similar to a bar graph. There should be two X's above 65, two X's above 70, no X above 75, four X's above 80, one X above 85, no X above 90, three X's above 95, and one X above 100.

The girl from the Pre-Calc class correctly answers for this question. But unfortunately, the guy from that class skips this question altogether. It doesn't help that the question doesn't print on the packet properly -- rather than before Questions 12-13, it appears on the next page after Questions 16-17.

Question 12 of the SBAC Practice Exam is on dimensional analysis:

The formula for the rate at which water flows is R = V/t, where

  *   R is the rate,
  *   V is the volume of water measured in gallons (g), and
  *   t is the amount of time, in seconds (s), for which the water was measured.

Select an appropriate measurement unit for the rate.

A) gs
B) g/s
C) s/g
D) 1/sg

This question on units could appear in the first half of an Algebra I text, since this is a linear equation (provided t is given or a constant). This problem basically solves itself -- the rate is V/t, and V is in g while t is in s. Thus V/t is in g/s, or gallons per second. The correct answer is B).

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

Conclusion

We're done with Lesson 3 of the Lemay text. Some of the things I tried to do in the BlooP programs yesterday will be taught in Lesson 4, which will be at some point next week.

And for those of you who are students about to take the AP Comp Sci A exam tomorrow (and are using this website to review a little Java), I wish you good luck in answering the questions, and good luck in being able to submit your answers.

No comments:

Post a Comment