Question: import java.util.*; /** * This program contains the individual card in ArrayofCards with data contained within. The Card class * implements the comparable interface to
import java.util.*;
/** * This program contains the individual card in ArrayofCards with data contained within. The Card class * implements the comparable interface to advance features designed as a LinkedList of Cards. */ public class Card implements Comparable{ private int toughness; private int power; /** * Creates card of random power and toughness within 1 and 100. */ public Card(){ Random rand = new Random(); power = rand.nextInt(100)+1; toughness = rand.nextInt(100)+1; } /** * Creates a card of equal power and toughness so long as within appropriate bounds. * @param x magnitude of power and toughness desired. */ public Card(int x) { if(x > 100){ throw new IndexOutOfBoundsException(); } power = x; toughness = x; } /** * Creates a card with power and toughness equal to input values as long as within appropriate bounds. * @param p input power level * @param t input toughness level */ public Card(int p, int t) { if(p > 100 || t > 100){ throw new IndexOutOfBoundsException(); } power =p; toughness =t; } /** * Get the current power of Card * @return power of Card */ public int getPower(){ return power; } /** * Get the current toughness of Card * @return toughness of Card */ public int getToughness(){ return toughness; } /** * Get the current cost of card * @return cost of card */ public int getCost(){ return ((int) Math.abs(Math.pow(((2 * power) + toughness),.5)*10)); // = |(2 + )^(.5)*10 | } /** * Shows current power, toughness, and cost of the card in the order described. * @return String of Card attributes. */ public String toString(){ return ("[" + power + "/" + toughness + ":" + getCost() + "]"); // [x/x:x] } /** * Reduce power and toughness of card by 2 permanently as long as value is above zero. */ public void weaken(){ if(toughness >2 ){ toughness = toughness -2; } if(power >2){ power = power -2; } } /** * Increase power and toughness of card by 2 permanently as long as value is below one hundred one. */ public void boost(){ if(toughness < 101 ){ toughness = toughness + 2; } if(power < 101){ power = power + 2; } } /** * Compares two cards to see which is greater or smaller in terms of the first card. Comparison begins with * Cost, and if equal comparison to power begins, and if equal toughness. If first card is greater, a positive * value is returned. If smaller, a negative is returned. If both cards are equal in Cost, power, toughness, return * zero. * @param x Card to compare to. * @return positive if getCard is greater and negative if getCard is smaller. */ public int compareTo(Card x){ if(getCost() > x.getCost()) { return 10; } if(x.getCost()> getCost()){ return -10; } if(getPower()> x.getPower()) { return 10; } if(x.getPower()> getPower()){ return -10; } if(getToughness() > x.getToughness()) { return 10; } if(x.getToughness()> getCost()){ return -10; } return 0; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
