Sunteți pe pagina 1din 2

1 Common Mistakes in Take Home Lab#1

After going over the solutions of the students for take-home lab no.1, we
have compiled a short list of mistakes and comments. The mistakes are not
always ‘mistakes’. They may just be not-so-good programing practices.
The list has been divided according to the three questions in the take
home lab, however, some of the mistakes are applicable to more than one
question.

1.1 Question 1
.
A: Be familiar with the formattings available in System.out.printf(). For
example, when we say System.out.printf(%.2f, x), it will print the value of x,
correct to two decimal places. That is something that we would want to do
often. No one really wants to see “1.00000”. Much more often we just want
to see the number correct to a few decimal places. printf() several kinds of
formattings.
B: Try to think in terms of loops (for, while, do). If the user can enter a
maximum of three sets of data, it maybe better to write a few lines of code
inside some loop such as for(i = 0 ; i < 3; i++), rather than writing the
same lines of code multiple times. Yes it will work. But it can be harder to
debug and update, as the code would be so much longer.

1.2 Question 2
A: There was a mistake in the skeleton file of MyCircle.java. In the method
setRadius(double radius), there was the line radius = radius. Such a state-
ment, we hope, would immediately catch your attention. You should ask
radius = radius???. The left hand side is the same as the right hand side,
there must be something wrong.
In the skeleton file, two variables were given the same name. One was
the private class variable radius; and the other was the input from the user,
radius. One could correct it by either changing the name of one of the
variables, OR one would change radius = radius to this.radius = radius.
It’s a good practice to avoid ambiguity. For example,
MyCircle MyCircle = new MyCircle() will work. But I would prefer not to
give the object the same name as the class, so that the code is easier to read
and understand.

1
B: Be familiar with some of the common methods available from the
java.lang.Math package. Such as pow, to take the power; abs to take the
absolute value. Math.abs(-3) would return the number 3. Also, pow(radius,
2) will return the number (radius)2 . These will help you to code more
efficiently.

1.3 Question 3
A: In this question, amongst other things, we were required to print the
maximum difference between any two consecutive trips.
In order to compute this we do not need to store the distances of all the
trips in an array. All we need is the knowledge of (a) The distance of the
current trip (currDist) (b) the distance of the previous trip (prevDist) (c)
the maximum difference between any two trips so far (maxDiff). Then:

if (M ath.abs(currDist − prevDist) > maxDif f )


maxDif f = M ath.abs(currDist − prevDist)

In words: If currDist − prevDist is more than any difference I have seen


so far, then currDist − prevDist is my new maximum.

S-ar putea să vă placă și