-
Title:
Arithmetic Operations 2 - Intro to Java Programming
-
Description:
-
Initially, if I compile and run the tester, all
-
of the methods are actually returning zeros. Let's fix
-
the amdahl Speedup first. Right now, it's returning 0.
-
What if I just write it in sort of the
-
way it is? Now, if I run the tester
-
again, I'm getting an actual of 2.875. When I
-
expected 1.6. I think I'd better go back and
-
fix it. Right now, this is doing the divisions first,
-
so 1 over s, and s over n.
-
And then the additions and subtractions in the middle.
-
And there's no real sense of the fraction
-
that we started with. This whole piece From s
-
to n should stay together in the denominator,
-
and the 1 minus s needs to stay together
-
in its numerator. Let's try this again. Alright, looks
-
like Amdalh/s speed up is working, but we still
-
need to do the cross ratio. I'll try
-
doing this sort of naively again. I've written this
-
as it looks but flattened it onto one line.
-
So, if I return this quantity and I compile
-
I'm getting an error, unexpected type, required class found
-
value. This error probably doesn't make a whole lot
-
of sense to yet but this is a little
-
hint of what's to come. A pair of parentheses
-
like this immediately followed by some other quantity has another meaning.
-
In particular, if you put a type in here instead of
-
an expression like a minus c. What I need to indicate
-
is that I want to multiply, and I'll need to do it
-
over here as well. And now if I try to run
-
the tester again I'm definitely not getting right answers for the cross
-
ratio. This is because when we have a bunch of operators
-
with the same precedents Travel will just start from the left and
-
move to the right so instead of computing a
-
minus c times b minus d over in parentheses b
-
minus c times a minus d. This will do a minus c times b minus d divided by b
-
minus c and multiply that entire quantity by a
-
minus d. What I need to do here, is group
-
the denominator. I could also group the numerator, if
-
I really wanted to but it wouldn't make much of
-
a difference. If I compile again, and run the tester, I've
-
now got two methods working right. Now for the average. I might try retuning
-
the sum of all of these, all divided by four. Let's
-
see how this works. If I run the tester, it looks like it works in one case, but
-
not in the other. Let's look at the second
-
case. I'm going to go read inside of the tester.
-
The case that isn't working is when we try
-
to take the average of 3, 4, 3, and 3.
-
We should, in fact, expect 3.25, but it looks
-
like we're losing the decimal. Java is interpreting this as
-
integer division, because 4 is an integer, and a,
-
b, c and d are all declared as integers. There
-
are a few ways I could fix this. I could write 4 as 4., or 4.0 And then it would
-
get the right answer here. Or I could actually change
-
all of these ints to doubles, and then I wouldn't actually
-
need to specify that the 4 was a double. I
-
can compile this and run the tester, and it still works.
-
That's because if any of these variables are a double,
-
this whole expression in parentheses is going to come out as a
-
double, and a double divided by an int doesn't need
-
to be done with integer division, that calls for regular division.
-
This is a really easy thing to mix up. It
-
looks right to us, but the computer's going to read
-
it wrong and the compiler won't warn you. When we
-
ran this with all ints and no doubles we saw that
-
it ended up being a run time error. This is
-
a good example of why it's helpful to think about what
-
you want your answer to be beforehand. Calculate a couple
-
examples and then write your code. Good book on this quiz.