Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question regarding interfaces: A) What is an Interface? (we’re specifically talk

ID: 3907701 • Letter: Q

Question

Question regarding interfaces:

A) What is an Interface? (we’re specifically talking about the class-like construct, not the public interface described in Encapsulation)

B) Given this class:

public class Card implements Comparable

{

       protected char suit;

       protect int value;

       public int getValue() {return this.value;} //Ace = 1, J = 11, Q = 12, K = 13

       public char getSuit() {return this.suit;} // C – clubs, D – Diamonds, H – Hearts, S - Spades

}

Give the method and definition that fulfills the Comparable interface.

NOTE: The rules for comparison are:

One card is less than the other if the value is numerically less (Ace = 1)

If they have the same value then C < D < H < S

Two cards are equal if they have the same value and same suit.

Explanation / Answer

an interface is a collection of abstract methods and which can be implemented by its sub class

Q2

the method will be

public int compareTo(Card c)

{

if(this.value>c.getValue())return 1;

else if(this.value<c.getValue())return -1;

else

{

char c1=c.getSuit();

if(c1>this.suit)return -1;

else if(c1<this.suit) return 1;

else return 0;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote