views:

84

answers:

2

First off I apologize for not elaborating and clarifying the question first, so I will do that right now.

I am currently trying to complete a a lab exercise (topic is on earthquakes) that requires UML class diagram and pseudocode.

one of the method required is named the determineclassification()

so for the pseudocode it is required that you have an if then else statement for the method above.

example

magnitude
0-3
4-6

classification
little
medium

determineclassification()

If magnitude 0 > and magnitude < 3

then

classification = little

elseif magnitude 4 > and magnitude < 6

then

classification = medium

and vice versa

I was wondering if the method that I am using right is the correct way to create a if-then-else statement.

+1  A: 

Your question could be a lot clearer, but since it'll look similar in most languages I'll just go with it.

if (age > 10 && age < 18) {
    person = young;
}
else if (age >= 18) {
    person = old;
}

I assume people >= 100 are also old, so no reason for an upper bound. Using < 18 and > 17 may work if using int types, but it's safer and clearer just to use >= on the second range.

Earthquake version:

Now that you've change it to this earthquake example, you're still messing up your edge cases. Where does 3.5 fit? 4? 6?

determineClassification(magnitude) {
    if (magnitude < 4)
        return "little"

    if (magnitude < 6)
        return "medium"

    return "large"
}
bemace
A: 

You have pretty good pseudo code, there is nothing wrong in it. There is no need for an upper bound in the second if unless you want to create another qualifier for people whose age > 100, e.g. ancient or something.

fastcodejava