Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please add detailed and useful comments for a beginner Java coder to understand these classes: //--------------------------------- public class Quad extends Polygon { public Quad(int a,
Please add detailed and useful comments for a beginner Java coder to understand these classes:
//--------------------------------- public class Quad extends Polygon { public Quad(int a, int b, int c, int d) { int[] lengths = {a, b, c, d}; this.sides = lengths; } } //------------------------------------- public class Rectangle extends Quad { public Rectangle(int w, int h) { super(w,h,w,h); } public int getPerimeter() { System.out.println("Rect:"); return (sides[0] * 2) + (2 * sides[1]); } } //------------------------------------ public class Polygon { protected int[] sides; public int getPerimeter() { System.out.println("Poly:"); int sum = 0; for (int i = 0; i < sides.length; i++) { sum += sides[i]; } return sum; } } //----------------------------------- public class Card { public static final String CLUBS = "Clubs"; public static final String HEARTS = "Hearts"; public static final String DIAMONDS = "Diamonds"; public static final String SPADES = "Spades"; public static final int ACE = 14; public static final int KING = 13; public static final int QUEEN = 12; public static final int JACK = 11; private String suit; private int face; public Card(String suit, int face){ this.suit = suit; this.face = face; } public String getSuit(){ return suit; } public void setSuit(String suit){ this.suit = suit; } public int getFace(){ return face; } public void setFace(int face){ this.face = face; } public String toString(){ return "Card{" + "suit=" + suit + ", face=" + face + '}'; } public boolean equals(Card other){ boolean result = false; try{ boolean suitMatch = this.suit.equals(other.getSuit()); boolean faceMatch = this.face == other.getFace(); if(suitMatch && faceMatch){ result = true; } } catch (NullPointerException e){ result = false; } return result; } //------------------- public class Deck { ArrayListcards; public Deck() { cards = new ArrayList<>(52); for(int face = 2; face <= Card.ACE; face++) { cards.add(new Card(Card.CLUBS, face)); cards.add(new Card(Card.DIAMONDS, face)); cards.add(new Card(Card.HEARTS, face)); cards.add(new Card(Card.SPADES, face)); } } public void shuffle() { Random r = new Random(); ArrayList shuffled = new ArrayList(52); while(cards.size() > 0) { int n = r.nextInt(cards.size()); Card temp = cards.remove(n); shuffled.add(temp); } cards = shuffled; } public Card draw() { Card top = cards.remove(0); return top; } public int size(){ return cards.size(); } public boolean isEmpty() { return cards.isEmpty(); } public String toString() { return "Deck{" + "cards=" + cards + '}'; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started