Question

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 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

Please let me know if you need more information :-
==========================================

public class PairTester {
   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.");
   }
}

interface Comparable {
   public boolean isGreater(Pair other);

   public boolean isLesser(Pair other);

   public boolean isSame(Pair other);
}

interface Nameable {
   public boolean setName();

   public boolean getName();
}

class Pair {
   private int p1;
   private int p2;

   public Pair(int p1, int p2) {
       super();
       this.p1 = p1;
       this.p2 = p2;
   }

   public int getP1() {
       return p1;
   }

   public void setP1(int p1) {
       this.p1 = p1;
   }

   public int getP2() {
       return p2;
   }

   public void setP2(int p2) {
       this.p2 = p2;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Pair other = (Pair) obj;
       // Override the equals method so that a Pair (1,2) would be considered equal to
       // Pair (2,1)
       return ((p1 == other.p1 && p2 == other.p2) || (p1 == other.p2 && p2 == other.p1));
   }

   @Override
   public final String toString() {
       return "Pair [p1=" + p1 + ", p2=" + p2 + "]";
   }

}

class GoodPair extends Pair implements Comparable, Nameable {

   public GoodPair(int p1, int p2) {
       super(p1, p2);
   }

   @Override
   public boolean setName() {
       return false;
   }

   @Override
   public boolean getName() {
       return false;
   }

   @Override
   public boolean isGreater(Pair other) {
       // 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
       return (this.getP1() + this.getP2() > other.getP1() + other.getP2());
   }

   @Override
   public boolean isLesser(Pair other) {
       // You can interpret isLesser as returning true if the sum of the two elements
       // of a pair is lesser than the sum of the two element of the other pair that
       // it is being compared to
       return (this.getP1() + this.getP2() < other.getP1() + other.getP2());
   }

   @Override
   public boolean isSame(Pair other) {
       // The isSame method should consider Pair (1,2) to be the same as Pair (1,2) but
       // not the same as Pair (2,1).
       return (this.getP1() == other.getP1() && this.getP1() == other.getP2());
   }

}
=======
OUTPUT:-

===

The pair A = Pair [p1=2, p2=1]
The pair B = Pair [p1=1, p2=2]
The pair A is equal to pair B.
The good pair A = Pair [p1=2, p2=1]
The good pair B = Pair [p1=1, p2=2]
The good pair A is NOT the same as good pair B.
The good pair A is NOT greater than good pair B.

===

The pair A = Pair [p1=2, P2=1] The pair B = Pair [p1=1, p2=2] The pair A is equal to pair B. The good pair A = Pair [p1=2, p2

===

public class PairTester { public static void main(String[] args) { Pair pairA = new Pair(2, 1); Pair pairB = new Pair(1, 2);

==

this.p1 = p1; this.p2 = p2; public int getP1() { return p1; public void setP1(int pl) { this.p1 = p1; public int getP2() { re

==

public boolean setName() { return false; @Override public boolean getName() { return false; @Override public boolean isGreate

==

===

Thanks

Add a comment
Know the answer?
Add Answer to:
please who can help with this question in Java 1.  Write a class called Pair that...
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
  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • 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,...

  • Can some please help me to answer these two questions. Write a program in Java create...

    Can some please help me to answer these two questions. Write a program in Java create a class diagram fragment with two classes: User and Patient. Patient is a sub-class of User. User has a method getName(), which is both over-loaded and over-ridden in the subclass In Java write an abstract class called User. User has one abstract method called authenticate, which takes two string parameters. Write a concrete sub-class of User called Administrator with an implementation of the authenticate...

  • 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...

  • 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...

  • 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...

  • 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...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • JAVA 1.            Given the following class definition, what are the contents of the fields x and...

    JAVA 1.            Given the following class definition, what are the contents of the fields x and y of the object p ?    class MyPoint {      int x;      int y; public MyPoint (int x, int y){        x = x;        y = y;      } --------------------------------------- MyPoint p = new MyPoint( 100, 88 ); 2.            static and non-static members What would happen if you tried to compile and run the following code ? public class Driver {...

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
ADVERTISEMENT