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

Question 1 1 pts /* Something that can be compared to other Comparable things pu

ID: 3746549 • Letter: Q

Question

Question 1 1 pts /* Something that can be compared to other Comparable things public interface Comparable f Compare this object with the other object * returns -1if this comes before other 0 if this and other are the same 1 if this comes after other public int compareTo(Comparable other); public class Cat implements Comparable private String breed; private double cutenessRating; public Cat(String breed, double cutenessRating) this.breed - breed; this.cutenessRating-cutenessRating; @0verride public int compareTo(Comparable other) Cat a (Cat) other if (this.cutenessRating a.cutenessRating) return 1; else return0 @0verride public String toString) return "Cat" "breed" + breed + ", cutenessRating-" cuten essRating

Explanation / Answer

Hi,
Thank you for the opportunity to help you with your assignment.

The following are the results for Q #1 and #2/.

#1

RetValue: -1
RetValue: 1
RetValue: 1
RetValue: 1
RetValue: -1

#2

B
A
C
A
ERROR (underflow)

I have included the code for Q #1.

The output and screenshot is at the end of this answer.

If you have any queries, need changes in the code or need help in following the code, leave me a comment.

I will reply within reasonable time.

If this answer helps you with your assignment, do upvote it.
Your vote will motivate me to work better.

The following is the code
//File: Comparable.java


public interface Comparable {
public int compareTo(Comparable other);
}

//Cat.java


public class Cat implements Comparable {
private String breed;
private double cutenessRating;
public Cat(String breed, double cutenessRating) {
this.breed = breed;
this.cutenessRating = cutenessRating;
}
@Override
public int compareTo(Comparable other) {
int retValue = 0;
Cat a = (Cat) other;
if(this.cutenessRating < a.cutenessRating) retValue = -1;
else if(this.cutenessRating > a.cutenessRating) retValue = 1;
else retValue = 0;
System.out.println("RetValue: " + retValue);
return retValue;
}
@Override
public String toString() {
return "Cat{ breed=" + breed + ", cutenessRating=" + cutenessRating + "}";
}
}

//SortingInserter.java


public class SortingInserter {
public static void insertSorted(Comparable[] sorted, int currentSize, Comparable newElement) {
sorted[currentSize] = newElement;
int i = currentSize;
while(i > 0 && sorted[i-1].compareTo(sorted[i]) > 0) {
Comparable larger = sorted[i-1];
Comparable smaller = sorted[i];
sorted[i-1] = smaller;
sorted[i] = larger;
i--;
}
}
public static void main(String[] args) {
Comparable[] myCats = new Cat[10];
SortingInserter.insertSorted(myCats, 0, new Cat("Siamese", 5.1));
SortingInserter.insertSorted(myCats, 1, new Cat("Tabby", 6));
SortingInserter.insertSorted(myCats, 2, new Cat("GrumpyCat", 1));
SortingInserter.insertSorted(myCats, 3, new Cat("Sylvester", 5.8));
}
}


And finally, this is the output when the code is executed
//Output:

#1

RetValue: -1
RetValue: 1
RetValue: 1
RetValue: 1
RetValue: -1

#2

B
A
C
A
ERROR (underflow)

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