You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2.1 KiB

IntOperations

Instructions

Create a file IntOperations.java.

Write a function addTwoIntegers that returns the sum of two integers passed as parameter. Write a function substractTwoIntegers that returns subtraction of two integers passed as parameter. Write a function multiplyTwoIntegers that returns the multiplication of two integers passed as parameter. Write a function divideTwoIntegers that returns the Euclidian division of two integers passed as parameter. We ignore the divide by 0 case.

Expected Functions

public class IntOperations {
    public static int addTwoIntegers(int a, int b) {
        // your code here
    }
    public static int subtractTwoIntegers(int a, int b) {
        // your code here
    }
    public static int multiplyTwoIntegers(int a, int b) {
        // your code here
    }
    public static int divideTwoIntegers(int a, int b) {
        // your code here
    }
}

Usage

Here is a possible ExerciseRunner.java to test your function :

public class ExerciseRunner {
    public static void main(String[] args) {
        System.out.println("Add");
        System.out.println(IntOperations.addTwoIntegers(1, 2));
        System.out.println(IntOperations.addTwoIntegers(15, 68));

        System.out.println("Subtract");
        System.out.println(IntOperations.subtractTwoIntegers(4, 1));
        System.out.println(IntOperations.subtractTwoIntegers(10, 23));

        System.out.println("Multiply");
        System.out.println(IntOperations.multiplyTwoIntegers(3, 6));
        System.out.println(IntOperations.multiplyTwoIntegers(12, 11));

        System.out.println("Divide");
        System.out.println(IntOperations.divideTwoIntegers(8, 2));
        System.out.println(IntOperations.divideTwoIntegers(13, 4));
    }
}

and its output :

$ javac *.java -d build
$ java -cp build ExerciseRunner
Add
3
83
Subtract
3
-13
Multiply
18
132
Divide
4
3
$

Notions

Primitive Links
Operations