Define a class named Pair where the data(state) part contains two double-typed
numbers, num1 and num2. The method(behaviour) part contain the following methods:
o getSum() – returns the result of adding num1 with num2
o getdifference() – returns the result of substracting num2 from num1
o getProduct() - returns the multiplication of num1 with num2
o getAverage() – returns the average of num1 & num2
o getMax() - returns the larger of the two numbers
o getMin() – returns the smaller of the two numbers
You also need to define a constructor method as described below:

public Pair(double aFirst, double aSecond) {...}
/*
Constructs a Pair object
parameter aFirst the first value of the pair
parameter aSecond the second value of the pair
/

Then write a program named PairTest that reads in two numbers (from standard
input) constructs a Pair object, invokes all of its methods and prints the results.

Dani AI

Generated

's point about including an attempt is correct. Below is a concise, ready-to-run implementation of a Pair class plus a small PairTest that reads two doubles from standard input and prints sum, difference, product, average, max and min. Note: the assignment used the method name getdifference (lowercase "d"); Java naming convention would prefer getDifference. If an exact method name is required by a grader, keep the original casing.

public class Pair {
    private double num1;
    private double num2;

    public Pair(double aFirst, double aSecond) {
        this.num1 = aFirst;
        this.num2 = aSecond;
    }

    public double getSum() {
        return num1 + num2;
    }

    public double getdifference() {
        return num1 - num2;
    }

    public double getProduct() {
        return num1 * num2;
    }

    public double getAverage() {
        return (num1 + num2) / 2.0;
    }

    public double getMax() {
        return Math.max(num1, num2);
    }

    public double getMin() {
        return Math.min(num1, num2);
    }
}
import java.util.Scanner;

public class PairTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter two numbers separated by space: ");
        if (!sc.hasNextDouble()) { System.out.println("Invalid first number."); sc.close(); return; }
        double a = sc.nextDouble();
        if (!sc.hasNextDouble()) { System.out.println("Invalid second number."); sc.close(); return; }
        double b = sc.nextDouble();
        sc.close();

        Pair p = new Pair(a, b);
        System.out.printf("Sum: %f%n", p.getSum());
        System.out.printf("Difference: %f%n", p.getdifference());
        System.out.printf("Product: %f%n", p.getProduct());
        System.out.printf("Average: %f%n", p.getAverage());
        System.out.printf("Max: %f%n", p.getMax());
        System.out.printf("Min: %f%n", p.getMin());
    }
}

Quick troubleshooting notes:

  • Method names are case-sensitive; match the expected names if automated grading is used.
  • Use hasNextDouble() (as shown) to avoid InputMismatchException when reading from standard input.
  • Math.max/Math.min simplifies getMax/getMin.
  • For monetary or high-precision needs, prefer BigDecimal instead of double.

This implementation is minimal and intended for study and adaptation.

Please acquaint yourself with the rules taking particular note of "do provide evidence of having done some work yourself if posting questions from school or work assignments" and then come back with the appropriate additional information such as what problems you are having with your code, and include that code, so people can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.