Java homework help. I seriously need something to start with.?

Write a class named Calculation.java that has 5 class methods to calculate the 1) addition, 2) subtraction, and 3) multiplication of two integer numbers, and calculate the 4) maximum number and 5) minimum number of three integer numbers. Write a class named TestCalculation.java to test the class methods in Calculation. Using Math class is not allowed. Following is an example of possible output in the command-line window

Please give me something to start with or the actual code if possible. I will rate best asnwer.

The input is 4 and 2, the addition result is 6

The input is 1 and 9, the subtraction result is -8

The input is 3 and 8, the multiplication result is 24

The input is 2, 3, and 6, the minimum number is 2

The input is 3, 12, and 7, the maximum number is 12

3 Answers

Relevance
  • John
    Lv 7
    8 years ago
    Favorite Answer

    public class TestCalculation {

    public static void main(String[] args) {

    Calculation calculator = new Calculation();

    int x, y = 0;

    x = 4;

    y = 2;

    int result = calculator.add(x, y);

    String message = "The input is %d and %d, the addition result is %d.";

    assertEquals(6, result);

    CalculationConsole.printResult(x, y, result, message);

    // calculator.subtract(1, 9);

    // calculator.multiple(3, 8);

    int[] numbers = new int[3];

    numbers[0] = 2;

    numbers[1] = 3;

    numbers[2] = 6;

    result = calculator.min(numbers);

    message = "The input is %s, the minimum number is %d.";

    assertEquals(2, result);

    CalculationConsole.printListResult( numbers, result, message);

    // calculator.max(numbers);

    System.exit(0);

    }

    private static void assertEquals(int expect, int actual) {

    if (expect != actual) {

    throw new RuntimeException(String.format(

    "Expect '%d', but was '%d'.", expect, actual));

    }

    }

    }

    class Calculation {

    public int add(int x, int y) {

    return x + y;

    }

    public int min(int[] numbers) {

    int result = numbers[0];

    for (int i : numbers) {

    if (result > i)

    result = i;

    }

    return result;

    }

    }

    final class CalculationConsole {

    private CalculationConsole() {

    }

    public static void printResult(int x, int y, int result, String message) {

    System.out.printf(message, x, y, result);

    System.out.println();

    }

    public static void printListResult(int[] numbers, int result, String message) {

    String number = getNumbersList(numbers);

    System.out.printf(message, number, result);

    System.out.println();

    }

    private static String getNumbersList(int[] numbers) {

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < numbers.length - 1; i++) {

    sb.append(", ").append(numbers[i]);

    }

    sb.append(", and ").append(numbers[numbers.length - 1]);

    sb.delete(0, 2);

    return sb.toString();

    }

    }

  • 8 years ago

    90% of programming is grinding away, reading API and method info and solving problems.

    If someone posts a code solution for you, you will NEVER be able to do this on your own, and this is about as basic as it gets.

    This is an extremely simple task.

    Just look up(and study) the following things.

    System.out.println()

    and java basic arithmetic.

    If you can't make it through this assignment then I suggest you attend class, or look into veterinary assistant schools.

  • 8 years ago

    I don't really know Java well, in fact I only spent 2 days on Java but I hope this helps:

    This HW requires OOP and all you have to do is create classes and within them have methods and properties. Then, your functions need parameters so the user can plug into the function. Then just use mathematical operators in the functions.

Still have questions? Get your answers by asking now.