Question: I can not make this code COMPILE nor RUN please help! import java.math.BigInteger; public class Card{ public static void main(String[] args) { Card card =

I can not make this code COMPILE nor RUN please help!

import java.math.BigInteger;

public class Card{

public static void main(String[] args) {

Card card = new Card(1, 1);

card.print();

String s = card.toString();

System.out.println(s);

System.out.println(card);

Card card2 = new Card(1, 1);

System.out.println(card.equals(card2));

Deck deck = new Deck();

deck.shuffle();

deck.sort();

checkSorted(deck);

int index;

for (int i=0; i<52; i++) {

index = deck.findBisect(deck.cards[i], 0,

deck.cards.length-1);

if (index != i) {

System.out.println("Not found!");

}

}

Deck hand = deck.subdeck(8, 12);

hand.print();

Card badCard = new Card(1, 1);

index = hand.findBisect(badCard, 0, hand.cards.length-1);

if (index != -1) {

System.out.println("Found?");

}

deck.shuffle();

deck = deck.mergeSort();

checkSorted(deck);

}

public static void checkSorted(Deck deck) {

for (int i=0; i<51; i++) {

int flag = deck.cards[i].compareTo(deck.cards[i+1]);

if (flag != -1) {

System.out.println("Not sorted!");

}

}

}

}

class Card {

int suit, rank;

static String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };

static String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

public Card() {

this.suit = 0; this.rank = 0;

}

public Card(int suit, int rank) {

this.suit = suit; this.rank = rank;

}

public void print() {

System.out.println(ranks[rank] + " of " + suits[suit]);

}

public String toString() {

return ranks[rank] + " of " + suits[suit];

}

public boolean equals(Card that) {

return (this.suit == that.suit && this.rank == that.rank);

}

public int compareTo(Card that) {

if (this.suit > that.suit) return 1;

if (this.suit < that.suit) return -1;

int rank1 = (this.rank + 11) % 13;

int rank2 = (that.rank + 11) % 13;

if (rank1 > rank2) return 1;

if (rank1 < rank2) return -1;

return 0;

}

}

class Deck {

Card[] cards;

public Deck(int n) {

this.cards = new Card[n];

}

public Deck() {

this.cards = new Card [52];

int index = 0;

for (int suit = 0; suit <= 3; suit++) {

for (int rank = 1; rank <= 13; rank++) {

this.cards[index] = new Card(suit, rank);

index++;

}

}

}

public void print() {

for (int i=0; i

cards[i].print();

}

}

public int findCard (Card card) {

for (int i = 0; i< cards.length; i++) {

if (card.equals(cards[i])) {

return i;

}

}

return -1;

}

public int findBisect(Card card, int low, int high) {

if (high < low) return -1;

int mid = (high + low) / 2;

int comp = card.compareTo(cards[mid]);

if (comp == 0) {

return mid;

} else if (comp < 0) {

return findBisect(card, low, mid-1);

} else {

return findBisect(card, mid+1, high);

}

}

public int randInt(int low, int high) {

int x = (int)(Math.random() * (high-low) + low);

return x;

}

public void swapCards(int i, int j) {

Card temp = cards[i];

cards[i] = cards[j];

cards[j] = temp;

}

public void shuffle() {

for (int i=0; i

int j = randInt(i, cards.length-1);

swapCards(i, j);

}

}

public void sort() {

for (int i=0; i

int j = indexLowestCard(i, cards.length-1);

swapCards(i, j);

}

}

public int indexLowestCard(int low, int high) {

int winner = low;

for (int i=low+1; i<=high; i++) {

if (cards[i].compareTo(cards[winner]) < 0) {

winner = i;

}

}

return winner;

}

public Deck subdeck(int low, int high) {

Deck sub = new Deck(high-low+1);

for (int i = 0; i

sub.cards[i] = cards[low+i];

}

return sub;

}

public static Deck merge(Deck d1, Deck d2) {

Deck result = new Deck (d1.cards.length + d2.cards.length);

int choice;

int i = 0;

int j = 0;

for (int k = 0; k < result.cards.length; k++) {

choice = 1;

if (i == d1.cards.length)

choice = 2;

else if (j == d2.cards.length)

choice = 1;

else if (d1.cards[i].compareTo(d2.cards[j]) > 0)

choice = 2;

if (choice == 1) {

result.cards[k] = d1.cards[i]; i++;

} else {

result.cards[k] = d2.cards[j]; j++;

}

}

return result;

}

public Deck mergeSort() {

if (cards.length < 2) {

return this;

}

int mid = (cards.length-1) / 2;

Deck d1 = subdeck(0, mid);

Deck d2 = subdeck(mid+1, cards.length-1);

d1 = d1.mergeSort();

d2 = d2.mergeSort();

return merge(d1, d2);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!