|
|
mkdir ~/cs170/lab12 cd ~cs170002/share/lab12 cp *.java ~/cs170/lab12 cd ~/cs170/lab12 |
You will have these files:
Card.java - the base class TwoHighCard.java - the derived class TestTwoHigh1.java - Test program used for task 1 TestTwoHigh2.java - Test program used for task 2 TestTwoHigh3.java - Test program used for task 3 |
Execute the following command to list your lab12 directory and make sure you have copied all the files:
ls ~/cs170/lab12
|
public class TestTwoHigh1
{
public static void main( String[] args )
{
TwoHighCard a = new TwoHighCard( ); // Make a TwoHightCard object
System.out.println( a ); // Print it
}
}
|
In this program, we create a TwoHighCard object using no parameters:
new TwoHighCard( )
|
javac TestTwoHigh1.java
java TestTwoHigh1
|
You will see this message:
TwoHighCard@2a9931f5
|
|
After you have done so, compile and run the TestTwoHigh1 program again:
javac TestTwoHigh1.java
java TestTwoHigh1
|
You will see this message:
Can you figured out why you see this message ? Js |
Explanation: (there are 2 special effects built into the Java compiler that made this happen)
|
|
After you have made this change, compile and run the TestTwoHigh1 program again:
javac TestTwoHigh1.java
java TestTwoHigh1
|
You should see this message:
I have figured out why I see this message ! Js |
|
public class TestTwoHigh2
{
public static void main( String[] args )
{
TwoHighCard a = new TwoHighCard( 4, 1 );
System.out.println( a );
}
}
|
In this program, we create a TwoHighCard object using these type of parameters:
new TwoHighCard( int, int )
|
The first integer is the code for the suit and the second integer is the code for the rank
javac TestTwoHigh2.java
|
You will see these kinds of errors:
TestTwoHigh2.java:11: cannot find symbol
symbol : constructor TwoHighCard(int,int)
location: class TwoHighCard
TwoHighCard a = new TwoHighCard( 4, 1 );
^
|
This is caused by the fact that there is no constructor method defined inside TwoHighCard.java that takes 2 integer parameters
|
javac TestTwoHigh2.java
java TestTwoHigh2
If successful, the output is:
As
|
|
TwoHighCard a = new TwoHighCard( ..., ... );
TwoHighCard b = new TwoHighCard( ..., ... );
if ( a.beats(b) )
System.out.println( a + " beats " + b );
else
System.out.println( a + " does not beat " + b );
|
The call a.beats(b) will return:
|
Task 3:
|
After you have done so, compile and run the TestTwoHigh1 program again:
javac TestTwoHigh3.java
java TestTwoHigh3
|
You will see this output:
The cards: 2s rank = 2 2h rank = 2 As rank = 14 Testing beats(): 2s beats 2h: false 2s beats As: true 2h beats 2s: false 2h beats As: true As beats 2s: false As beats 2h: false |
Notice that:
|
cd ~/cs170/lab12
/home/cs170002/turnin-lab Card.java lab12
/home/cs170002/turnin-lab TwoHighCard.java lab12a
|