Question: Hello, I have an assignment which I don't know how to solve. I have most of the functions and method ready, and all I need

Hello, I have an assignment which I don't know how to solve.

I have most of the functions and method ready, and all I need is to create 3 more methods.

The requirements:

Hello, I have an assignment which I don't know how to solve.

**********************************************************

THE CODE

// *****The Code I have. The 3 methods are at //the end of "main" ********* package Leeor;

import java.util.LinkedList; import java.util.ListIterator;

public class Foothill { public static void main(String[] args) {

Card first = new Card('A', Card.Suit.spades); Card second = new Card('4', Card.Suit.hearts); Card third = new Card('T', Card.Suit.clubs);

System.out.println( "should all be 0: "); System.out.println( first.compareTo( first ) ); System.out.println( second.compareTo( second ) ); System.out.println( third.compareTo( third ) );

System.out.println( " should all be

System.out.println( " should all be > 0: "); System.out.println( first.compareTo( second ) ); System.out.println( third.compareTo( second ) ); System.out.println( first.compareTo( third ) );

System.out.println( " Some random cards: "); for ( k = 0; k

// "global" static Foothill methods static Card generateRandomCard() { // if firstTime = true, use clock to seed, else fixed seed for debugging Card.Suit suit; char val;

int suitSelector, valSelector;

// get random suit and value suitSelector = (int) (Math.random() * 4); valSelector = (int) (Math.random() * 13);

// pick suit suit = turnIntIntoSuit(suitSelector); val = turnIntIntoVal(valSelector);

return new Card(val, suit); }

// note: this method not needed if we use int for suits instead of enum static Card.Suit turnIntIntoSuit(int k) { return Card.Suit.values()[k]; // }

static char turnIntIntoVal(int k) { String legalVals = "23456789TJQKA";

if (k = legalVals.length()) return '?'; return legalVals.charAt(k); }

// need to do static boolean removeAll(LinkedList my_List, Card x)

// need to do static boolean insert

// need to do static boolean remove

}

class Card { public enum Suit { clubs, diamonds, hearts, spades }

private char value; private Suit suit; boolean errorFlag;

protected static Suit[] suitRanks = {Suit.clubs, Suit.diamonds, Suit.hearts, Suit.spades}; protected static char[] valueRanks = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};

protected static final int NUM_VALS = 13;

public Card(char value, Suit suit) { set(value, suit); }

public Card(char value) { this(value, Suit.spades); } public Card() { this('A', Suit.spades); }

public Card(Card card) { this(card.value, card.suit); }

public boolean set(char value, Suit suit) { char upVal; upVal = Character.toUpperCase(value);

if ( !isValid(upVal, suit)) { errorFlag = true; return false; }

errorFlag = false; this.value = upVal; this.suit = suit; return true; }

public char getVal() { return value; }

public Suit getSuit() { return suit; }

public boolean getErrorFlag() { return errorFlag; }

public boolean equals(Card card) { if (this.value != card.value) return false; if (this.suit != card.suit) return false; if (this.errorFlag != card.errorFlag) return false; return true; }

public String toString() { String retVal;

if (errorFlag) return "** illegal **";

retVal = String.valueOf(value); retVal += " of "; retVal += String.valueOf(suit);

return retVal; }

private boolean isValid(char value, Suit suit) { char upVal;

upVal = Character.toUpperCase(value);

if ( upVal == 'A' || upVal == 'K' || upVal == 'Q' || upVal == 'J' || upVal == 'T' || (upVal >= '2' && upVal

return ( getValueRank(this.value) - getValueRank(other.value) ); } public static int getSuitRank(Suit st) { int k;

for (k = 0; k

// should not happen return 0; } public static int getValueRank(char val) { int k;

for (k = 0; k

// should not happen return 0; } }

Understand the Problem In the lectures we instantiated an java.util LinkedList of Floats and, above it, built some insert) and remove() methods on the client side that managed the list in increasing sorted order. In this assignment we will do the same for Card objects, using the natural ordering that was established in a past exercise. Before starting the assignment, you will need to bring in three components, one from the past and two supplied below I. The Card class from lab #1 2. compareTo) mechanism for the Card class, supplied below. 3. The generateRandomCard method, supplied below Then, you will provide cleint-side (i.e. static Foothill class methods) insert)remove0 and removeAll0 like in the lessons. They take an java.util LinkedList of Cards (first parameter) and a Card (second parameter). The Details java.util LinkedList There are very few changes you'll need to make to the overall design of what you saw: static boolean removeAll(LinkedList Card my-List, Card x))-Notice that this method returns a boolean which should be true if x was in the list, and false if it was not. . Use compareTo0 in your insert() and/or remove) methods, as needed, for any test: equality, less than or greater than equality is based on Card's compareTo0. not based on field-by-held comparisons. -. Nothing should be changed in any of the classes or methods brought in from the earlier assignment other than the additions I suppy, below. Everything else should be added as global scope code. Client Notes Instantiate about five to ten cards randomly using the lab #1 code), but produce duplicates of every card in the list every card you generate goes into your list twice. Display the list immediately after you build it. (Note, you might have four, not two, of some cards because of generateRandomCard), and that's fine. (Maybe even six or eight or.. Display the list. Next, pick about half of the cards you inserted for removal, but do it as follows. If you instantiated five cards initially, and put them into the list twice, then your list has 10 cards. Pick, say, two distinct cards from your initial five and remove) all traces of each of those two cards. I don't want you to use removeAll0 for this step. Instead useremove), which only removes one instance of a card. So you'll have to use remove) on the same card repeatedly. Le., if 3 of diamonds is a card, remove all copies of 3 of Diamonds, not just the first. Don't rely on any knowledge you might have about how many copies of the 3 of diamonds are in the list, but use an intelligent loop to keep remove0-ing it until the return value of remove) tells you the loop is done. Display the list and confirm that it looks right after the removal. Finally, test removeAllO's return value somehow after the above test

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!