In the Card1 class, the compareTo() method orders cards by first comparing the card’s suits and then ranks if needed (when there is a tie). The suits and ranks are ordered as follows: suits: The suits will be ordered diamonds © < clubs ® < hearts ™ < spades ́ ranks: The ranks will be ordered (READ CAREFULLY) 2 < 3 < · · · < 9 < 10 < Jack < King < Queen < Ace In the Card2 class, the compareTo() method orders cards solely by rank as follows: ranks: The ranks will be ordered (READ CAREFULLY) Ace < 2 < 3 < · · · < 9 < 10 < Jack = King = Queen ︸ ︷︷ ︸ considered equal

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 In the Card1 class, the compareTo() method orders cards by first comparing the card’s suits
and then ranks if needed (when there is a tie). The suits and ranks are ordered as follows:
suits: The suits will be ordered
diamonds © < clubs ® < hearts ™ < spades ́
ranks: The ranks will be ordered (READ CAREFULLY)
2 < 3 < · · · < 9 < 10 < Jack < King < Queen < Ace

In the Card2 class, the compareTo() method orders cards solely by rank as follows:
ranks: The ranks will be ordered (READ CAREFULLY)
Ace < 2 < 3 < · · · < 9 < 10 < Jack = King = Queen
︸ ︷︷ ︸
considered equal

Now suppose that you have an array (AbstractCard[]) that contains both Card1 objects and
Card2 objects and you want to sort the array using bubble sort. The pseudocode for bubble sort
is as follows:
procedure bubbleSort (A : list of sortable items)
n := length(A)
print "initial:", the list A
repeat
swapped := false
for i := 1 to n-1 inclusive do
/+ if this pair is out of order +/
if A[i-1] > A[i] then
/* swap them and remember something changed +/
swap(A[i-1), A[i])
// <- this is the red line
swapped :- true
end if
end for
n := n - 1
print "it {i}", the list A
until not swapped
end procedure
The line in red (above) is problematic for a couple of reasons. One issue is that this line can
translate into java in two ways:
if( A[i-1].compareTo (A[i]) > 0 ){
if( A[i].compareTo(A[i-1]) < 0 ){
version 1:
version 2:
If we run the bubble sort algorithm using version 1 of the red line on the following list
Transcribed Image Text:Now suppose that you have an array (AbstractCard[]) that contains both Card1 objects and Card2 objects and you want to sort the array using bubble sort. The pseudocode for bubble sort is as follows: procedure bubbleSort (A : list of sortable items) n := length(A) print "initial:", the list A repeat swapped := false for i := 1 to n-1 inclusive do /+ if this pair is out of order +/ if A[i-1] > A[i] then /* swap them and remember something changed +/ swap(A[i-1), A[i]) // <- this is the red line swapped :- true end if end for n := n - 1 print "it {i}", the list A until not swapped end procedure The line in red (above) is problematic for a couple of reasons. One issue is that this line can translate into java in two ways: if( A[i-1].compareTo (A[i]) > 0 ){ if( A[i].compareTo(A[i-1]) < 0 ){ version 1: version 2: If we run the bubble sort algorithm using version 1 of the red line on the following list
AbstractCard [] cards = {new Card1 ("QD"),new Cardi ("9H"),new Card1("JD"),new Card1 ("AD")};
%3D
the output (what is printed) would be as follows: (colours will NOT be shown, the red indicate
values that are fixed at the end of the iteration of the repeat loop)
initial: [QD, 9H, JD, AD]
it 1: [QD, JD, AD, 9H]
it 2: [JD, QD, AD, 9H]
it 3: [JD, QD, AD, 9H]
For cach of the following cases, run the bubble sort algorithm as described in this question. Display
the output of cach print statement in the bubble sort algorithm as done above. Add
appropriate padding so that the colons are lined up in the output (as above). You do NOT have
to use any colours for this.
(A) Run the bubble sort pseudocode (using version 1 of the red line) on the following list
AbstractCard] carde = {new Card2("QD"),nev Card2 ("9H"),new Card2("JD"),new Card2("AD")};
(B) Run the bubble sort pseudocode (using version 1 of the red linc) on the following list
AbstractCard (] cards = {new Card2("QD"),nev Cardi ("9H"),new Cardi1 ("JD"),new Card2("AD")};
(C) Run the bubble sort pseudocode (using version 2 of the red line) on the following list
AbstractCard ] cards = {new Card2("QD"),nev Cardi ("9H"),new Card1("JD") ,nev Card2("AD")};
In addition to the code traces, describe the results. Why are the 'sorted' lists different? Explain
why care must be taken when different subclasses override the compareTo () method differently.
Write up your solution to this question in a text file called sorting.txt.
Transcribed Image Text:AbstractCard [] cards = {new Card1 ("QD"),new Cardi ("9H"),new Card1("JD"),new Card1 ("AD")}; %3D the output (what is printed) would be as follows: (colours will NOT be shown, the red indicate values that are fixed at the end of the iteration of the repeat loop) initial: [QD, 9H, JD, AD] it 1: [QD, JD, AD, 9H] it 2: [JD, QD, AD, 9H] it 3: [JD, QD, AD, 9H] For cach of the following cases, run the bubble sort algorithm as described in this question. Display the output of cach print statement in the bubble sort algorithm as done above. Add appropriate padding so that the colons are lined up in the output (as above). You do NOT have to use any colours for this. (A) Run the bubble sort pseudocode (using version 1 of the red line) on the following list AbstractCard] carde = {new Card2("QD"),nev Card2 ("9H"),new Card2("JD"),new Card2("AD")}; (B) Run the bubble sort pseudocode (using version 1 of the red linc) on the following list AbstractCard (] cards = {new Card2("QD"),nev Cardi ("9H"),new Cardi1 ("JD"),new Card2("AD")}; (C) Run the bubble sort pseudocode (using version 2 of the red line) on the following list AbstractCard ] cards = {new Card2("QD"),nev Cardi ("9H"),new Card1("JD") ,nev Card2("AD")}; In addition to the code traces, describe the results. Why are the 'sorted' lists different? Explain why care must be taken when different subclasses override the compareTo () method differently. Write up your solution to this question in a text file called sorting.txt.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY