Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1A. The class Client has a field cname that is of String type. It also has a field score of int type. B) The natural

1A. The class Client has a field cname that is of String type. It also has a field score of int type.

B) The natural order of Client is by name (alphabetical order). Write the code of the Client class, make it implements Comparable interface, and write the code of the compareTo method by comparing names. [Write correct class declaration, and implement the required method. You can skip the rest of the code for the class.]

C) Write the complete code of a Comparator class ByScore for this Client class. The ByScore class has the compare method comparing scores of two clients, and set a client of higher score ahead of the one of lower score.

D. Change and complete code the BySuitFirst class for Card type. You may use the helper methods.

Card class

Face-value first. The class is with some helper methods.

//helper method

int compFace(Card other) { //A before K before Q before J before 10..2

if (this.face.equals(other.face)) return 0;

else if (this.face.equals("A")) return -1;

else if (other.face.equals("A")) return 1;

else if (this.face.equals("K")) return -1;

else if (other.face.equals("K")) return 1;

else return other.num%13 - this.num%13; }

//helper method

int compSuit(Card other) {//spade before heart before diamond before club

if (this.suit.equals(other.suit)) return 0;

else if (this.suit.equals("spade")) return -1;

else if (other.suit.equals("spade")) return 1;

else if (this.suit.equals("heart")) return -1;

else if (other.suit.equals("heart")) return 1;

else if (this.suit.equals("diamond")) return -1;

else if (other.suit.equals("diamond")) return 1;

else return 0; }

//default order: by-face-first

public int compareTo(Card other) {

if (this.compFace(other) != 0) return

this.compFace(other);

else return this.compSuit(other); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions