Create a class called Memory card. 1.Create class variables to hold its name (a
ID: 672886 • Letter: C
Question
Create a class called Memory card.
1.Create class variables to hold its name (a String), whether it is the first copy (a boolean), its picture (an Image), whether it is flipped (a boolean), and its location (a Square, not a MemorySquare).
2. Make MemoryCard implement the Card interface.
3.Create a constructor that takes a String (the name of the card), a boolean (if it is the first copy or not), and an Image. If the boolean parameter is true, it is the first copy. If false, it is the second copy. Use the parameters to update the appropriate class variables. The card’s initial location should be null (which indicates it is not on the board) and it should not be flipped.
4.Implement the methods from the Card interface.
a) isFirstCopy - Returns true if this card is the first copy of this particular card on the board. Otherwise it returns false. Return the appropriate variable.
b) isSecondCopy – Returns true if this card is the second copy of this particular card on the board. Otherwise it returns false. This class doesn’t store a variable that is true if it is the second copy of the card; it stores a variable that is true if it is first copy and, consequently, false if it is the second copy. How can you use that variable to get a value that is true if it is the second copy and false if it is the first? This can also be done in one line of code.
c) toString - Creates a String for the card based on the following format:
<name> (<copy>)
Where
<name> is the name of the card
<copy> is either 1 or 2 depending on if the card is the first or second copy
If the name is null, an empty string (“”) should be returned.
d) display – Displays this card in the current graphics context within the supplied rectangle.
e) getSquare – Gets a reference to the square this card is currently on. Return the appropriate variable.
f) setSquare – Updates this card’s current square. This is a setter, so update the appropriate variable.
g) getName – Gets the name of the card. Return the appropriate variable.
h) isFlipped – Returns true if the card is flipped and false otherwise. Return the appropriate variable.
i) setFlipped – Updates whether the card is flipped or not. If the parameter is true, it is flipped (i.e. showing its front), if it is false it is not flipped (i.e. is showing its back).
j) isMatchingCard – Returns true if the card is the match to the parameter. If the name of the card is null, then it is not a match and should return false. If the name of the card and the name of the parameter are equal and one is a first copy and the other is a second copy, then the cards are a match and this should return true. Otherwise, return false.
k) getMatchingCard – This method finds the card on the board that matches this card. This only works if the name of the card is not null, the card is on a square
Explanation / Answer
MemoryCard class:
import java.awt.Image;
public class Memorycard implements Card{
char name;
boolean isFirstCopy;
Image image1;
boolean isFlipped;
int location;
Memorycard(char name,boolean isFirstCopy,Image image,int location)
{
char name1=name;
if(isFirstCopy=false)
{
boolean secondcopy=true;
}
location=0;
}
public boolean isFirstCopy() {
// TODO Auto-generated method stub
return false;
}
public boolean isSecondCopy() {
// TODO Auto-generated method stub
return false;
}
public void display() {
// TODO Auto-generated method stub
}
public int getSquare() {
// TODO Auto-generated method stub
return 0;
}
public int setSquare() {
// TODO Auto-generated method stub
return 0;
}
public char getName() {
// TODO Auto-generated method stub
return 0;
}
public boolean isFlipped() {
// TODO Auto-generated method stub
return false;
}
public boolean setFlipped() {
// TODO Auto-generated method stub
return false;
}
public boolean isMatchingCard() {
// TODO Auto-generated method stub
return false;
}
public char getMatchingCard() {
// TODO Auto-generated method stub
return 0;
}
}
Card Interface:
public interface Card {
boolean isFirstCopy();
boolean isSecondCopy();
String toString();
void display();
int getSquare();
int setSquare();
char getName();
boolean isFlipped();
boolean setFlipped();
boolean isMatchingCard();
char getMatchingCard();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.