******** I do not understand how using \"this \". in the code. Can someone expla
ID: 3569315 • Letter: #
Question
******** I do not understand how using "this". in the code. Can someone explain to me how it works? Also, I need Method comments to be written for the code down below.****************
****** This is an example of what kind of Method comments I want. ********
/**
Computes the volume of a cube.
@param sideLength the side length of the cube
@return the volume
*/
**********************
import java.util.*;
class Card
{
private int rank;
private char suit;
public Card()
{
this.suit = 'S';
this.rank = '1';
}
public Card(int r, char s)
{
this.suit = s;
this.rank = r;
}
public void setCard(int r, char s)
{
this.rank = r;
this.suit = s;
}
public int getRank()
{
return this.rank;
}
public char getSuit()
{
return this.suit;
}
public boolean equals(Card c)
{
return this.getRank() == c.getRank() && this.getSuit() == c.getSuit();
}
public int compareTo(Card c)
{
int ret = 0;
if(this.getRank()-c.getRank()<0)ret = -1;
if(this.getRank()-c.getRank()>0)ret = 1;
return ret;
}
public String toString()
{
String r = "";
switch(this.getRank())
{
case 1:r = "A";break;
case 11:r = "J";break;
case 12:r = "Q";break;
case 13:r = "K";break;
default:r = this.getRank()+"";
}
return r + this.getSuit();
}
}
class Pile
{
private ArrayList pile;
public Pile()
{
pile = new ArrayList();
}
public Pile(int num)
{
pile = new ArrayList(num);
if(num == 52)
{
char c = 'S';
for(int j = 0; j<4; j++)
{
if(j == 1)c='H';
if(j == 2)c='D';
if(j == 3)c='C';
for(int i = 1; i < 14; i++)
{
this.pile.add(new Card(i,c));
}
}
}
}
public Card dealCard()
{
Card c = pile.get(0);
pile.remove(0);
return c;
}
public boolean addCard(Card c)
{
return this.pile.add(c);
}
public boolean addPile(Pile p)
{
boolean ret = true;
int i = 0;
Pile p1 = new Pile(this.pile.size());
while(ret)
{
if(!p1.pile.add(p.pile.get(i)))ret = false;
i++;
}
if(ret)
{
i = 0;
while(ret)
{
if(!this.pile.add(p.pile.get(i)))ret = false;
i++;
}
}
return ret;
}
public void shuffle()
{
for(int i= 0; i < 100; i++)
{
int r1 = (int)(Math.random()*this.pile.size());
int r2 = (int)(Math.random()*this.pile.size());
Card c1 = this.pile.get(r1);
Card c2 = this.pile.get(r2);
this.pile.set(r1, c2);
this.pile.set(r2, c1);
}
}
public String toString()
{
String ret = "";
for(int i = 0; i < this.pile.size(); i++)
{
if(i % 10 == 0 && i != 0)ret+=" ";
ret += this.pile.get(i)+" ";
}
return ret;
}
}
public class Driver
{
public static void main(String[] args)
{
Card c = new Card();
c.setCard(3, 'A');
System.out.println("Card1: " +c.toString());
Card c2 = new Card(4, 'S');
System.out.println("Card2: " + c2.toString());
System.out.println("Card1 compared to Card2: " + c.compareTo(c2));
System.out.println(c.equals(c2));
Pile p = new Pile(52);
System.out.println("Before shuffling: ");
System.out.println(p.toString());
p.shuffle();
System.out.println("After shuffling: ");
System.out.println(p.toString());
}
}
Explanation / Answer
import java.util.*;
class Card
{
private int rank;
private char suit;
//this keyword is used to refer current class instance variable.
//eg: Card c = new Card() => this.suit is equivalent to c.suit. Since out constructor or function doesn't know c variable, we use this.
public Card() //public constructor to set with default values.
{
this.suit = 'S';
this.rank = '1';
}
public Card(int r, char s) //parameterized constructor.
{
this.suit = s;
this.rank = r;
}
public void setCard(int r, char s) //set the suit and rank
{
this.rank = r;
this.suit = s;
}
public int getRank() //return rank
{
return this.rank;
}
public char getSuit() //return suit
{
return this.suit;
}
//return true if two cards are equal.
// eg: a.equals(c); => a.rank is equivalent to this.rank. "this" basically refers to a
public boolean equals(Card c)
{
return this.getRank() == c.getRank() && this.getSuit() == c.getSuit();
}
//compare two cards. explanation of using "this" is same
public int compareTo(Card c)
{
int ret = 0;
if(this.getRank()-c.getRank()<0)ret = -1;
if(this.getRank()-c.getRank()>0)ret = 1;
return ret;
}
//prints the card.
public String toString()
{
String r = "";
switch(this.getRank())
{
case 1:r = "A";break;
case 11:r = "J";break;
case 12:r = "Q";break;
case 13:r = "K";break;
default:r = this.getRank()+"";
}
return r + this.getSuit();
}
}
//class of set of cards.
class Pile
{
private ArrayList pile;
public Pile()
{
pile = new ArrayList();
}
public Pile(int num)
{
pile = new ArrayList(num);
if(num == 52)
{
char c = 'S';
for(int j = 0; j<4; j++)
{
if(j == 1)c='H';
if(j == 2)c='D';
if(j == 3)c='C';
for(int i = 1; i < 14; i++)
{
this.pile.add(new Card(i,c));
}
}
}
}
public Card dealCard() //get the first card from pile
{
Card c = pile.get(0);
pile.remove(0);
return c;
}
public boolean addCard(Card c) //add the card to end of pile
{
return this.pile.add(c);
}
public boolean addPile(Pile p) //add pile of cards.
{
boolean ret = true;
int i = 0;
Pile p1 = new Pile(this.pile.size());
while(ret)
{
if(!p1.pile.add(p.pile.get(i)))ret = false;
i++;
}
if(ret)
{
i = 0;
while(ret)
{
if(!this.pile.add(p.pile.get(i)))ret = false;
i++;
}
}
return ret;
}
public void shuffle() //shuffle the cards position in pile
{
for(int i= 0; i < 100; i++)
{
int r1 = (int)(Math.random()*this.pile.size());
int r2 = (int)(Math.random()*this.pile.size());
Card c1 = this.pile.get(r1);
Card c2 = this.pile.get(r2);
this.pile.set(r1, c2);
this.pile.set(r2, c1);
}
}
public String toString() //prints the pile of cards
{
String ret = "";
for(int i = 0; i < this.pile.size(); i++)
{
if(i % 10 == 0 && i != 0)ret+=" ";
ret += this.pile.get(i)+" ";
}
return ret;
}
}
//class to test the program
public class Driver
{
public static void main(String[] args)
{
Card c = new Card();
c.setCard(3, 'A');
System.out.println("Card1: " +c.toString());
Card c2 = new Card(4, 'S');
System.out.println("Card2: " + c2.toString());
System.out.println("Card1 compared to Card2: " + c.compareTo(c2));
System.out.println(c.equals(c2));
Pile p = new Pile(52);
System.out.println("Before shuffling: ");
System.out.println(p.toString());
p.shuffle();
System.out.println("After shuffling: ");
System.out.println(p.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.