James Liang - ICS Blog

ICS4U Blogging

Methods - Multiplayer Coding

Multiplayer Coding

/*
Create a method that calculates addition, subtraction, multiplication and division on two numbers (double) and returns the result.

Create an overloaded method that also does the same, except for an array (double).
  Add Javadoc comments for each method
*/
import java.util.Scanner;

class Main {
  /**
  * This is the main method. We reference all the other
  * methods from this one. It takes user input and also
  * outputs text.
  *
  * @author James Liang, Anishan Siva
  * @version 1.0
  * @since 2021-10-25
  */
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String type = "+";
    double result;
    double a = 3.0;
    double b = 4.0;
    double array [] = {3.4,5.1,8.3};

    System.out.println("What operation? (+,-,*,/)");
    type = input.nextLine();
    result = simpleCalc(type, a, b);
    System.out.println(result);
    result = simpleCalc(type, array);
    System.out.println(result);
  }
  /**
  * This method does a simple calculation based on the 
  * parameters that are passed through. It uses the
  * type of calculation that the user inputted and the
  * two predefined variables.
  *
  * @author James Liang, Anishan Siva
  * @version 1.0
  * @since 2021-10-25
  */
  public static double simpleCalc(String operation, double numOne, double numTwo)
  {
    double calculation = 0;
    if (operation.equals("+"))
    {
      System.out.println("Addition");
      calculation = numOne+numTwo;
    }
    else if (operation.equals("-"))
    {
      System.out.println("Subtraction");
      calculation = numOne-numTwo;
    }
    else if (operation.equals("/"))
    {
      System.out.println("Division");
      calculation = numOne/numTwo;
    }
    else if (operation.equals("*"))
    {
      System.out.println("Multiplication");
      calculation = numOne*numTwo;
    }
    return calculation;
  }
  /**
  * This method does a calculation based on the 
  * parameters that are passed through. It uses the
  * type of calculation that the user inputted and the
  * predefined array. It is an overloaded method.
  *
  * @author James Liang, Anishan Siva
  * @version 1.0
  * @since 2021-10-25
  */
 public static double simpleCalc(String operation, double numArray [])
 {
   double calculation = 0;
    if (operation.equals("+"))
    {
      System.out.println("Addition");
      calculation = numArray[0];
      for (int temp = 1; temp < numArray.length; temp++)
        {
          calculation += numArray[temp];
        }
    }
    else if (operation.equals("-"))
    {
      System.out.println("Subtraction");
      calculation = numArray[0];
      for (int temp = 1; temp < numArray.length; temp++)
      {
        calculation -= numArray[temp];
      }
    }
    else if (operation.equals("/"))
    {
      System.out.println("Division");
      calculation = numArray[0];
      for (int temp = 1; temp < numArray.length; temp++)
      {
        calculation /= numArray[temp];
      }
    }
    else if (operation.equals("*"))
    {
      System.out.println("Multiplication");
      calculation = numArray[0];
      for (int temp = 1; temp < numArray.length; temp++)
      {
        calculation *= numArray[temp];
      }
    }
    return calculation;
 }
}

Post on your Learning Blog with a DETAILED EXPLANATION of your code, try to use all the programming TERMINOLOGY from this topic

First, since I wanted to read input from the user, I imported the Scanner utility. I then stated my class name and then wrote a Javadoc comment to explain the method that would follow. I like to write my main method at the top, so that is where I placed it. I initialized the scanner and then initialized the type variable as “+”, which would be the the default operation. I found that if I didn’t initialize variables with a method, the program would complain and not function. I then created more variables such as result and the inputs. I then prompt the user for the operation that they want to execute. The type variable is set to the operation that they pick. I then run the simpleCalc methods that deals with the a and b variables and set the result variable as the output. I print out the result and then move onto the overloaded method that deals with the array. I print the result of that method as well.

Moving onto the simpleCalc method, I first wrote a Javadoc comment to explain what the method does. I then created the method header with double as the return type and simpleCalc as the name. I left the access modifier and the class method as public static. I passed through the three parameters that were needed in the method. Inside the method, I initialize a double variable called calculation and set it to zero. I then have if and if else statements to check which operation the method would be executing. Inside each statement is the output of which calculation it is doing, it then sets the calculation variable to the result of the operation. Finally, the calculation variable is returned.

The next method is the overloaded simpleCalc method. I wrote my Javadoc comment to explain what the use of this was. I then created my method header with the same access modifier and class method as the previous. Since I wanted to do an overloaded method, the return data type and method name needed to be the same as the other. The parameters this time were different, as I took in the type of operation and an array. Like before, I initialized the calculation variable and set it to zero. I used similar if and else if statements to check the type of operation that was chosen. In each statement I set the calculation variable to the first value of the array. I then used a for loop to go through the rest of the values in the array. Since I already used the first value in the array, I started the loop at one. Inside each for loop, the calculation variable is changed with math operators. Once, the loop is done, the calculation is returned.