Question

Programming Language: Java 1. Write a class called Pair that stores a pair of numbers in...

Programming Language: Java

1. Write a class called Pair that stores a pair of numbers in private fields. Include a two-arg constructor, mutators and accessors. Override the equals method so that a Pair (1,2) would be considered equal to Pair (2,1). Write an appropriate toString method and make the toString method non-overrideable.

2. Write an interface called Comparable that includes a method isGreater, isLesser, and isSame. These methods should each take a Pair object as a parameter.

3. Write an interface called Nameable that includes method setName and method getName.

4. Write a child class of Pair called GoodPair that implements the two interfaces above. The isSame method should consider Pair (1,2) to be the same as Pair (1,2) but not the same as Pair (2,1). You can interpret isGreater as returning true if the sum of the two elements of a pair is greater than the sum of the two element of the other pair that it is being compared to, and you can interpret isLesser similarly.

Use the following code in the demo class:

public static void main(String[] args)
{
       Pair pairA = new Pair(2, 1);
       Pair pairB = new Pair(1, 2);
       System.out.println("The pair A = " + pairA + "\nThe pair B = " + pairB);
       if(pairA.equals(pairB))
           System.out.println("The pair A is equal to pair B.");
       else
           System.out.println("The pair A is NOT equal to pair B.");

       GoodPair gPairA = new GoodPair(2, 1);
       GoodPair gPairB = new GoodPair(1, 2);
       System.out.println("The good pair A = " + pairA + "\nThe good pair B = " + pairB);
       if(gPairA.isSame(gPairB))
           System.out.println("The good pair A is the same as good pair B.");
       else
           System.out.println("The good pair A is NOT the same as good pair B.");

       if(gPairA.isGreater(gPairB))
           System.out.println("The good pair A is greater than good pair B.");
       else
           System.out.println("The good pair A is NOT greater than good pair B.");
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer.

Thank You !

===========================================================================


public class Pair {

    private int x;
    private int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pair pair = (Pair) o;
        return x == pair.x &&
                y == pair.y;
    }
    

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }

    public static void main(String[] args) {
        Pair pairA = new Pair(2, 1);
        Pair pairB = new Pair(1, 2);
        System.out.println("The pair A = " + pairA + "\nThe pair B = " + pairB);
        if (pairA.equals(pairB))
            System.out.println("The pair A is equal to pair B.");
        else
            System.out.println("The pair A is NOT equal to pair B.");

        GoodPair gPairA = new GoodPair(2, 1);
        GoodPair gPairB = new GoodPair(1, 2);
        System.out.println("The good pair A = " + pairA + "\nThe good pair B = " + pairB);
        if (gPairA.isSame(gPairB))
            System.out.println("The good pair A is the same as good pair B.");
        else
            System.out.println("The good pair A is NOT the same as good pair B.");

        if (gPairA.isGreater(gPairB))
            System.out.println("The good pair A is greater than good pair B.");
        else
            System.out.println("The good pair A is NOT greater than good pair B.");
    }
}

================================================================

public interface Comparable {

    public boolean isGreater(Pair pair);
    public boolean isLesser(Pair pair);
    public boolean isSame(Pair pair);
}

================================================================

public interface Nameable {

    public void setName(String name);
    public void getName();
}

================================================================

public class GoodPair extends Pair implements Comparable,Nameable {


    public GoodPair(int x, int y) {
        super(x, y);
    }

    @Override
    public boolean isGreater(Pair pair) {
        return (getY()+getX())>(pair.getY()+pair.getX());
    }

    @Override
    public boolean isLesser(Pair pair) {
        return (getY()+getX())<(pair.getY()+pair.getX());
    }

    @Override
    public boolean isSame(Pair pair) {
        return getX()==pair.getX() && getY()==pair.getY();
    }

    @Override
    public void setName(String name) {

    }

    @Override
    public void getName() {

    }
}

================================================================

Add a comment
Know the answer?
Add Answer to:
Programming Language: Java 1. Write a class called Pair that stores a pair of numbers in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please who can help with this question in Java 1.  Write a class called Pair that...

    please who can help with this question in Java 1.  Write a class called Pair that stores a pair of numbers in private fields. Include a two-arg constructor, mutators and accessors. Override the equals method so that a Pair (1,2) would be considered equal to Pair (2,1). Write an appropriate toString method and make the toString method non-overrideable. 2.  Write an interface called Comparable that includes a method isGreater, isLesser, and isSame. These methods should each take a Pair object...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • Registration Language Java Step 1: Design a class called Student. The Student class should contain the...

    Registration Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration”...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still...

    ATM Revisited In Java, design a subclass of the Account class called GoldAccount. It is still an Account class, however it has an additional field and some additional functionality. But it will still retain all the behavior of the Account class. Write a class called GoldAccount. This class will have an additional private field called bonusPoints. This field will require no get or set methods. But it will need to be initialized to 100 in the constructor. Thereafter, after a...

  • You are to write a class called Point – this will represent a geometric point in...

    You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following: Data:  that hold the x-value and the y-value. They should be ints and must be private. Constructors:  A default constructor that will set the values to (3, -5)  A parameterized constructor that will receive 2 ints (x then y) and set the data to what is...

  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT