Blog on learning

OOP Java P1 Wk3

|

UPDATE

In MOOC’s Week 1 and Week 2 exercises, I did not capitalize classes. I learned that classes should always be capitalized. I don’t want to change all Week 1 & 2 classes, too much of work. Starting from this Week 3 and on, classes are capitalized.

Starting OOP Java Week 3!

I will be covering from Exercise #43 to Exercise #46 in this post. I am going to skip some easy exercises and focus on what might be needed to reviewed again for other uses in the future. Let’s start with the easiest one!

Sum of numbers

The purpose of this exercise is to get us comfortable with using method’s parameter. In this Exercise #43, it asks us to create a sum method which will automatically add up any numbers given (by users) using parameter.

public class SumOfNumbers {

    public static int sum(int number1, int number2, int number3, int number4) {
        return number1 + number2 + number3 + number4;
    }

    public static void main(String[] args) {
        int answer = sum(4, 3, 6, 1);
        System.out.println("Sum: " + answer);
    }
}

// Sum: 14

See how that works? The parameter sum(int number1, int number2, int number3, int number4) is like either variables in formula waiting to happen, or placeholders for yet inputs. We will be using parameters often in this week! This week is building up on making methods and using parameters. Let’s see another example of using parameter.

Greatest

Exercise #45 teaches us how to identify the largest number among given numbers. I decided to do this quickly and it works as it should.

public static int greatest(int number1, int number2, int number3) {
    if (number1 > number2 && number1 > number3){
        return number1;
    } else if (number2 > number1 && number2 > number3) {
        return number2;
    } else {
        return number3;
    }
}

public static void main(String[] args) {
  int result = greatest(2, 7, 3);
  System.out.println("Greatest: " + result);
}

// Greatest: 7

The conditional statements in coding above goes like:

  • if number1 larger than number2 and number3, its number1
  • if number2 larger than number1 and number3, its number2
  • neither above, it’s number3 that’s the largest

There’s a better way to do this and I should have known from my experience with JavaScript programming.

int greatest = number1;
if (number2 > greatest) {
   greatest = number2;
}
if (number3 > greatest) {
   greatest = number3;
}
return greatest;

What coding above does:

  • assigning greatest to number1
  • if number2 is greater, then assign greatest to number2
  • likewise with number3
  • if neither is greater than number1 then return greatest.

The latter method is much cleaner and more applicable to other challenges. We will see similarity of this later on.

Average of Given numbers

We learn how methods are being called and used in other methods. Exercise #46 has two methods and one main methods. The first method is being called into the second method and the main method calls the second method.

public static int sum(int number1, int number2, int number3, int number4) {
    int sum = number1 + number2 + number3 + number4;
    return sum;
}

public static double average(int number1, int number2, int number3, int number4) {
    int sum = sum(number1, number2, number3, number4);
    double average = 1.0 * sum / 4;
    return average;
}

public static void main(String[] args) {
    double result = average(4, 3, 6, 1);
    System.out.println("Average: " + result);
}

The sum method has four parameters, and they are being added up and returned as sum. The average method also has the exact same four parameters, but receives sum from sum method to calculate the average.

It might be confusing to see int sum = sum(int number1, number2, number3, number4) in average method when already have int sum = number1 + number2 + number3 + number4; in sum method. Both of these are not exactly the same. sum method is simply an instruction of how to add up numbers together. sum in average method is on how to use sum method WITH given numbers later on in the main method.

double average = 1.0 * sum / 4 is not exactly the best way to do the average, because what if somebody want to do 5 or 7 parameters? I am sure we will learn how to do average properly later on in different exercise. Lastly, the main method calls average method to do the sum and average with given numbers.

Ending this part of the Week 3.

Posts on Week 3 part 2, and part 3 coming up next!

Comments